Christian Rodriguez
Christian Rodriguez

Reputation: 690

Python OpenCV - perspective transformation issues

I'm writing a script to process an image and extract a PDF417 2D barcode, which will then be decoded. I'm extracting the ROI without problems, but when I try to correct the perspective using cv2.warpPerspective, the result is not as expected.

The following is the extracted barcode, the red dots are the detected corners: Original

This is the resulting image: Warped

This is the code I'm using for the transformation (the values are found by the script, but for the previous images are as follow):

box_or = np.float32([[23, 30],[395, 23],[26, 2141],[389, 2142]])
box_fix = np.float32([[0,0],[415,0],[0,2159],[415,2159]])
M = cv2.getPerspectiveTransform(box_or,box_fix)
warped = cv2.warpPerspective(img,M,(cols,rows))

I've checked and I don't find anything wrong with the code, yet the transformation is definitely wrong. The amount of perspective distortion in the extracted ROI is minimum, but may affect the decoding process.

So, is there a way to get rid of the perspective distortion? Am I doing something wrong? Is this a known bug or something? Any help is very much welcome.

BTW, I'm using OpenCV 3.3.0

Upvotes: 1

Views: 994

Answers (1)

Spoonless
Spoonless

Reputation: 571

It looks like you're giving the image coordinates as (y, x). I know the interpretation of coordinates varies within OpenCV. In the homography example code they provide the coordinates as (x,y) - at least based on their use of 'h' and 'w' in this snippet:

h,w = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)

So try providing the coordinates as (x,y) to both getPerspectiveTransform and warpPerspective.

Upvotes: 2

Related Questions