使用两个不同的相机矩阵计算基本矩阵

时间:2019-01-04 21:27:15

标签: c++ opencv

我有两个不同的相机,我想用openCV查找基本矩阵。 cv::findEssentialMat()无法实现,因为它期望使用相同的摄像机,因此我检查了如下实现:

double fx = cameraMatrix.at<double>(0,0);
double fy = cameraMatrix.at<double>(1,1);
double cx = cameraMatrix.at<double>(0,2);
double cy = cameraMatrix.at<double>(1,2);

points1.col(0) = (points1.col(0) - cx) / fx;
points2.col(0) = (points2.col(0) - cx) / fx;
points1.col(1) = (points1.col(1) - cy) / fy;
points2.col(1) = (points2.col(1) - cy) / fy;

// Reshape data to fit opencv ransac function
points1 = points1.reshape(2, npoints);
points2 = points2.reshape(2, npoints);

threshold /= (fx+fy)/2;

Mat E;
if( method == RANSAC )
    createRANSACPointSetRegistrator(makePtr<EMEstimatorCallback>(), 5, threshold, prob)->run(points1, points2, E, _mask);
else
    createLMeDSPointSetRegistrator(makePtr<EMEstimatorCallback>(), 5, prob)->run(points1, points2, E, _mask);

return E;

我的解决方法如下:

std::vector<cv::Point2d> leftPoints_, rightPoints_;
leftPoints_.reserve(leftPoints.size());
rightPoints_.reserve(leftPoints.size());
for(int i=0;i<leftPoints.size();i++)
{
    double left_x = (leftPoints[i].x - leftCameraMatrix.at<double>(0,2))/leftCameraMatrix.at<double>(0,0);
    double left_y = (leftPoints[i].y - leftCameraMatrix.at<double>(1,2))/leftCameraMatrix.at<double>(1,1);
    cv::Point2d left(left_x, left_y);
    double right_x = (rightPoints[i].x - rightCameraMatrix.at<double>(0,2))/rightCameraMatrix.at<double>(0,0);
    double right_y = (rightPoints[i].y - rightCameraMatrix.at<double>(1,2))/rightCameraMatrix.at<double>(1,1);
    cv::Point2d right(right_x, right_y);

    leftPoints_.push_back(left);
    rightPoints_.push_back(right);
}
cv::Mat fakeCameraMatrix = cv::Mat::eye(3, 3, CV_64F);

cv::Mat E, mask;
double threshold = rejection_threshold;
threshold /= ((leftCameraMatrix.at<double>(0, 0) + rightCameraMatrix.at<double>(0, 0)) / 2 + (leftCameraMatrix.at<double>(1, 1) + rightCameraMatrix.at<double>(1, 1)) / 2) / 2;


E = cv::findEssentialMat(leftPoints_, rightPoints_, fakeCameraMatrix, cv::RANSAC, rejection_confidence, threshold, mask);

看起来像在工作,但是我有非常相似的相机(焦距4.3mm和3.8mm且分辨率相同)。这是好的解决方案吗?我认为实在太简单了。为什么不能在openCV中实现它?

0 个答案:

没有答案