问题:在用python3使用img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)的时候,可能会产生错误:TypeError: Required argument 'outImg' (pos 6) not found
-
import numpy as np
-
import cv2
-
from matplotlib import pyplot as plt
-
img1 = cv2.imread('data/queryImage.jpg', 0) # queryImage
-
img2 = cv2.imread('data/trainImage.jpg', 0) # trainImage
-
# Initiate SIFT detector
-
sift = cv2.xfeatures2d.SIFT_create()
-
# find the keypoints and descriptors with SIFT
-
kp1, des1 = sift.detectAndCompute(img1,None)
-
kp2, des2 = sift.detectAndCompute(img2,None)
-
# BFMatcher with default params
-
bf = cv2.BFMatcher()
-
matches = bf.knnMatch(des1,des2, k=2)
-
# Apply ratio test
-
good = []
-
for m,n in matches:
-
if m.distance < 0.75*n.distance:
-
good.append([m])
-
# cv2.drawMatchesKnn expects list of lists as matches.
-
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)
-
....
报错信息:
-
---------------------------------------------------------------------------
-
TypeError Traceback (most recent call last)
-
<ipython-input-54-8a7d0ce88553> in <module>()
-
18 good.append([m])
-
19 # cv2.drawMatchesKnn expects list of lists as matches.
-
---> 20 img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)
-
21 plt.imshow(img3),plt.show()
-
TypeError: Required argument 'outImg' (pos 6) not found
解决:将img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2) 替换为:
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2) 即:如果没有outImg参数则传入None。
分析:
在最后一个demo中:是将outImg参数以None的形式传入。
-
import cv2
-
cv2.drawMatchesKnn?
-
------------------------------------------------------------------------------------------------------
-
Docstring:
-
drawMatchesKnn(img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]) -> outImg
-
. @overload
-
Type: builtin_function_or_method
-
demo:
-
draw_params = dict(matchColor = (0,255,0),
-
singlePointColor = (255,0,0),
-
matchesMask = matchesMask,
-
flags = 0)
-
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
在python3中,如果没有outImg参数则传入None就没问题了。
变更样例:
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
可参考文章:https://www.e-learn.cn/content/wangluowenzhang/309127
说明:问题产生的环境
Python版本:3.6.5
OpenCV版本:3.4.2