How to estimate 2D similarity transformation (linear conformal, nonreflective similarity) in OpenCV?

故事扮演 提交于 2019-12-04 09:42:54

问题


I'm trying to search a specific object in input images by matching SIFT descriptors and finding the transformation matrix by RANSAC. The object can only be modified in scene by similarity transform in 2D space (scaled, rotated, translated), so I need to estimate 2x2 transform matrix instead of 3x3 homography matrix in 3D space. How can I achieve this in OpenCV?


回答1:


You can use estimateRigidTransform (I do not know whether it is RANSAC, the code at http://code.opencv.org/projects/opencv/repository/revisions/2.4.4/entry/modules/video/src/lkpyramid.cpp says RANSAC in its comment), the third parameter is set to false in order to get just scale+rotation+translation:

#include <vector>
#include <iostream>
#include "opencv2/video/tracking.hpp"

int main( int argc, char** argv )
{
    std::vector<cv::Point2f> p1s,p2s;

    p1s.push_back(cv::Point2f( 1, 0));
    p1s.push_back(cv::Point2f( 0, 1));
    p1s.push_back(cv::Point2f(-1, 0));
    p1s.push_back(cv::Point2f( 0,-1));

    p2s.push_back(cv::Point2f(1+sqrt(2)/2, 1+sqrt(2)/2));
    p2s.push_back(cv::Point2f(1-sqrt(2)/2, 1+sqrt(2)/2));
    p2s.push_back(cv::Point2f(1-sqrt(2)/2, 1-sqrt(2)/2));
    p2s.push_back(cv::Point2f(1+sqrt(2)/2, 1-sqrt(2)/2));

    cv::Mat t = cv::estimateRigidTransform(p1s,p2s,false);

    std::cout << t << "\n";

    return 0;
}

compiled and tested with OpenCV 2.4.4. The output is:

[0.7071067988872528, -0.7071067988872528, 1.000000029802322;
  0.7071067988872528, 0.7071067988872528, 1.000000029802322]



回答2:


You can use find an affine transformation between the point sets using opencv, this is slightly more general than the case you are describing (known as a similarity transform) as it describes shearing transformations of the shapes as well.

It can be performed using the function getAffineTransform(InputArray src, InputArray dst). This takes 2 sets of three points and calculates an affine transform between them.



来源:https://stackoverflow.com/questions/14833049/how-to-estimate-2d-similarity-transformation-linear-conformal-nonreflective-si

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!