OpenCV Python : No drawMatchesknn function

*爱你&永不变心* 提交于 2019-11-29 06:49:28
ryanmeasel

The functions cv2.drawMatches and cv2.drawMatchesKnn are not available in newer versions of OpenCV 2.4. @rayryeng provided a lightweight alternative which works as is for the output of DescriptorMatcher.match. The difference with DescriptorMatcher.knnMatch is that the matches are returned as a list of lists. To use the @rayryeng alternative, the matches must be extracted into a 1-D list.

For example, the Brute-Force Matching with SIFT Descriptors and Ratio Test tutorial could be amended as such:

# 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:
       # Removed the brackets around m 
       good.append(m)

# Invoke @rayryeng's drawMatches alternative, note it requires grayscale images
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
drawMatches(gray1,kp1,gray2,kp2,good)

You need to use OpenCV version 3. drawMatchesKnn() is present in 3.0.0-alpha but not in 2.4.11

That error is there, because you are using an old version of OpenCV.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!