Skip to content Skip to sidebar Skip to footer

Python: Opencv WarpPerspective Accepts Neither 2 Nor 3 Parameters

I found the Homography matrix following the Feature Matching + Homography tutorial using M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) and now I need to warp the

Solution 1:

Although it might be so counter-intuitive, for some reason, opencv has implemented warpPerspective function in this way:

corrected_image = cv2.warpPerspective(img1, M, (img1.shape[1], img1.shape[0]))

Also noticed that M obtained is for the mapping the first image to the second, which means I can use M on the first image to warp it and make it overlap with the second. (I was trying to use it on the img2 as shown in the question and it won't work)

And the reason for funny exceptions is NOT known yet. (Please feel free to update this answer if you know why)


Solution 2:

The problem with what you are doing is that you are passing 3 parameters instead of 2 to the "shape" argument. A perspective transform is performed on a gray-scale image which has shape as (H,W). A RGB image has shape (H,W,3) where the last dimension shows the number of channels--red, green and blue. Try with gray-scale image and no errors will be there :)

Edit :

Just saw the date. Guess I am too late :P


Post a Comment for "Python: Opencv WarpPerspective Accepts Neither 2 Nor 3 Parameters"