问题
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 second image (rotated one) to match the keypoints of the first one, so I tried to do with warpPerspective
directly on img2
since we have the Homography matrix already. (They did not use warpPersective in the tutorial)
dst = cv2.warpPerspective(img2, M)
and it complains that I'm missing the third argument
TypeError: Required argument 'dsize' (pos 3) not found
Fair enough, I checked what dsize
is in the docs, and it seems it's the destination image size. Well, it could be inferred if not given, but opencv is expecting it, (grrr... fine opencv, I'll give you that). I tried again
dst = cv2.warpPerspective(img2, M, img2.shape)
and it throws
TypeError: function takes exactly 2 arguments (3 given)
Wait, didn't I try it with 2 arguments just a minute ago?
What's wrong?
回答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)
来源:https://stackoverflow.com/questions/50947010/python-opencv-warpperspective-accepts-neither-2-nor-3-parameters