Python:opencv warpPerspective既不接受2个参数也不接受3个参数 [英] Python: opencv warpPerspective accepts neither 2 nor 3 parameters
问题描述
我在功能匹配+全息照相教程<<中找到了全息照相矩阵/a>使用
I found the Homography matrix following the Feature Matching + Homography tutorial using
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
,现在我需要扭曲第二个图像(旋转的图像)以匹配第一个图像的关键点,因此,由于我们已经具有Homography矩阵,因此我尝试直接在img2
上使用warpPerspective
进行操作. (他们没有在本教程中使用warpPersective)
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
足够公平,我检查了dsize是什么. > docs ,它似乎是目标图像的大小.好吧,如果没有给出,可以推断出来,但是opencv期待它(grrr ...好吧,opencv,我给你).我再试一次
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)
它会抛出
TypeError: function takes exactly 2 arguments (3 given)
等等,我不是在一分钟前用2个参数尝试过吗?
Wait, didn't I try it with 2 arguments just a minute ago?
怎么了?
推荐答案
尽管出于某种直觉,出于某种原因,opencv通过以下方式实现了warpPerspective
函数:
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]))
还要注意,获得的M用于将第一幅图像映射到第二幅图像,这意味着我可以在第一幅图像上使用M来扭曲它并使它与第二幅图像重叠. (我正尝试按照问题所示在img2上使用它,它将无法正常工作)
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)
这篇关于Python:opencv warpPerspective既不接受2个参数也不接受3个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!