雙目標定,匹配的筆記


清華大學黃耀的Stereo-Matching-Introduction ppt

Efficient Large-scale Stereo Matching

opencv 函數

Stereo Calibration and Rectification

雙目攝像頭矯正就是為了

  • 極點在無窮遠處
  • 對應的極線水平對齊

做法是:

旋轉左右相機使得他們看起來是在一個平面上面的,並且使得他們對應的極線是水平對其的,最后進行scale縮放使得水平的畸變最小

大概的公式是這樣的,定義左攝像頭作為世界參考坐標系,\(P_l\)是一個點p在世界坐標系表示,\(P_r\)是該點在右攝像機坐標系中表示,R,T是左右攝像機的坐標變換,那么有

\[P_l = R^TP_r +T \]

\[R_{rect}P_l = R_{rect}R^TP_r +R_{rect}T \]

上面對他們同時乘以一個\(R_{rect}\),要是\(R_{rect}T = \{|T| \quad 0 \quad 0\}\)

那么同一點p在左右攝像機中的表示只是相差一個x值,就是在同一水平線上,那么\(R_{rect}\)怎么構造呢

大概就是以攝像機的中心連線作為\(e_1\)\(e_1\)叉乘一個相機的光心軸的方向作為\(e_2\)\(e_3\) 就是\(e_1\)叉乘\(e_2\), \(e_1,e_2,e_3\)就是\(R_{rect}\)

具體可以參考上面的那個Stereo Calibration and Rectification,對opencv stereo代碼的使用還有原理講解非常好。

左右圖像已經矯正,搜索就是在同一行上面進行,一般有local window search, global energy function

  • Local Approach

最簡單的做法是下面這種

for each row, k
	for j = Δ to w
		c min = ∞
		for d = 0 to Δ // check each possible disparity
			c(d) = f ( I 1 (j,k), I 2 (j-d,k) )
			if c(d) < c min then
				d best = d
				c min = c(d)
		disp( j,k ) = d best // Save best d value

就是利用局部像素的信息,有滑動窗口,NCC 什么的,但是對於邊緣的地方效果不是很好,還有就是窗口的大小對效果很有關系

  • Global Approach

就是加了一項平滑的量

立體匹配具體的可以參考上面的黃耀的Stereo-Matching-Introduction

評價算法好壞的可以從以下幾個方面:

對邊界處深度的估計,無紋理地方的估計,漸變面的深度估計,遮掩地方的估計,還有需要計算的時間,內存

雙目標定的代碼

是別人的代碼,具體的來源我忘了(對不住了兄弟,知道的可以跟我說一下,我給個鏈接)

// stereoCalibration.cpp : 定義控制台應用程序的入口點。
//
//在進行雙目攝像頭的標定之前,最好事先分別對兩個攝像頭進行單目視覺的標定 
//分別確定兩個攝像頭的內參矩陣,然后再開始進行雙目攝像頭的標定
//在此例程中是先對兩個攝像頭進行單獨標定(見上一篇單目標定文章),然后在進行立體標定

#include "stdio.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "cv.h"
#include <cv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

const int imageWidth = 672;								//攝像頭的分辨率
const int imageHeight = 376;
const int boardWidth = 11;								//橫向的角點數目
const int boardHeight = 8;								//縱向的角點數據
const int boardCorner = boardWidth * boardHeight;		//總的角點數據
const int frameNumber = 40;								//相機標定時需要采用的圖像幀數
const int squareSize = 20;								//標定板黑白格子的大小 單位mm
const Size boardSize = Size(boardWidth, boardHeight);	//
Size imageSize = Size(imageWidth, imageHeight);

Mat R, T, E, F;											//R 旋轉矢量 T平移矢量 E本征矩陣 F基礎矩陣
vector<Mat> rvecs;									    //旋轉向量
vector<Mat> tvecs;										//平移向量
vector<vector<Point2f>> imagePointL;				    //左邊攝像機所有照片角點的坐標集合
vector<vector<Point2f>> imagePointR;					//右邊攝像機所有照片角點的坐標集合
vector<vector<Point3f>> objRealPoint;					//各副圖像的角點的實際物理坐標集合


