双目测距python_python+openCV实现双目视差图及测距

importcv2importnumpy as npimportstereoconfigdefgetRectifyTransform(height, width, config):#读取矩阵参数

left_K =config.cam_matrix_left

right_K=config.cam_matrix_right

left_distortion=config.distortion_l

right_distortion=config.distortion_r

R=config.R

T=config.T#计算校正变换

if type(height) != "int" or type(width) != "int":

height=int(height)

width=int(width)

R1, R2, P1, P2, Q, roi1, roi2=cv2.stereoRectify(left_K, left_distortion, right_K, right_distortion,

(width, height), R, T, alpha=0)

map1x, map1y=cv2.initUndistortRectifyMap(left_K, left_distortion, R1, P1, (width, height), cv2.CV_32FC1)

map2x, map2y=cv2.initUndistortRectifyMap(right_K, right_distortion, R2, P2, (width, height), cv2.CV_32FC1)returnmap1x, map1y, map2x, map2y, Q#畸变校正和立体校正

defrectifyImage(image1, image2, map1x, map1y, map2x, map2y):

rectifyed_img1=cv2.remap(image1, map1x, map1y, cv2.INTER_AREA)

rectifyed_img2=cv2.remap(image2, map2x, map2y, cv2.INTER_AREA)returnrectifyed_img1, rectifyed_img2#视差计算

defsgbm(imgL, imgR):#SGBM参数设置

blockSize = 8img_channels= 3stereo= cv2.StereoSGBM_create(minDisparity = 1,

numDisparities= 64,

blockSize=blockSize,

P1= 8 * img_channels * blockSize *blockSize,

P2= 32 * img_channels * blockSize *blockSize,

disp12MaxDiff= -1,

preFilterCap= 1,

uniquenessRatio= 10,

speckleWindowSize= 100,

speckleRange= 100,

mode=cv2.STEREO_SGBM_MODE_HH)#计算视差图

disp =stereo.compute(imgL, imgR)

disp= np.divide(disp.astype(np.float32), 16.)#除以16得到真实视差图

returndisp#计算三维坐标,并删除错误点

defthreeD(disp, Q):#计算像素点的3D坐标(左相机坐标系下)

points_3d =cv2.reprojectImageTo3D(disp, Q)

points_3d= points_3d.reshape(points_3d.shape[0] * points_3d.shape[1], 3)

X=points_3d[:, 0]

Y= points_3d[:, 1]

Z= points_3d[:, 2]#选择并删除错误的点

remove_idx1 = np.where(Z <=0)

remove_idx2= np.where(Z > 15000)

remove_idx3= np.where(X > 10000)

remove_idx4= np.where(X < -10000)

remove_idx5= np.where(Y > 10000)

remove_idx6= np.where(Y < -10000)

remove_idx=np.hstack(

(remove_idx1[0], remove_idx2[0], remove_idx3[0], remove_idx4[0], remove_idx5[0], remove_idx6[0]))

points_3d=np.delete(points_3d, remove_idx, 0)#计算目标点(这里我选择的是目标区域的中位数,可根据实际情况选取)

ifpoints_3d.any():

x=np.median(points_3d[:, 0])

y= np.median(points_3d[:, 1])

z= np.median(points_3d[:, 2])

targetPoint=[x, y, z]else:

targetPoint= [0, 0, -1]#无法识别目标区域

returntargetPoint

imgL= cv2.imread("_left.jpg")

imgR= cv2.imread("_right.jpg")

height, width= imgL.shape[0:2]#读取相机内参和外参

config =stereoconfig.stereoCameral()

map1x, map1y, map2x, map2y, Q=getRectifyTransform(height, width, config)

iml_rectified, imr_rectified=rectifyImage(imgL, imgR, map1x, map1y, map2x, map2y)

disp=sgbm(iml_rectified, imr_rectified)

cv2.imshow("disp", disp)

target_point= threeD(disp, Q)#计算目标点的3D坐标(左相机坐标系下)

print(target_point)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
各标定步骤实现方法 1 计算标靶平面与像平面之间的映射矩阵 计算标靶平面与像平面之间的映射矩阵,计算映射矩阵时不考虑摄像机的成像模型,只是根据平面标靶坐标点和对应的像坐标点的数据,利用最小二乘方法计算得到[ [ix] ] .2 求解摄像机参数矩阵 由计算得到的标靶平面和像平面的映射矩阵得到与摄像机内部参数相关的基本方程关系,求解方程得到摄像机内部参数,考虑镜头的畸变模型,将上述解方程获 得的内部参数作为初值,进行非线性优化搜索,从而计算出所有参数的准确值 [[x] ] .3 求解左右两摄像机之间的相对位置关系 设双目视觉系统左右摄像机的外部参数分别为Rl, Tl,与Rr, Tr,,即Rl, Tl表示左摄像机与世界坐标系的相对位置,Rr, Tr表示右摄像机与世界坐标系的相对位置 [[xi] ]。因此,对于空间任意一点,如果在世界坐标系、左摄像机坐标系和右摄像机坐标系中的坐标分别为Xw,, Xl , Xr,则有:Xl=RlXw+Tl ;Xr=RrXw+Tr .因此,两台摄像机之间的相对几何关系可以由下式表示R=RrRl-1 ;T=Tr- RrRl-1Tl 在实际标定过程中,由标定靶对两台摄像机同时进行摄像标定,以分别获得两台摄像机的内、外参数,从而不仅可以标定出摄像机的内部参数,还可以同时标定出双目视觉系统的结构参数 [xii] 。由单摄像机标定过程可以知道,标定靶每变换一个位置就可以得到一组摄像机外参数:Rr,Tr,与Rl, Tl,因此,由公式R=RrRl-1 ;T=Tr- RrRl-1Tl,可以得到一组结构参数R和T

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值