Bootstrap

Python3 TypeError: Required argument ‘outImg‘ (pos 3) not found

 

 

问题:在用python3使用img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)的时候,可能会产生错误:TypeError: Required argument 'outImg' (pos 6) not found

 
  1. import numpy as np

  2. import cv2

  3. from matplotlib import pyplot as plt

  4. img1 = cv2.imread('data/queryImage.jpg', 0) # queryImage

  5. img2 = cv2.imread('data/trainImage.jpg', 0) # trainImage

  6. # Initiate SIFT detector

  7. sift = cv2.xfeatures2d.SIFT_create()

  8. # find the keypoints and descriptors with SIFT

  9. kp1, des1 = sift.detectAndCompute(img1,None)

  10. kp2, des2 = sift.detectAndCompute(img2,None)

  11. # BFMatcher with default params

  12. bf = cv2.BFMatcher()

  13. matches = bf.knnMatch(des1,des2, k=2)

  14. # Apply ratio test

  15. good = []

  16. for m,n in matches:

  17. if m.distance < 0.75*n.distance:

  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. ....

报错信息:

 
  1. ---------------------------------------------------------------------------

  2. TypeError Traceback (most recent call last)

  3. <ipython-input-54-8a7d0ce88553> in <module>()

  4. 18 good.append([m])

  5. 19 # cv2.drawMatchesKnn expects list of lists as matches.

  6. ---> 20 img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)

  7. 21 plt.imshow(img3),plt.show()

  8.  
  9. 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的形式传入。

 
  1. import cv2

  2. cv2.drawMatchesKnn?

  3. ------------------------------------------------------------------------------------------------------

  4. Docstring:

  5. drawMatchesKnn(img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]) -> outImg

  6. . @overload

  7. Type: builtin_function_or_method

  8.  
  9.  
  10. demo:

  11. draw_params = dict(matchColor = (0,255,0),

  12. singlePointColor = (255,0,0),

  13. matchesMask = matchesMask,

  14. flags = 0)

  15.  
  16. 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

 

;