Home openCV perspective transformation doesn't work as expected
Reply: 1

openCV perspective transformation doesn't work as expected

Binyamin Even
1#
Binyamin Even Published in 2018-02-14 08:23:45Z

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

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:

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

Any help will be appreciated!

api55
2#
api55 Reply to 2018-02-14 10:42:53Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.348357 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO