dontloo - 2 months ago 41

C++ Question

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

.But

`getRotationMatrix2D()`

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

Answer

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());
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)
//warp image with transform
cv::Mat warped;
cv::warpPerspective(src,warped,H,src.size());
```

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