How to find the transformation matrix for warpPerspective to bring the rotated image to the normalized position?

StackOverflow https://stackoverflow.com/questions/15655569

Question

I need to process some of the information obtained from the scanner. The problem is the image I get occasionally rotated. I decided to add a few special objects on the photo to be able to use with technical Features2D + Homography to find that object. I think after finding special objects I could rotate the image back to its original position (with warpPerspective?). Here is an example:
The rotated image, I know that under the black-box there is some text, I need that area for OCR function: enter image description here

I'm stuck after finding the black-box.I think I'll use the warpPerspective to rotate it, but how to find out the 3x3 transformation matrix?
Thanks.

Was it helpful?

Solution

Use the FindHomography method.

Feed it the 4 source points. For the destination points, you will have to do some math as in where the 4 points should lay down in the destination image. A simple, straightforward operation to start with would be:

(from top-left going clockwise)
A' = (A.x, A.y)
B' = (A.x + length, A.y)
C' = (A.x + length, A.y + width)
D' = (A.x, A.y + width)

Map (A,B,C,D) to (A', B', C', D') using the FindHomography().

Where width and length are those of the identified rectangle.

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