首页 > 编程知识 正文

opencv打开双目摄像头,opencv立体匹配

时间:2023-05-06 08:31:02 阅读:163864 作者:710

代码和数据集来自“氛围鲜艳的公众号”

链接: https://pan.Baidu.com/s/1 byrj 57 medhwwp 45 xxvbdpa提取代码: b3m6

双目摄像机校准的过程:

1 .调节两个摄像头的角度,在标定板上拍10组左右的照片。

2 )通过悲伤魔镜的思想得到两个摄像机的内外参矩阵和畸变矩阵,为两个摄像机的畸变矫正做准备。

3、在现实中,不可能完全控制两个摄像机完全平行地拍摄一个物体,所以只能通过矫正建立平行的条件,根据两个摄像机的几个参数计算深度z。

4 )通过计算照片中物体各点距离摄像机镜头的深度,构建该物体的三维立体图。

总之,双目照相机可以拍摄一个物体的更多细节,然后,从该细节在照片中的不同角度的位置,获得该细节的空间坐标信息,最终恢复照片中物体在三维空间中的图像。

缺点:双目摄像机只能针对用两个镜头拍摄的物体细节提取空间信息。 也就是说,对于由两个照相机拍摄到的物体的细微部分不交叉的部分,产生3D空间复原误差。

思考:如果是更多维的摄像头,还原的效果不是更好吗?

现在知道的只是,多功能摄像机可以更快地恢复速度。

双目摄像机的应用:

1 )视差距离测量

2,3d构建

代码:

以下是双目摄像机的标定代码和校正双目摄像机失真的代码。

下一步:用双目摄像机拍摄真实物体,利用双目参数进行三维立体投影。

# include opencv2(opencv.HPP # include fstream # include iostream # includevectorusingnamespacestd; 使用命名空间cv; voidgetimgspoints (vectormatimgs,vectorvectorPoint2f imgsPoints,Size board_size ) for ) intI=0; i imgs.size (; I ) {Mat img1=imgs[i]; 马特格雷1; cvtcolor(img1,gray1,COLOR_BGR2GRAY ); vectorPoint2f img1_points; findchessboardcorners(gray1,board_size,img1_points ); //计算方格标定板角点find4quadcornersubpix(gray1,img1_points,size (5,5 ) ); //方格标定板转角坐标imgs points.push _ back (img1_ points ); }}int main ()//要读取所有图像的vectorMat imgLs; vectorMat imgRs; 字符串img lname; 字符串名称; ifstreamfinl (sterocalibdatal.txt ); ifstreamfinr (sterocalibdatar.txt ); while(getline(finl,imgLName ) getline (finr,imgRName ) ) mati mgl=im read (img lname ); matimgr=imread(imgrname ); imgls.push_back(imgl; imgRS.push_back(imgr; } size board _ size=size (9,6 ); //方格标定板内角点的数量(行、列) vectorvectorPoint2f imgLsPoints; vectorvectorPoint2f imgRsPoints; getimgspoints(imgls,imgLsPoints,board_size ); getimgspoints(imgRS,imgRsPoints,board_size ); //棋盘格各内角点的空间三维坐标sizesquaresize=size (10,10 ); //棋盘格每个方格的实际尺寸vectorvectorpoint3fobjectpoints; for(intI=0; i imgLsPoints.size (; I ) {vectorPoint3f tempPointSet; for(intj=0; j board_size.height; j ) for(intk=0; k board_size.width; k ) {Point3f realPoint; //假设标定板为世界坐标系的z平面,z=0realpoint.x=j * squaresize.width; real point.y=k * squaresize.height; realPoint.z=0; temppoint set.push _ back (realpoint ); } object points.push _ back (temppoint set ); //图像大小Size imageSize; imageSize.width=imgLs[0].cols; imageSize.height=imgLs[0].rows; Mat Matrix1、dist1、Matrix2、dist2、rvecs、tvecs; calibratecamera(objectpoints,imgLsPoints,imageSize,Matrix1,dist1,rvecs,tvecs,0 ); calibratecamera(objectpoints,imgRsPoints,imageSize,Matrix2,dist2,rvecs,tvecs,0 ); //双目贴近标定的Mat R、t、e、f; stereocalibrate(objectpoints,imgLsPoints,imgRsPoints,Matrix1,dist1,Matrix2,dist2,imageSize,r,t,e,f,cond ) sereo rectify (矩阵1、dist1、矩阵2、dist2、imageSize、r、t、R1、R2、P1、P2、q、0 ); //计算校正映射矩阵Mat map11、map12、map21、map22; initundistortrectifymap (矩阵1、dist1、R1、P1、imageSize、CV_16SC2、map11、map12 ); initundistortrectifymap (矩阵2、dist2、R2、P2、imageSize、CV_16SC2、map21、map22 ); for(intI=0; i imgLs.size (; I ) (/进行校正图Mat img1r、img2r; remap(imgls[I],img1r,map11,map12,INTER_LINEAR ); remap(imgRS[I],img2r,map21,map22,INTER_LINEAR ); //子画面图像Mat result; hconcat(img1r,img2r,result ); //同一内角点的y轴是否与line(result,point )-1,imgLsPoints[i][0].y ),point ) result.cols,imglspoints[I] )一致维客(0; }return 0; }所需的文件:

结果:

数据集:

steroCalibDataL.txt保存用左照相机拍摄的照片

steroCalibDataR.txt保存右摄像机拍摄的照片

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。