Foros del Web » Programando para Internet » Python »

opencv-Python

Estas en el tema de opencv-Python en el foro de Python en Foros del Web. Buenos Días amigos, soy nuevo en el foro. Estoy necesitando ayuda con la utilización de las librerías de opencv con Python. Antes de plantear el ...
  #1 (permalink)  
Antiguo 10/03/2016, 03:35
 
Fecha de Ingreso: marzo-2016
Mensajes: 1
Antigüedad: 8 años, 1 mes
Puntos: 0
opencv-Python

Buenos Días amigos, soy nuevo en el foro. Estoy necesitando ayuda con la utilización de las librerías de opencv con Python. Antes de plantear el problema les aclaro que no se mucho de programación, ya que, no es mi área de estudio y les pediría el favor de que me hablar en lenguaje mas explicito de ser posible. En la tesis que estoy desarrollando surgió la idea de realizar geometría epipolar para obtener un estimado de las dimensiones de un talúd. He utilizado los códigos que están en la página de opencv 2.4.12, ya que, este trae un algoritmo llamado sift() y flann() que en la nueva versión no los trae compilados. Estoy en el punto de rectificación de las imágenes, pero al realizarlo las mismas se ven fatal.

Los datos de la calibración se importan a estos códigos

Este es un código de rectificación de las imágenes:

R1, R2, P1, P2, Q1, ValidRoiL, ValidRoiR = cv2.stereoRectify(MatrizDeLaCamaraIzq, CoefDistIzq, MatrizDeLaCamaraDer, CoefDistDer, (w1, h1), MtzRot, MtzTra, flags = cv2.CALIB_ZERO_DISPARITY, alpha = -1, newImageSize = (w1, h1))
#PathTxtR1b = PathSal + NombreCamara + 'R1' + '.txt'
#PathTxtR2b = PathSal + NombreCamara + 'R2' + '.txt'
#np.savetxt(PathTxtR1b, R1, fmt='%s', delimiter=' ', newline='\n', header='R1', footer='', comments='# ')
#np.savetxt(PathTxtR2b, R2, fmt='%s', delimiter=' ', newline='\n', header='R2', footer='', comments='# ')


MapxIzq, MapyIzq = cv2.initUndistortRectifyMap(MatrizDeLaCamaraIzq, CoefDistIzq, R1, P1, (w1, h1), cv2.CV_32FC1)
MapxDer, MapyDer = cv2.initUndistortRectifyMap(MatrizDeLaCamaraDer, CoefDistDer, R2, P2, (w1, h1), cv2.CV_32FC1)
NombreCamara = 'Sony'
PathEntIzq = 'C:/Users/Luis/Desktop/Python OpenCV/ImagenStereo_Izq/'
PathEntDer = 'C:/Users/Luis/Desktop/Python OpenCV/ImagenStereo_Der/'
PathSal = 'C:/Users/Luis/Desktop/Python OpenCV/'
PathCarpetaIzq = PathSal + 'Imagenes Estereo Rectificadas_Met1_Izq' + '_' + time.strftime("%A") + time.strftime("%d") + time.strftime("%b") + time.strftime("%Y") + time.strftime("%H") + time.strftime("%M") + time.strftime("%S")
PathCarpetaDer = PathSal + 'Imagenes Estereo Rectificadas_Met1_Der' + '_' + time.strftime("%A") + time.strftime("%d") + time.strftime("%b") + time.strftime("%Y") + time.strftime("%H") + time.strftime("%M") + time.strftime("%S")
os.mkdir(PathCarpetaIzq)
os.mkdir(PathCarpetaDer)
ListaDeImagenesIzq = glob.glob(os.path.join(PathEntIzq,'*.jpg'))
ListaDeImagenesDer = glob.glob(os.path.join(PathEntDer,'*.jpg'))

NumImagen = int(1)

for fnameIzq,fnameDer in zip(ListaDeImagenesIzq,ListaDeImagenesDer):

ImagenEstIzq = cv2.imread(fnameIzq,0) #queryimage # left image
ImagenEstDer = cv2.imread(fnameDer,0) #trainimage # right image
r,c = ImagenEstIzq.shape
DstIzq = cv2.remap(ImagenEstIzq,MapxIzq,MapyIzq,cv2.INTER_L INEAR)
DstDer = cv2.remap(ImagenEstDer,MapxDer,MapyDer,cv2.INTER_L INEAR)
# x,y,w,h = ValidRoiL
# DstIzq = DstIzq[y:y+h, x:x+h]
# DstDer = DstDer[y:y+h, x:x+h]
# DstIzq = DstIzq[MapxIzq, MapyIzq]
# DstDer = DstDer[MapxDer, MapyDer]
PathCarpetaImgEstIzq = PathCarpetaIzq + '/' + str(NumImagen) + '_Rectificada_Met1.png'
PathCarpetaImgEstDer = PathCarpetaDer + '/' + str(NumImagen) + '_Rectificada_Met1.png'
cv2.imwrite(PathCarpetaImgEstIzq,DstIzq)
cv2.imwrite(PathCarpetaImgEstDer,DstDer)
NumImagen = NumImagen + int(1)

print('Se han Rectificado las imagenes.')



Y este es otro código de rectificación de imágenes:


