Question

can somebody help me, how we can calculate the new positions of keypoints in the transformed image,the keypoints were detected in original image. I am using opencv homography matrix and warpPerspective to make the transformed image.

Here is a code..

...
 std::vector< Point2f > points1,points2;
 for( int i = 0; i < matches1.size(); i++ )
    {
     points1.push_back( keypoints_input1[matches1[i].queryIdx ].pt );
     points2.push_back( keypoints_input2[matches1[i].trainIdx ].pt );
    }
 /* Find the Homography Matrix for current and next frame*/
 Mat H1 = findHomography( points2, points1, CV_RANSAC );
 /* Use the Homography Matrix to warp the images*/
cv::Mat result1;
warpPerspective(input2, result1, H1, Size(input2.cols+150, input2.rows+150),              
INTER_CUBIC);
...
}

Now I want to calculate the new positions of points2 in the result1 image.

For example in the below transformed image , we know the corner points. Now I want to calculate the new position of the keypoints say before transformation {(x1,y1),(x2,y2),(x3,y3)...}, How we can calculate it?

Update: opencv 'perspectiveTransform' does what I trying to do.

Was it helpful?

Solution

Let's call I' the image obtained by warping image I using homography H.

If you extracted keypoints mi = (xi, yi, 1) in original image I, you can get the keypoints m'i in the warped image I' using the homography transform: S * m'i = H * mi. Notice the scale factor S, if you want the keypoints coordinates in pixels, you have to scale m'i so that the third element is 1.

If you want to understand where the scale factor comes from, have a look at Homogeneous Coordinates.

Also, there is an OpenCV function to apply this transformation to an array of points: perspectiveTransform(documentation).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top