使用estimateRigidTransform而不是findHomography

Tom*_*ith 6 c++ opencv transformation homography ransac

以下链接中的示例findHomography用于获取两组点之间的转换.我想限制在改造中使用的自由度,所以想更换findHomographyestimateRigidTransform.

http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography

下面我estimateRigidTransform用来获取对象和场景点之间的转换.objPointsscePoints由下式表示vector <Point2f>.

Mat H = estimateRigidTransform(objPoints, scePoints, false);
Run Code Online (Sandbox Code Playgroud)

按照上面教程中使用的方法,我想使用转换来转换角点值H.本教程使用perspectiveTransform返回的3x3矩阵findHomography.使用刚性变换时,它仅返回2x3矩阵,因此无法使用此方法.

如何变换角的值,vector <Point2f>用这个2x3矩阵表示.我只是想要执行与教程相同的功能,但转换的自由度较低.我已经看过其他方法,如warpAffinegetPerspectiveTransform好,但到目前为止还没有找到一个解决方案.

编辑:

我试过David Nilosek的建议.下面我将额外的行添加到矩阵中.

Mat row = (Mat_<double>(1,3) << 0, 0, 1);
H.push_back(row);
Run Code Online (Sandbox Code Playgroud)

但是,这在使用perspectiveTransform时会出现此错误.

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /Users/cgray/Downloads/opencv-2.4.6/modules/core/src/matrix.cpp, line 1486
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/cgray/Downloads/opencv-2.4.6/modules/core/src/matrix.cpp:1486: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create
Run Code Online (Sandbox Code Playgroud)

ChronoTrigger建议使用warpAffine.我在调用warpAffine下面的方法,1 x 5的大小是objCorners和的大小sceCorners.

warpAffine(objCorners, sceCorners, H, Size(1,4));
Run Code Online (Sandbox Code Playgroud)

这给出了下面的错误,这表明错误的类型.objCornerssceCornersvector <Point2f>表示4个角.我认为warpAffine会接受Mat可能解释错误的图像.

OpenCV Error: Assertion failed ((M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 2 && M0.cols == 3) in warpAffine, file /Users/cgray/Downloads/opencv-2.4.6/modules/imgproc/src/imgwarp.cpp, line 3280
Run Code Online (Sandbox Code Playgroud)

Chr*_*ger 5

仿射变换( 的结果cv::estimateRigidTransform)通过函数 应用于图像cv::warpAffine


Mic*_*cka 5

过去我是这样做的:

cv::Mat R = cv::estimateRigidTransform(p1,p2,false);

    if(R.cols == 0)
    {
        continue;
    }

    cv::Mat H = cv::Mat(3,3,R.type());
    H.at<double>(0,0) = R.at<double>(0,0);
    H.at<double>(0,1) = R.at<double>(0,1);
    H.at<double>(0,2) = R.at<double>(0,2);

    H.at<double>(1,0) = R.at<double>(1,0);
    H.at<double>(1,1) = R.at<double>(1,1);
    H.at<double>(1,2) = R.at<double>(1,2);

    H.at<double>(2,0) = 0.0;
    H.at<double>(2,1) = 0.0;
    H.at<double>(2,2) = 1.0;


    cv::Mat warped;
    cv::warpPerspective(img1,warped,H,img1.size());
Run Code Online (Sandbox Code Playgroud)

这与David Nilosek建议的相同:在矩阵末尾添加0 0 1行

此代码通过严格的转换使图像变形。

我想扭曲/转换点,必须使用perspectiveTransform具有3x3矩阵的函数(http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#perspectivetransform

教程在这里:

http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html

或者您可以通过遍历矢量和

cv::Point2f result;
result.x = point.x * R.at<double>(0,0) + point.y * R.at<double>(0,1) + R.at<double>(0,2);
result.y = point.x * R.at<double>(1,0) + point.y * R.at<double>(1,1) + R.at<double>(1,2);
Run Code Online (Sandbox Code Playgroud)

希望能有所帮助。

备注:没有测试手册代码,但应该可以。那里不需要PerspectiveTransform转换!

编辑:这是完整的(经过测试的)代码:

// points
std::vector<cv::Point2f> p1;
p1.push_back(cv::Point2f(0,0));
p1.push_back(cv::Point2f(1,0));
p1.push_back(cv::Point2f(0,1));

// simple translation from p1 for testing:
std::vector<cv::Point2f> p2;
p2.push_back(cv::Point2f(1,1));
p2.push_back(cv::Point2f(2,1));
p2.push_back(cv::Point2f(1,2));

cv::Mat R = cv::estimateRigidTransform(p1,p2,false);

// extend rigid transformation to use perspectiveTransform:
cv::Mat H = cv::Mat(3,3,R.type());
H.at<double>(0,0) = R.at<double>(0,0);
H.at<double>(0,1) = R.at<double>(0,1);
H.at<double>(0,2) = R.at<double>(0,2);

H.at<double>(1,0) = R.at<double>(1,0);
H.at<double>(1,1) = R.at<double>(1,1);
H.at<double>(1,2) = R.at<double>(1,2);

H.at<double>(2,0) = 0.0;
H.at<double>(2,1) = 0.0;
H.at<double>(2,2) = 1.0;

// compute perspectiveTransform on p1
std::vector<cv::Point2f> result;
cv::perspectiveTransform(p1,result,H);

for(unsigned int i=0; i<result.size(); ++i)
    std::cout << result[i] << std::endl;
Run Code Online (Sandbox Code Playgroud)

得到预期的输出:

[1, 1]
[2, 1]
[1, 2]
Run Code Online (Sandbox Code Playgroud)