Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Calculating the bounding box of the transformed image was a good step :)

Now, as you said, you have to "translate" the final image to fit the bounding box. So if the tranformed top-left corner is at (-10,-10), you have to translate it 10 pixels on each direction.

The translation matrix has the following form:

    | 1 0 tx |
A = | 0 1 ty |
    | 0 0  1 |

where tx=-bb.left and ty=-bb.top (bb is the bounding box)

If you have a P transformation matrix, calculate the final matrix F = P x A. The size of the transformed image is (bb.height,bb.width).

Calculating the bounding box of the transformed image was a good step :)

Now, as you said, you have to "translate" the final image to fit the bounding box. So if the tranformed top-left corner is at (-10,-10), you have to translate it 10 pixels on each direction.

The translation matrix has the following form:

    | 1 0 tx |
A = | 0 1 ty |
    | 0 0  1 |

where tx=-bb.left and ty=-bb.top (bb is the bounding box)box).

If you have a P transformation matrix, calculate the final matrix F = P A x AP. The size of the transformed image is (bb.height,bb.width)(bb.width,bb.height).

Code:

Mat A = Mat::eye(3,3,CV_64F); A.at<double>(0,2)= -bb.top; A.at<double>(1,2)= -bb.left;
Mat F = A*P;
warpPerspective(image,result,F,Size(bb.width,bb.height);

Note: if there's a problem, check if I didn't mess up the x/y and width/height order... (I looked at some old code I had lying around.

Calculating the bounding box of the transformed image was a good step :)

Now, as you said, you have to "translate" the final image to fit the bounding box. So if the tranformed top-left corner is at (-10,-10), you have to translate it 10 pixels on each direction.

The translation matrix has the following form:

    | 1 0 tx |
A = | 0 1 ty |
    | 0 0  1 |

where tx=-bb.left and ty=-bb.top (bb is the bounding box).

If you have a P transformation matrix, calculate the final matrix F = A x P. The size of the transformed image is (bb.width,bb.height).

Code:

Mat A = Mat::eye(3,3,CV_64F); A.at<double>(0,2)= -bb.top; A.at<double>(1,2)= -bb.left;
Mat F = A*P;
warpPerspective(image,result,F,Size(bb.width,bb.height);
warpPerspective(image,result,F,Size(bb.width,bb.height));

Note: if there's a problem, check if I didn't mess up the x/y and width/height order... (I looked at some old code I had lying around.around).