vector<Point2f> cornerL;								//左邊攝像機某一照片角點坐標集合
vector<Point2f> cornerR;								//右邊攝像機某一照片角點坐標集合

Mat rgbImageL, grayImageL;
Mat rgbImageR, grayImageR;

Mat Rl, Rr, Pl, Pr, Q;									//校正旋轉矩陣R,投影矩陣P 重投影矩陣Q (下面有具體的含義解釋)	
Mat mapLx, mapLy, mapRx, mapRy;							//映射表
Rect validROIL, validROIR;								//圖像校正之后,會對圖像進行裁剪,這里的validROI就是指裁剪之后的區域

/*
事先標定好的左相機的內參矩陣
fx 0 cx
0 fy cy
0 0  1
*/
Mat cameraMatrixL = (Mat_<double>(3, 3) << 350.630987, 0.000000, 338.489358,
0.000000, 350.460123 ,186.370994,
										  0,  0,       1);
Mat distCoeffL = (Mat_<double>(5, 1) << -0.167658 ,0.022202 ,-0.000261 ,-0.000113, 0.000000
);
/*
事先標定好的右相機的內參矩陣
fx 0 cx
0 fy cy
0 0  1
*/
Mat cameraMatrixR = (Mat_<double>(3, 3) << 350.032707, 0.000000 ,352.606748,
0.000000 ,349.998921 ,189.797070,
											0,		0,		 1);
Mat distCoeffR = (Mat_<double>(5, 1) << -0.167346, 0.023118 ,-0.000105, 0.000318, 0.000000);


/*計算標定板上模塊的實際物理坐標*/
void calRealPoint(vector<vector<Point3f>>& obj, int boardwidth, int boardheight, int imgNumber, int squaresize)
{
	//	Mat imgpoint(boardheight, boardwidth, CV_32FC3,Scalar(0,0,0));
	vector<Point3f> imgpoint;
	for (int rowIndex = 0; rowIndex < boardheight; rowIndex++)
	{
		for (int colIndex = 0; colIndex < boardwidth; colIndex++)
		{
			//	imgpoint.at<Vec3f>(rowIndex, colIndex) = Vec3f(rowIndex * squaresize, colIndex*squaresize, 0);
			imgpoint.push_back(Point3f(rowIndex * squaresize, colIndex * squaresize, 0));
		}
	}
	for (int imgIndex = 0; imgIndex < imgNumber; imgIndex++)
	{
		obj.push_back(imgpoint);
	}
}

void outputCameraParam(void)
{
	/*保存數據*/
	/*輸出數據*/
	FileStorage fs("intrinsics.yml", FileStorage::WRITE);
	if (fs.isOpened())
	{
		fs << "cameraMatrixL" << cameraMatrixL << "cameraDistcoeffL" << distCoeffL <<"cameraMatrixR" << cameraMatrixR << "cameraDistcoeffR" << distCoeffR;
		fs.release();
		cout << "cameraMatrixL=:" << cameraMatrixL <<endl<< "cameraDistcoeffL=:" << distCoeffL <<endl<<"cameraMatrixR=:" << cameraMatrixR <<endl<< "cameraDistcoeffR=:" << distCoeffR<<endl;
	}
	else
	{
		cout << "Error: can not save the intrinsics!!!!!" << endl;
	}

	fs.open("extrinsics.yml", FileStorage::WRITE);
	if (fs.isOpened())
	{
		fs << "R" << R << "T" << T << "Rl" << Rl << "Rr" << Rr << "Pl" << Pl << "Pr" << Pr << "Q" << Q;
		cout << "R=" << R << endl << "T=" << T << endl << "Rl=" << Rl << endl << "Rr=" << Rr << endl << "Pl=" << Pl << endl << "Pr=" << Pr << endl << "Q=" << Q << endl;
		fs.release();
	}
	else
		cout << "Error: can not save the extrinsic parameters\n";
}


