 # 双目相机标定及三角测量

• 29440Rm5lN
了解作者
• Python
开发工具
• 4.6KB
文件大小
• rar
文件格式
• 0
收藏次数
• 5 积分
下载积分
• 0
下载次数
• 2022-05-26 16:47
上传日期 • 三角测距.py
8.1KB
• 单目标定.py
2.3KB
• 双目标定.py
2.7KB

import cv2 as cv import glob import numpy as np import matplotlib.pyplot as plt def calibrate_camera(images_folder): images_names = glob.glob(images_folder) images = [] for imname in images_names: im = cv.imread(imname, 1) images.append(im) # plt.figure(figsize = (10,10)) # ax = [plt.subplot(2,2,i+1) for i in range(4)] # # for a, frame in zip(ax, images): # a.imshow(frame[:,:,[2,1,0]]) # a.set_xticklabels([]) # a.set_yticklabels([]) # plt.subplots_adjust(wspace=0, hspace=0) # plt.show() #criteria used by checkerboard pattern detector. #Change this if the code can't find the checkerboard criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001) rows = 5 #number of checkerboard rows. columns = 8 #number of checkerboard columns. world_scaling = 1. #change this to the real world square size. Or not. #coordinates of squares in the checkerboard world space objp = np.zeros((rows*columns,3), np.float32) objp[:,:2] = np.mgrid[0:rows,0:columns].T.reshape(-1,2) objp = world_scaling* objp #frame dimensions. Frames should be the same size. width = images.shape height = images.shape #Pixel coordinates of checkerboards imgpoints = [] # 2d points in image plane. #coordinates of the checkerboard in checkerboard world space. objpoints = [] # 3d point in real world space for frame in images: gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) #find the checkerboard ret, corners = cv.findChessboardCorners(gray, (rows, columns), None) if ret == True: #Convolution size used to improve corner detection. Don't make this too large. conv_size = (11, 11) #opencv can attempt to improve the checkerboard coordinates corners = cv.cornerSubPix(gray, corners, conv_size, (-1, -1), criteria) cv.drawChessboardCorners(frame, (rows,columns), corners, ret) cv.imshow('img', frame) cv.waitKey(500) objpoints.append(objp) imgpoints.append(corners) ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, (width, height), None, None) print('rmse:', ret) print('camera matrix:\n', mtx) print('distortion coeffs:', dist) print('Rs:\n', rvecs) print('Ts:\n', tvecs) return mtx, dist def stereo_calibrate(mtx1, dist1, mtx2, dist2, frames_folder): #read the synched frames images_names = glob.glob(frames_folder) images_names = sorted(images_names) c1_images_names = images_names[:len(images_names)//2] c2_images_names = images_names[len(images_names)//2:] c1_images = [] c2_images = [] for im1, im2 in zip(c1_images_names, c2_images_names): _im = cv.imread(im1, 1) c1_images.append(_im) _im = cv.imread(im2, 1) c2_images.append(_im) #change this if stereo calibration not good. criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.0001) rows = 5 #number of checkerboard rows. columns = 8 #number of checkerboard columns. world_scaling = 1. #change this to the real world square size. Or not. #coordinates of squares in the checkerboard world space objp = np.zeros((rows*columns,3), np.float32) objp[:,:2] = np.mgrid[0:rows,0:columns].T.reshape(-1,2) objp = world_scaling* objp #frame dimensions. Frames should be the same size. width = c1_images.shape height = c1_images.shape #Pixel coordinates of checkerboards imgpoints_left = [] # 2d points in image plane. imgpoints_right = [] #coordinates of the checkerboard in checkerboard world space. objpoints = [] # 3d point in real world space for frame1, frame2 in zip(c1_images, c2_images): gray1 = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY) gray2 = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY) c_ret1, corners1 = cv.findChessboardCorners(gray1, (5, 8), None) c_ret2, corners2 = cv.findChessboardCorners(gray2, (5, 8), None) if c_ret1 == True and c_ret2 == True: corners1 = cv.cornerSubPix(gray1, corners1, (11, 11), (-1, -1), criteria) corners2 = cv.cornerSubPix(gray2, corners2, (11, 11), (-1, -1), criteria) cv.drawChessboardCorners(frame1, (5,8), corners1, c_ret1) cv.imshow('img', frame1) cv.drawChessboardCorners(frame2, (5,8), corners2, c_ret2) cv.imshow('img2', frame2) cv.waitKey(500) objpoints.append(objp) imgpoints_left.append(corners1) imgpoints_right.append(corners2) stereocalibration_flags = cv.CALIB_FIX_INTRINSIC ret, CM1, dist1, CM2, dist2, R, T, E, F = cv.stereoCalibrate(objpoints, imgpoints_left, imgpoints_right, mtx1, dist1, mtx2, dist2, (width, height), criteria = criteria, flags = stereocalibration_flags) print(ret) return R, T def triangulate(mtx1, mtx2, R, T): uvs1 = [[458, 86], [451, 164], [287, 181], [196, 383], [297, 444], [564, 194], [562, 375], [596, 520], [329, 620], [488, 622], [432, 52], [489, 56]] uvs2 = [[540, 311], [603, 359], [542, 378], [525, 507], [485, 542], [691, 352], [752, 488], [711, 605], [549, 651], [651, 663], [526, 293], [542, 290]] uvs1 = np.array(uvs1) uvs2 = np.array(uvs2) frame1 = cv.imread('testing/_C1.png') frame2 = cv.imread('testing/_C2.png') plt.imshow(frame1[:,:,[2,1,0]]) plt.scatter(uvs1[:,0], uvs1[:,1]) plt.show() #this call will cause a crash if you use cv.imshow() above. Comment out cv.imshow() to see this. plt.imshow(frame2[:,:,[2,1,0]]) plt.scatter(uvs2[:,0], uvs2[:,1]) plt.show()#this call will cause a crash if you use cv.imshow() above. Comment out cv.imshow() to see this #RT matrix for C1 is identity. RT1 = np.concatenate([np.eye(3), [,,]], axis = -1) P1 = mtx1 @ RT1 #projection matrix for C1 #RT matrix for C2 is the R and T obtained from stereo calibration. RT2 = np.concatenate([R, T], axis = -1) P2 = mtx2 @ RT2 #projection matrix for C2 def DLT(P1, P2, point1, point2): A = [point1*P1[2,:] - P1[1,:], P1[0,:] - point1*P1[2,:], point2*P2[2,:] - P2[1,:], P2[0,:] - point2*P2[2,:] ] A = np.array(A).reshape((4,4)) #print('A: ') #print(A) B = A.transpose() @ A from scipy import linalg U, s, Vh = linalg.svd(B, full_matrices = False) print('Triangulated point: ') print(Vh[3,0:3]/Vh[3,3]) return Vh[3,0:3]/Vh[3,3] p3ds = [] for uv1, uv2 in zip(uvs1, uvs2): _p3d = DLT(P1, P2, uv1, uv2) p3ds.append(_p3d) p3ds = np.array(p3ds) from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_xlim3d(-15, 5) ax.set_ylim3d(-10, 10) ax.set_zlim3d(10, 30) connections = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,8], [1,9], [2,8], [5,9], [8,9], [0, 10], [0, 11]] for _c in connections: print(p3ds[_c]) print(p3ds[_c]) ax.plot(xs = [p3ds[_c,0], p3ds[_c,0]], ys = [p3ds[_c,1], p3ds[_c,1]], zs = [p3ds[_c,2], p3ds[_c,2]], c = 'red') ax.set_title('This figure can be rotated.') #uncomment to see the triangulated pose. This may cause a crash if youre also using cv.imshow() above. plt.show() mtx1, dist1 = calibrate_camera(images_fo   相关推荐
• 视觉采集（双目）.rar
本程序为LabVIEW双目采集程序，打开左右两个摄像头采集图像后自动合为一个图像，并对图像进行图像灰度化，图像灰度均值化，图像中值滤波一系列操作，并可以保存图像为jpeg图像
• 计算机视觉双目视觉综述.rar
一篇关于计算机视觉双目视觉的综述文章，对于初学者非常有引导作用。
• 武汉大学摄影测量 图像理解与计算机视觉 机器人双目视觉定位技术研究
武汉大学摄影测量 图像理解与计算机视觉 机器人双目视觉定位技术研究
• 双目视觉所有代码
1.matlab标定 2. opencv立体校正 3.BM、SGBM、GC算法进行匹配 4.最后用公式法还原三维图像。 收纳了博客上很多资料完成的毕设，给需要的人
• 双目视觉相机标定
本资源对双目视觉相机进行标定，获取目标的世界坐标和图像坐标系，求解M矩阵，得出相机内外参数
• 计算机双目立体视觉_)高宏伟
双目立体视觉技术模拟人类双眼处理景物的方式，运用双目摄像头从不同角度同时获取目标物的左右两幅数字图像，综合应用相机标定、特征点检测、立体匹配、三维重建等关键技术还原出物体的三维几何信息。 以双目立体...
• MATLAB双目立体视觉实验