def projectionPlots(rV, tV, data): ''' compara los puntos de calibracio y sus proyecciones en las tres etapas ''' imagePoints, objectPoints, cameraMatrix, model = data imagePointsProj = cl.direct(objectPoints, rV, tV, cameraMatrix, distCoeffs, model) objectPointsProj = cl.inverse(imagePoints, rV, tV, cameraMatrix, distCoeffs, model) xc, yc, zc = cl.rotoTrasRodri(objectPoints, rV, tV).T xp, yp = cl.ccd2homUndistorted(imagePoints, cameraMatrix, distCoeffs, model) xp2, yp2 = cl.rotoTrasHomog(objectPoints, rV, tV).T ll = np.linalg.norm(objectPoints, axis=0) r = np.mean(ll) / 7 plt.figure() plt.title('image Points') plt.imshow(img) plt.scatter(imagePoints[:, 0], imagePoints[:, 1], marker='+', label='calibration') plt.scatter(imagePointsProj[:, 0], imagePointsProj[:, 1], marker='x', label='projected') plt.legend() #plt.figure() #plt.title('homogenous Points') #plt.scatter(xp, yp, marker='+', label='undistorted from image') #plt.scatter(xp2, yp2, marker='x', label='rptotraslated from map') #plt.legend() plt.figure() plt.title('object Points') plt.scatter(objectPoints[:, 0], objectPoints[:, 1], marker='+', label='calibration') plt.scatter(objectPointsProj[:, 0], objectPointsProj[:, 1], marker='x', label='projected') plt.legend() #fig = plt.figure() #plt.title('3D object Points, camera ref frame') #ax = fig.gca(projection='3d') #ax.axis('equal') #ax.scatter(xc, yc, zc) #ax.plot([0, r], [0, 0], [0, 0], "-r") #ax.plot([0, 0], [0, r], [0, 0], "-b") #ax.plot([0, 0], [0, 0], [0, r], "-k") return imagePointsProj, objectPointsProj
def projectionPlots(rV, tV, data): ''' compara los puntos de calibracio y sus proyecciones en las tres etapas ''' imagePoints, objectPoints, cameraMatrix, model = data imagePointsProj = cl.direct(objectPoints, rV, tV, cameraMatrix, distCoeffs, model) objectPointsProj = cl.inverse(imagePoints, rV, tV, cameraMatrix, distCoeffs, model) xc, yc, zc = cl.rotoTrasRodri(objectPoints,rV,tV).T xp, yp = cl.ccd2homUndistorted(imagePoints, cameraMatrix, distCoeffs, model) xp2, yp2 = cl.rotoTrasHomog(objectPoints, rV, tV).T ll = np.linalg.norm(objectPoints, axis=0) r = np.mean(ll) / 7 plt.figure() plt.title('image Points') plt.imshow(img) plt.scatter(imagePoints[:,0], imagePoints[:,1], marker='+', label='calibration') plt.scatter(imagePointsProj[:,0], imagePointsProj[:,1], marker='x', label='projected') plt.legend() #plt.figure() #plt.title('homogenous Points') #plt.scatter(xp, yp, marker='+', label='undistorted from image') #plt.scatter(xp2, yp2, marker='x', label='rptotraslated from map') #plt.legend() plt.figure() plt.title('object Points') plt.scatter(objectPoints[:,0], objectPoints[:,1], marker='+', label='calibration') plt.scatter(objectPointsProj[:,0], objectPointsProj[:,1], marker='x', label='projected') plt.legend() #fig = plt.figure() #plt.title('3D object Points, camera ref frame') #ax = fig.gca(projection='3d') #ax.axis('equal') #ax.scatter(xc, yc, zc) #ax.plot([0, r], [0, 0], [0, 0], "-r") #ax.plot([0, 0], [0, r], [0, 0], "-b") #ax.plot([0, 0], [0, 0], [0, r], "-k") return imagePointsProj, objectPointsProj
SyntChesRvecs = np.load(rVecsFileOCV) SyntChesTvecs = np.load(tVecsFileOCV) RealChes = realches(ChesnIm, ChesnPt, ChesObjPt, RealChesImgPt, RealChesImgFiles) # ahora hago la proyección hacia la imagen from calibration import calibrator as cl cameraMatrix = np.eye(3) cameraMatrix[[0, 1], 2] = uv distCoeff = np.array([k]) SyntChesImPts = np.array([ cl.direct(ChesObjPt, SyntChesRvecs[i], SyntChesTvecs[i], cameraMatrix, distCoeff, model) for i in range(ChesnIm) ]) SyntChesImPtsNoise = np.random.randn(np.prod(SyntChesImPts.shape)).reshape( SyntChesImPts.shape) SyntChes = syntches(ChesnIm, ChesnPt, SyntChesRvecs, SyntChesTvecs, ChesObjPt, SyntChesImPts, SyntChesImPtsNoise) plt.figure() plt.title('corners de todas las imagenes') for crnr in SyntChes.imgPt: plt.scatter(crnr[:, 0], crnr[:, 1], marker='x', s=5) plt.axis('equal') # %% =========================================================================
m = chessboardModel.shape[1] # cantidad de puntos # load model specific data distCoeffs = np.load(distCoeffsFile) cameraMatrix = np.load(linearCoeffsFile) rVecs = np.load(rVecsFile)[imageSelection] tVecs = np.load(tVecsFile)[imageSelection] # project from chessboard model to image asuming rvec, tvecs as True corners = list() for i in imageSelection: corners.append( cl.direct(objpoints[i], rVecs[i], tVecs[i], cameraMatrix, distCoeffs, model, ocv=False)) corners = np.array(corners) # standar deviation from subpixel epsilon used # sumo ruid de std std = 1.0 corners += np.random.randn(np.prod(corners.shape)).reshape(corners.shape) * std ''' ahora que tengo los datos sinteticos deberia sacar condiciones iniciales con opencv. esa deberia ser la idea. el tema es que ocv no tiene el modelo este. algo como lo que hago en lo extrinseco, para cada modelo genero rvec, tvecs '''
RPP[model] = [] K[model] = cameraMatrix D[model] = distCoeffs RV[model] = rVecs TV[model] = tVecs IP[model] = [] OP[model] = [] # MAP TO HOMOGENOUS PLANE TO GET RADIUS for j in range(n): print('\t imagen', j) rvec = rVecs[j] tvec = tVecs[j] imagePointsProjected = cl.direct(chessboardModel, rvec, tvec, cameraMatrix, distCoeffs, model) imagePointsProjected = imagePointsProjected.reshape((-1, 2)) IP[model].append(imagePointsProjected) objectPointsProjected = cl.inverse(imagePoints[j, 0], rvec, tvec, cameraMatrix, distCoeffs, model) #objectPointsProjected = opbjectPointsProjected.reshape((-1,3)) OP[model].append(objectPointsProjected) if plotCorners: imagePntsX = imagePoints[j, 0, :, 0] imagePntsY = imagePoints[j, 0, :, 1] xPos = imagePointsProjected[:, 0] yPos = imagePointsProjected[:, 1]
cl.fiducialComparison3D(rVec, tVec, fiducial1) # # %% TEST DIRECT MAPPING (DISTORTION MODEL) # pruebo con la imagen j-esima if plotCornersDirect: for j in range(n): # range(len(imgpoints)): imagePntsX = imagePoints[j, 0, :, 0] imagePntsY = imagePoints[j, 0, :, 1] rvec = rVecs[j] tvec = tVecs[j] imagePointsProjected = cl.direct(chessboardModel, rvec, tvec, K, D, model) imagePointsProjected = imagePointsProjected.reshape((-1, 2)) xPos = imagePointsProjected[:, 0] yPos = imagePointsProjected[:, 1] plt.figure() im = plt.imread(images[j]) plt.imshow(im) plt.plot(imagePntsX, imagePntsY, 'xr', markersize=10) plt.plot(xPos, yPos, '+b', markersize=10) #fig.savefig("distortedPoints3.png") # # %% TEST INVERSE MAPPING (DISTORTION MODEL) # pruebo con la imagen j-esima
# cameraMatrixOCV = np.load(linearCoeffsFileOCV) SyntChesRvecs = np.load(rVecsFileOCV) SyntChesTvecs = np.load(tVecsFileOCV) RealChes = realches(ChesnIm, ChesnPt, ChesObjPt, RealChesImgPt, RealChesImgFiles) # ahora hago la proyección hacia la imagen from calibration import calibrator as cl cameraMatrix = np.eye(3) cameraMatrix[[0,1],2] = uv distCoeff = np.array([k]) SyntChesImPts = np.array([cl.direct(ChesObjPt, SyntChesRvecs[i], SyntChesTvecs[i], cameraMatrix, distCoeff, model) for i in range(ChesnIm)]) SyntChesImPtsNoise = np.random.randn(np.prod(SyntChesImPts.shape) ).reshape(SyntChesImPts.shape) SyntChes = syntches(ChesnIm, ChesnPt, SyntChesRvecs, SyntChesTvecs, ChesObjPt, SyntChesImPts, SyntChesImPtsNoise) plt.figure() plt.title('corners de todas las imagenes') for crnr in SyntChes.imgPt: plt.scatter(crnr[:, 0], crnr[:, 1], marker='x', s=5) plt.axis('equal')
-0.1]))[0] # np.array([[1,0,0],[0,-1,0],[0,0,-1]],dtype=float) rVecIni = cv2.Rodrigues(Rini)[0] # paramteros a encontrar m = np.array([ Rini[0, 0], Rini[1, 0], Rini[2, 0], Rini[0, 1], Rini[1, 1], Rini[2, 1], tVecIni[0], tVecIni[1], tVecIni[2] ]) # %% genero puntos en el plano y los proyecto a la imagen gridAxis = np.linspace(-100, 100, 40) gridX, gridY = np.meshgrid(gridAxis, gridAxis) gridZ = np.zeros_like(gridX) objectPoints = np.array([gridX, gridY, gridZ]).reshape(3, -1).T # proyecto a la imagen imagePoints = cl.direct(objectPoints, rVecIni, tVecIni, cameraMatrix, distCoeffs, model) imagePoints.shape = (-1, 2) plt.subplot(121) plt.scatter(objectPoints[:, 0], objectPoints[:, 1]) plt.subplot(122) plt.scatter(imagePoints[:, 0], imagePoints[:, 1]) # %% saco lo puntos que estan demasiado cerca del horizonte # desde el marco de referencia de la camara: objectPointsC = cl.rotoTrasRodri(objectPoints, rVecIni, tVecIni).T l = np.linalg.norm(objectPointsC, axis=0) s = np.mean(l) / 4 # tomo ~15 grados de margen fi = np.arccos(objectPointsC[2] / l)
# Parametros de entrada/salida de la calibracion objpoints = np.array([chessboardModel]*n) m = chessboardModel.shape[1] # cantidad de puntos # load model specific data distCoeffs = np.load(distCoeffsFile) cameraMatrix = np.load(linearCoeffsFile) rVecs = np.load(rVecsFile)[imageSelection] tVecs = np.load(tVecsFile)[imageSelection] # project from chessboard model to image asuming rvec, tvecs as True corners = list() for i in imageSelection: corners.append(cl.direct(objpoints[i], rVecs[i], tVecs[i], cameraMatrix, distCoeffs, model, ocv=False)) corners = np.array(corners) # standar deviation from subpixel epsilon used # sumo ruid de std std = 1.0 corners += np.random.randn(np.prod(corners.shape)).reshape(corners.shape) * std ''' ahora que tengo los datos sinteticos deberia sacar condiciones iniciales con opencv. esa deberia ser la idea. el tema es que ocv no tiene el modelo este. algo como lo que hago en lo extrinseco, para cada modelo genero rvec, tvecs ''' # pongo en forma flat los valores iniciales
RPP[model] = [] K[model] = cameraMatrix D[model] = distCoeffs RV[model] = rVecs TV[model]= tVecs IP[model] = [] OP[model] = [] # MAP TO HOMOGENOUS PLANE TO GET RADIUS for j in range(n): print('\t imagen', j) rvec = rVecs[j] tvec = tVecs[j] imagePointsProjected = cl.direct(chessboardModel, rvec, tvec, cameraMatrix, distCoeffs, model) imagePointsProjected = imagePointsProjected.reshape((-1,2)) IP[model].append(imagePointsProjected) objectPointsProjected = cl.inverse(imagePoints[j,0], rvec, tvec, cameraMatrix, distCoeffs, model) #objectPointsProjected = opbjectPointsProjected.reshape((-1,3)) OP[model].append(objectPointsProjected) if plotCorners: imagePntsX = imagePoints[j, 0, :, 0] imagePntsY = imagePoints[j, 0, :, 1] xPos = imagePointsProjected[:, 0] yPos = imagePointsProjected[:, 1]
tVecIni = np.array([0, -1, 15.0]) Rini = cv2.Rodrigues(np.array([np.pi*0.9, 0.1, -0.1]))[0]# np.array([[1,0,0],[0,-1,0],[0,0,-1]],dtype=float) rVecIni = cv2.Rodrigues(Rini)[0] # paramteros a encontrar m = np.array([Rini[0,0], Rini[1,0], Rini[2,0], Rini[0,1], Rini[1,1], Rini[2,1], tVecIni[0], tVecIni[1], tVecIni[2]]) # %% genero puntos en el plano y los proyecto a la imagen gridAxis = np.linspace(-100,100,40) gridX, gridY = np.meshgrid(gridAxis, gridAxis) gridZ = np.zeros_like(gridX) objectPoints = np.array([gridX, gridY, gridZ]).reshape(3,-1).T # proyecto a la imagen imagePoints = cl.direct(objectPoints, rVecIni, tVecIni, cameraMatrix, distCoeffs, model) imagePoints.shape = (-1,2) plt.subplot(121) plt.scatter(objectPoints[:,0], objectPoints[:,1]) plt.subplot(122) plt.scatter(imagePoints[:,0], imagePoints[:,1]) # %% saco lo puntos que estan demasiado cerca del horizonte # desde el marco de referencia de la camara: objectPointsC = cl.rotoTrasRodri(objectPoints,rVecIni,tVecIni).T l = np.linalg.norm(objectPointsC, axis=0) s = np.mean(l)/4 # tomo ~15 grados de margen fi = np.arccos(objectPointsC[2] / l)