Search code examples
pythonpython-3.xnumpyopencvorb

When use "cv2.drawMatches", error occurs: "outImg is not a numpy array, neither a scalar"


I have the following code fo the keyframe matching by ORB:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread("C:\\Users\\user\\Desktop\\picture\\Pikachu_Libre.png",0)
img2 = cv2.imread("C:\\Users\\user\\Desktop\\picture\\Pikachu_Libre.png",0)
# Initiate STAR detector
orb = cv2.ORB_create()

# find the keypoints with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,None,matches[:10], flags=2)
plt.imshow(img3),plt.show()

After i run I get the following error:

img3 = cv2.drawMatches(img1,kp1,img2,kp2,None,matches[:10], flags=2)
TypeError: outImg is not a numpy array, neither a scalar

Anyone can help me on this?


Solution

  • Notice the prototype of cv2.drawMatches():

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

    So your parameters' order is wrong.


    From:

    img3 = cv2.drawMatches(img1,kp1,img2,kp2,None,matches[:10], flags=2)
    

    To:

    img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], None,flags=2)