[opencv] 极线校正
//R = Rr*inv(Rl); T = Tr - R*Tl; invert(RLeft,RLeft); Mat R = RRight.clone(); gemm(R,RLeft,1,1,0,R); Mat T; gemm(R,TLeft,1,1,0,T); T = TRight - T; //计算旋转矩阵和投影矩阵 Mat R1, R2, P1, P2, Q; stereoRectify(KLeft,Mat::zeros(1,5,CV_32F),KRight,Mat::zeros(1,5,CV_32F), imageSize,R,T,R1,R2,P1,P2,Q,CALIB_ZERO_DISPARITY,0); //计算映射 Mat rmap[2][2]; initUndistortRectifyMap(KLeft,Mat::zeros(1,5,CV_32F), R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]); initUndistortRectifyMap(KRight, Mat::zeros(1,5,CV_32F), R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]); remap(imgL, imgLr, rmap[0][0], rmap[0][1], CV_INTER_AREA);//左校正 remap(imgR, imgRr, rmap[1][0], rmap[1][1], CV_INTER_AREA);//右校正 Mat showImage(imageSize.height,2*imageSize.width,CV_8UC3); Rect rectLeft(0,0,imageSize.width,imageSize.height); Rect rectRight(imageSize.width,0,imageSize.width,imageSize.height); imgLr.copyTo(showImage(rectLeft)); imgRr.copyTo(showImage(rectRight)); for( int i = 0; i < imageSize.height; i += 32 ) { line(showImage, Point(0, i), Point(2*imageSize.width, i), Scalar(0, 255, 0), 1, 8); }
效果图:
代码下载:https://github.com/zhanxiage1994/rectifyImages.git