dontloo dontloo - 1 year ago 185
C++ Question

Compute the similarity transformation matrix given two sets of points?

I know in OpenCV we can get the affine transformation given two sets of points by


only supports pre-computed angel and scale.

How can I compute the similarity transformation matrix given two sets of points?

Answer Source

There is cv::estimateRigidTransform. You can choose between a full affine transform, which has 6 degrees of freedom (rotation, translation, scaling, shearing) or a partial affine (rotation, translation, uniform scaling), which has 5 degrees of freedom.

You can compute the similarity transform by two vector<Point> p1 and p2 with the code from this answer:

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

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

// compute perspectiveTransform on p1
std::vector<cv::Point2f> result;

//warp image with transform
cv::Mat warped;

I didnt try it, but refering to the answer it should work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download