def drawlines(ImagenEstIzq,ImagenEstDer,lines,ptsIzqE, ptsDerE):
''' img1 - image on which we draw the epilines for the points in img2
lines - corresponding epilines '''
r,c = ImagenEstIzq.shape
ImagenEstIzq = cv2.cvtColor(ImagenEstIzq, cv2.COLOR_GRAY2BGR)
ImagenEstDer = cv2.cvtColor(ImagenEstDer, cv2.COLOR_GRAY2BGR)
PtsIzq = ptsIzqE.astype('int')
PtsDer = ptsDerE.astype('int')
for r,pt1,pt2 in zip(lines,PtsIzq,PtsDer):
color = tuple(np.random.randint(0,255,3).tolist())
x0,y0 = map(int, [0, -r[2]/r[1] ])
x1,y1 = map(int, [c, -(r[2]+r[0]*c)/r[1] ])
cv2.line(ImagenEstIzq, (x0,y0), (x1,y1), color, 1)
cv2.circle(ImagenEstIzq,tuple(pt1),5,color,-1)
cv2.circle(ImagenEstDer,tuple(pt2),5,color,-1)
return ImagenEstIzq,ImagenEstDer

for fnameIzq,fnameDer in zip(ListaDeImagenesIzq,ListaDeImagenesDer):
ImagenEstIzq = cv2.imread(fnameIzq,0) #queryimage # left image
ImagenEstDer = cv2.imread(fnameDer,0) #trainimage # right image
r,c = ImagenEstIzq.shape
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kpIzq, desIzq = sift.detectAndCompute(ImagenEstIzq,None)
kpDer, desDer = sift.detectAndCompute(ImagenEstDer,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(desIzq,desDer,k=2)
good = []
ptsIzq = []
ptsDer = []

# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
if m.distance < 0.8*n.distance:
good.append(m)
ptsDer.append(kpDer[m.trainIdx].pt)
ptsIzq.append(kpIzq[m.queryIdx].pt)
ptsIzq = np.float64(ptsIzq)
ptsDer = np.float64(ptsDer)
F, mask = cv2.findFundamentalMat(ptsIzq,ptsDer,cv2.FM_LMEDS)

# We select only inlier poin2ts
ptsIzqE = ptsIzq[mask.ravel()==1]
ptsDerE = ptsDer[mask.ravel()==1]
ptsDerL = ptsDerE.reshape(-1,1,2).astype('float32')
ptsIzqL = ptsIzqE.reshape(-1,1,2).astype('float32')

# Find epilines corresponding to points in right image (second image) and
# drawing its lines on left image
linesIzq = cv2.computeCorrespondEpilines(ptsDerL, 2,F)
linesIzq = linesIzq.reshape(-1,3)
# ptsDer = np.int32(ptsDer)
# ptsIzq = np.int32(ptsIzq)
ImagenEstIzq1,ImagenEstDer1 = drawlines(ImagenEstIzq,ImagenEstDer,linesIzq,ptsIz qE,ptsDerE)

# Find epilines corresponding to points in left image (first image) and
# drawing its lines on right image
linesDer = cv2.computeCorrespondEpilines(ptsIzqL, 1,F)
linesDer = linesDer.reshape(-1,3)
ImagenEstDer2,ImagenEstIzq2 = drawlines(ImagenEstDer,ImagenEstIzq,linesDer,ptsDe rE,ptsIzqE)
plt.subplot(121),plt.imshow(ImagenEstIzq1)
plt.subplot(122),plt.imshow(ImagenEstDer2)
plt.show()

# Se utiliza para calcular la homografía de las imagenes
# ptsIzq = np.array(ptsIzq)
# ptsDer = np.array(ptsDer)
retval, H1, H2 = cv2.stereoRectifyUncalibrated(ptsIzqL, ptsDerL, F, (c, r), threshold = 5)
MtzIzq = np.matrix(MatrizDeLaCamaraIzq).I
MtzDer = np.matrix(MatrizDeLaCamaraDer).I
RIzq = MtzIzq*np.matrix(H1)*np.matrix(MatrizDeLaCamaraIzq )
RDer = MtzDer*np.matrix(H2)*np.matrix(MatrizDeLaCamaraDer )
mapx1,mapy1 = cv2.initUndistortRectifyMap(MatrizDeLaCamaraIzq, CoefDistIzq, RIzq, MatrizDeLaCamaraIzq, (c, r), cv2.CV_16SC2)
mapx2,mapy2 = cv2.initUndistortRectifyMap(MatrizDeLaCamaraDer, CoefDistDer, RDer, MatrizDeLaCamaraDer, (c, r), cv2.CV_16SC2)
dst1 = cv2.remap(ImagenEstIzq,mapx1,mapy1,cv2.INTER_LINEA R)
dst2 = cv2.remap(ImagenEstDer,mapx2,mapy2,cv2.INTER_LINEA R)
# dst1 = np.uint8(dst1)
# dst2 = np.uint8(dst2)
# dst1 = dst1[mapx1,mapy1]
# dst2 = dst2[mapx2,mapy2]
PathCarpetaImgEstIzq = PathCarpetaIzq + '/' + fnameIzq + '_Rectificada_Met2.png'
PathCarpetaImgEstDer = PathCarpetaDer + '/' + fnameDer + '_Rectificada_Met2.png'
plt.subplot(121),plt.imshow(dst1)
plt.subplot(122),plt.imshow(dst2)
plt.show()
cv2.imwrite(PathCarpetaImgEstIzq, dst1)
cv2.imwrite(PathCarpetaImgEstDer, dst2)

Les agradezco su colaboración.
Saludos

Etiquetas: int, programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:21.