0

I'm trying to use the Feature Point Matching tutorial found here: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html

But, I'm getting undefined error on drawMatches:

img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)

On researching, I see that drawMatches is available in OpenCV 3.0.0, but I am using OpenCV 2.4.13.2 and do not want to upgrade because some features are not available in 3.0.0.

I found this alternative drawMatches function:

def draw_matches(img1, kp1, img2, kp2, matches, color=None): 
    """Draws lines between matching keypoints of two images.  
    Keypoints not in a matching pair are not drawn.
    Places the images side by side in a new image and draws circles 
    around each keypoint, with line segments connecting matching pairs.
    You can tweak the r, thickness, and figsize values as needed.
    Args:
        img1: An openCV image ndarray in a grayscale or color format.
        kp1: A list of cv2.KeyPoint objects for img1.
        img2: An openCV image ndarray of the same format and with the same 
        element type as img1.
        kp2: A list of cv2.KeyPoint objects for img2.
        matches: A list of DMatch objects whose trainIdx attribute refers to 
        img1 keypoints and whose queryIdx attribute refers to img2 keypoints.
        color: The color of the circles and connecting lines drawn on the images.  
        A 3-tuple for color images, a scalar for grayscale images.  If None, these
        values are randomly generated.  
    """
    # We're drawing them side by side.  Get dimensions accordingly.
    # Handle both color and grayscale images.
    if len(img1.shape) == 3:
        new_shape = (max(img1.shape[0], img2.shape[0]), img1.shape[1]+img2.shape[1], img1.shape[2])
    elif len(img1.shape) == 2:
        new_shape = (max(img1.shape[0], img2.shape[0]), img1.shape[1]+img2.shape[1])
    new_img = np.zeros(new_shape, type(img1.flat[0]))  
    # Place images onto the new image.
    new_img[0:img1.shape[0],0:img1.shape[1]] = img1
    new_img[0:img2.shape[0],img1.shape[1]:img1.shape[1]+img2.shape[1]] = img2

    # Draw lines between matches.  Make sure to offset kp coords in second image appropriately.
    r = 15
    thickness = 2
    if color:
        c = color
    for m in matches:
        # Generate random color for RGB/BGR and grayscale images as needed.
        if not color: 
            c = np.random.randint(0,256,3) if len(img1.shape) == 3 else np.random.randint(0,256)
        # So the keypoint locs are stored as a tuple of floats.  cv2.line(), like most other things,
        # wants locs as a tuple of ints.
        end1 = tuple(np.round(kp1[m.trainIdx].pt).astype(int))
        end2 = tuple(np.round(kp2[m.queryIdx].pt).astype(int) + np.array([img1.shape[1], 0]))
        cv2.line(new_img, end1, end2, c, thickness)
        cv2.circle(new_img, end1, r, c, thickness)
        cv2.circle(new_img, end2, r, c, thickness)

    plt.figure(figsize=(15,15))
    plt.imshow(new_img)
plt.show()

But, it doesn't take the draw_params parameter, on removing it from the first piece of code (the one from the feature point matching tutorial), I get the following error:

  line 161, in feature_point_matching
    drawMatches(img1,kp1,img2,kp2,good, None)
  line 78, in drawMatches
    new_shape = (max(img1.shape[0], img2.shape[0]), img1.shape[1]+img2.shape[1])
AttributeError: 'NoneType' object has no attribute 'shape'

Please help!!

akrama81
  • 341
  • 1
  • 7
  • 18
  • 2
    Looks like you're passing an empty image. Try using `imshow()` on the image you're sending in prior to sending it to the function to make sure the image is being read correctly. – alkasm Jul 03 '17 at 20:57
  • Also it looks like your error says `drawMatches` but your code says `draw_matches`...please make sure the code matches what you're actually using. And further, this doesn't error on the first line that calls `img1.shape` so it looks like `img2` is the empty image. – alkasm Jul 03 '17 at 21:04

0 Answers0