Binyamin Even
Binyamin Even

Reputation: 3382

openCV perspective transformation doesn't work as expected

I have an image with a text. This is the image: image with text

What I'm trying to do is to straighten the text, using perspective transformation.

The red dots in the corners are the detected boundaries.

This is more or less my code (hard-coded, for simplicity):

old_pts=np.float32([[2,41],[37,965],[1389,1121],[1389,0]])
bor=cv2.boundingRect(old_pts) #bounding_rect
ul=[bor[0],      bor[1]]       #upper left
ur=[bor[0],      bor[1]+bor[3]] #upper right
br=[bor[0]+bor[2],bor[1]+bor[3]] #bottom right
bl=[bor[0]+bor[2],bor[1]]       #bottom left

new_pts=np.float32([ul,ur,br,bl]) #new pts=[[2,0],[2,1122],[1390,1122],[1390,0]]

M = cv2.getPerspectiveTransform(old_pts,new_pts)
transformed_img = cv2.warpPerspective(new_img,M,(bor[3],bor[2])) #bor[3] and bor[4] are the bounding rect height&width. 

transforemed_img=transformed_img.astype(int)
cv2.imwrite('transformed.png',transformed_img)

Now, the result I'm getting is this:

transformed image

Why am I not getting a nice, straightened rectangle??

Any help will be appreciated!

Upvotes: 1

Views: 1118

Answers (1)

api55
api55

Reputation: 11420

Take a look at your points:

You have :

old_pts=np.float32([[2,41],[37,965],[1389,1121],[1389,0]])

and:

new_pts=np.float32([ul,ur,br,bl]) #new pts=[[2,0],[2,1122],[1390,1122],[1390,0]]

But, OpenCV manages POINTS as (x,y) values, yours are in (y,x)... I know this is confusing, since the matrix manipulation is done with (y,x) notation... The thing is that OpenCV sees the matrix manipulation as the rows and columns like a matrix, but points are seen as Cartesian coordinates...

In conclusion, try flipping the axes for the points and check the results.

Upvotes: 3

Related Questions