int main(int argc, char* argv[])
{
	Mat img;
	int goodFrameCount = 0;
	namedWindow("ImageL");
	namedWindow("ImageR");
	cout << "按Q退出 ..." << endl;
	while (goodFrameCount < frameNumber)
	{
		char filename[100];
		/*讀取左邊的圖像*/
		sprintf(filename, "image/left-%04d.png", goodFrameCount + 1);
		rgbImageL = imread(filename, CV_LOAD_IMAGE_COLOR);
		cvtColor(rgbImageL, grayImageL, CV_BGR2GRAY);

		/*讀取右邊的圖像*/
		sprintf(filename, "image/right-%04d.png", goodFrameCount + 1);
		rgbImageR = imread(filename, CV_LOAD_IMAGE_COLOR);
		cvtColor(rgbImageR, grayImageR, CV_BGR2GRAY);

		bool isFindL, isFindR;

		isFindL = findChessboardCorners(rgbImageL, boardSize, cornerL);
		isFindR = findChessboardCorners(rgbImageR, boardSize, cornerR);
		if (isFindL == true && isFindR == true)	 //如果兩幅圖像都找到了所有的角點 則說明這兩幅圖像是可行的
		{
			/*
			Size(5,5) 搜索窗口的一半大小
			Size(-1,-1) 死區的一半尺寸
			TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1)迭代終止條件
			*/
			cornerSubPix(grayImageL, cornerL, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
			drawChessboardCorners(rgbImageL, boardSize, cornerL, isFindL);
			imshow("chessboardL", rgbImageL);
			imagePointL.push_back(cornerL);


			cornerSubPix(grayImageR, cornerR, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
			drawChessboardCorners(rgbImageR, boardSize, cornerR, isFindR);
			imshow("chessboardR", rgbImageR);
			imagePointR.push_back(cornerR);

			/*
			本來應該判斷這兩幅圖像是不是好的,如果可以匹配的話才可以用來標定
			但是在這個例程當中,用的圖像是系統自帶的圖像,都是可以匹配成功的。
			所以這里就沒有判斷
			*/
			//string filename = "res\\image\\calibration";
			//filename += goodFrameCount + ".jpg";
			//cvSaveImage(filename.c_str(), &IplImage(rgbImage));		//把合格的圖片保存起來
			goodFrameCount++;
			cout << "The image is good" << endl;
		}
		else
		{
			cout << "The image is bad please try again" << endl;
		}

		if (waitKey(10) == 'q')
		{
			break;
		}
	}

	/*
	計算實際的校正點的三維坐標
	根據實際標定格子的大小來設置
	*/
	calRealPoint(objRealPoint, boardWidth, boardHeight, frameNumber, squareSize);
	cout << "cal real successful" << endl;

	/*
		標定攝像頭
		由於左右攝像機分別都經過了單目標定
		所以在此處選擇flag = CALIB_USE_INTRINSIC_GUESS
	*/
	double rms = stereoCalibrate(objRealPoint, imagePointL, imagePointR,
		cameraMatrixL, distCoeffL,
		cameraMatrixR, distCoeffR,
		Size(imageWidth, imageHeight), R, T, E, F,
		CALIB_USE_INTRINSIC_GUESS,
		TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, 1e-5));

	cout << "Stereo Calibration done with RMS error = " << rms << endl;

	/*
	立體校正的時候需要兩幅圖像共面並且行對准 以使得立體匹配更加的可靠
	使得兩幅圖像共面的方法就是把兩個攝像頭的圖像投影到一個公共成像面上,這樣每幅圖像從本圖像平面投影到公共圖像平面都需要一個旋轉矩陣R
	stereoRectify 這個函數計算的就是從圖像平面投影都公共成像平面的旋轉矩陣Rl,Rr。 Rl,Rr即為左右相機平面行對准的校正旋轉矩陣。
	左相機經過Rl旋轉,右相機經過Rr旋轉之后,兩幅圖像就已經共面並且行對准了。
	其中Pl,Pr為兩個相機的投影矩陣,其作用是將3D點的坐標轉換到圖像的2D點的坐標:P*[X Y Z 1]' =[x y w] 
	Q矩陣為重投影矩陣,即矩陣Q可以把2維平面(圖像平面)上的點投影到3維空間的點:Q*[x y d 1] = [X Y Z W]。其中d為左右兩幅圖像的時差
	*/
	stereoRectify(cameraMatrixL, distCoeffL, cameraMatrixR, distCoeffR, imageSize, R, T, Rl, Rr, Pl, Pr, Q,
				  CALIB_ZERO_DISPARITY,-1,imageSize,&validROIL,&validROIR);
	/*
	根據stereoRectify 計算出來的R 和 P 來計算圖像的映射表 mapx,mapy
	mapx,mapy這兩個映射表接下來可以給remap()函數調用,來校正圖像,使得兩幅圖像共面並且行對准
	ininUndistortRectifyMap()的參數newCameraMatrix就是校正后的攝像機矩陣。在openCV里面,校正后的計算機矩陣Mrect是跟投影矩陣P一起返回的。
	所以我們在這里傳入投影矩陣P,此函數可以從投影矩陣P中讀出校正后的攝像機矩陣
	*/
	initUndistortRectifyMap(cameraMatrixL, distCoeffL, Rl, Pr, imageSize, CV_32FC1, mapLx, mapLy);
	initUndistortRectifyMap(cameraMatrixR, distCoeffR, Rr, Pr, imageSize, CV_32FC1, mapRx, mapRy);


	Mat rectifyImageL, rectifyImageR;
	cvtColor(grayImageL, rectifyImageL, CV_GRAY2BGR);
	cvtColor(grayImageR, rectifyImageR, CV_GRAY2BGR);

	imshow("Rectify Before", rectifyImageL);

	/*
	經過remap之后,左右相機的圖像已經共面並且行對准了
	*/
	remap(rectifyImageL, rectifyImageL, mapLx, mapLy, INTER_LINEAR);
	remap(rectifyImageR, rectifyImageR, mapRx, mapRy, INTER_LINEAR);

	imshow("ImageL", rectifyImageL);
	imshow("ImageR", rectifyImageR);

	/*保存並輸出數據*/
	outputCameraParam();

	/*
		把校正結果顯示出來
		把左右兩幅圖像顯示到同一個畫面上
		這里只顯示了最后一副圖像的校正結果。並沒有把所有的圖像都顯示出來
	*/
	Mat canvas;
	double sf;
	int w, h;
	sf = 600. / MAX(imageSize.width, imageSize.height);
	w = cvRound(imageSize.width * sf);
	h = cvRound(imageSize.height * sf);
	canvas.create(h, w * 2, CV_8UC3);

	/*左圖像畫到畫布上*/
	Mat canvasPart = canvas(Rect(w*0, 0, w, h));								//得到畫布的一部分
	resize(rectifyImageL, canvasPart, canvasPart.size(), 0, 0, INTER_AREA);		//把圖像縮放到跟canvasPart一樣大小
	Rect vroiL(cvRound(validROIL.x*sf), cvRound(validROIL.y*sf),				//獲得被截取的區域	
		      cvRound(validROIL.width*sf), cvRound(validROIL.height*sf));
	rectangle(canvasPart, vroiL, Scalar(0, 0, 255), 3, 8);						//畫上一個矩形

	cout << "Painted ImageL" << endl;

	/*右圖像畫到畫布上*/
	canvasPart = canvas(Rect(w, 0, w, h));										//獲得畫布的另一部分
	resize(rectifyImageR, canvasPart, canvasPart.size(), 0, 0, INTER_LINEAR);
	Rect vroiR(cvRound(validROIR.x * sf), cvRound(validROIR.y*sf),			
			  cvRound(validROIR.width * sf), cvRound(validROIR.height * sf));
	rectangle(canvasPart, vroiR, Scalar(0, 255, 0), 3, 8);

	cout << "Painted ImageR" << endl;

	/*畫上對應的線條*/
	for (int i = 0; i < canvas.rows;i+=16)
		line(canvas, Point(0, i), Point(canvas.cols, i), Scalar(0, 255, 0), 1, 8);

	imshow("rectified", canvas);

	cout << "wait key" << endl;
	waitKey(0);
	system("pause");
	return 0;
}

stereoRectify只需要K1,D1,K2,D2,就可以求出R1,P1,R2,P2,Q. 不同的參數alpha會導致的不同的結果,可以把上面的alpha改為-1或者0,試試

stereoCalibrate這個才是矯正得到不同於單目攝像頭矯正得到的K1,D1,K2,D2,還有R,T,E,F。

矯正過程中多次取圖片的用處不是在重復運行上面兩個函數,而是得到多組不同的objectPoints以及對應的imagePoints


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM