OpenCV:基本矩阵和移动相机

标签 opencv image-processing camera-calibration extrinsic-parameters reprojection-error

我有一个校准的(内部参数)相机。我从物体上拍摄立体照片,并在重新投影过程中使用它们来寻找物体的一些 3-D 信息。

为此,我首先计算了基本矩阵。然后,我得到基本矩阵,并从那里得到旋转矩阵和平移向量以及其他需要的信息。

问题是,使用不同的参数,我会得到非常不同的结果。我知道基本矩阵对于相同的立体图像可能不同,但我希望具有相同的旋转矩阵和平移向量。但是,每个不同的参数(例如匹配特征的数量)都会导致不同的矩阵。我错过了什么吗?我是否认为相同的立体图像应该提供(非常)相同的旋转、平移和重新投影矩阵?

这是我的代码。任何帮助,将不胜感激。谢谢!

Mat img_1 = imread( "images/box01-edited.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat img_2 = imread( "images/box02-edited.jpg", CV_LOAD_IMAGE_GRAYSCALE );


if( !img_1.data || !img_2.data )
{ return -1; }

//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 1000;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 );

//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;
extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 );

//-- Step 3: Matching descriptor vectors with a brute force matcher
BFMatcher matcher(NORM_L1, true);
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );

//-- Draw matches
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );
//-- Show detected matches
namedWindow( "Matches", CV_WINDOW_NORMAL );
imshow("Matches", img_matches );
waitKey(0);


//-- Step 4: calculate Fundamental Matrix
vector<Point2f>imgpts1,imgpts2;
for( unsigned int i = 0; i<matches.size(); i++ )
{
// queryIdx is the "left" image
imgpts1.push_back(keypoints_1[matches[i].queryIdx].pt);
// trainIdx is the "right" image
imgpts2.push_back(keypoints_2[matches[i].trainIdx].pt);
}
Mat F =  findFundamentalMat  (imgpts1, imgpts2, FM_RANSAC, 0.1, 0.99);


//-- Step 5: calculate Essential Matrix

double data[] = {1189.46 , 0.0, 805.49, 
                0.0, 1191.78, 597.44,
                0.0, 0.0, 1.0};//Camera Matrix
Mat K(3, 3, CV_64F, data);
Mat_<double> E = K.t() * F * K; //according to HZ (9.12)

//-- Step 6: calculate Rotation Matrix and Translation Vector
Matx34d P;
Matx34d P1;
//decompose E to P' , HZ (9.19)
SVD svd(E,SVD::MODIFY_A);
Mat svd_u = svd.u;
Mat svd_vt = svd.vt;
Mat svd_w = svd.w;
Matx33d W(0,-1,0,1,0,0,0,0,1);//HZ 9.13
Mat_<double> R = svd_u * Mat(W) * svd_vt; //HZ 9.19
Mat_<double> t = svd_u.col(2); //u3

if (!CheckCoherentRotation (R)) {
std::cout<<"resulting rotation is not coherent\n";
P1 = 0;
return 0;
}

P1 = Matx34d(R(0,0),R(0,1),R(0,2),t(0),
             R(1,0),R(1,1),R(1,2),t(1),
             R(2,0),R(2,1),R(2,2),t(2));

//-- Step 7: Reprojection Matrix and rectification data
Mat R1, R2, P1_, P2_, Q;
Rect validRoi[2];
double dist[] = { -0.03432, 0.05332, -0.00347, 0.00106, 0.00000};
Mat D(1, 5, CV_64F, dist);

stereoRectify(K, D, K, D, img_1.size(), R, t, R1, R2, P1_, P2_, Q, CV_CALIB_ZERO_DISPARITY, 1, img_1.size(),  &validRoi[0], &validRoi[1] );

最佳答案

我强烈建议您在使用 RANSAC 算法消除异常特征匹配后,使用标准 8 点算法改进基本矩阵计算。

以下文本取自 calib3d 的 findFundamentalMat 函数文档。 status 是一个可选的输出参数,用于确定数据集中的异常值。

status – Output array of N elements, every element of which is set to 0 for outliers and to 1 for the other points. The array is computed only in the RANSAC and LMedS methods. For other methods, it is set to all 1’s.

然后您可以仅使用带 CV_FM_8POINT 选项的内嵌匹配来获得更可靠的基本矩阵。

关于OpenCV:基本矩阵和移动相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20467403/

相关文章:

c++ - 在 OpenCV 中使用 hog.compute(...) 访问冲突

c++ - 解密不适用于已保存的图像,但适用于图像的像素操作矩阵

matlab - 文档图像二值化

python - 使用OpenCV与Matlab评估校准结果

opencv - 相机校准:尺寸错误会导致正确的失真吗?

node.js - 使用 OpenCV 和 node.js 自动检测瞳孔

c - 使用循环加载多个图像时出现段错误

opencv - 如何使用 OpenCV 获取数字网络摄像头

java - undefined variable "images"Matlab编译代码错误

c++ - 相机传感器尺寸