0

currently i am working on opevcv with python but when i use

    kp1 = orb.detect(img1,None)
    kp2 = orb.detect(img2,None)
    kp1, des1 = orb.compute(img1, kp1)
    kp2, des2 = orb.compute(img2, kp2)
    matches = matcher.match(des1, des2)

i get the error that matcher is not defined

    matches = matcher.match(des1, des2)
    NameError: name 'matcher' is not defined

, i am using opencv 3.0.0 with python 2.7, can anyone tell me why i am getting this error ?? can we use matcher or not with python ??

whishky
  • 396
  • 3
  • 13
  • Have you created the [matcher object](http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html) first?. Something like `matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)` – Miki Jan 27 '16 at 13:31
  • no, actually i am newbie in this field so i don't know whats the proper syntax of creating matcher object – whishky Jan 27 '16 at 13:34
  • Check the tutorial I posted in the link above – Miki Jan 27 '16 at 13:35
  • after creating the matcher object now i got a new error img12 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,flag=2) TypeError: 'flag' is an invalid keyword argument for this function – whishky Jan 27 '16 at 13:36
  • the tutorial suggests using `flags`, with the "s"! `cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)` – Miki Jan 27 '16 at 13:38
  • thanks Miki, its done now – whishky Jan 27 '16 at 13:42

2 Answers2

2

You need to create the matcher object first. A complete example can be found on OpenCV tutorials:

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

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

# Initiate ORB detector
orb = cv2.ORB()

# find the keypoints and descriptors with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

# create BFMatcher object
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,matches[:10], flags=2)

plt.imshow(img3),plt.show()
Miki
  • 40,887
  • 13
  • 123
  • 202
2

In the above code instead of orb = cv2.ORB()

use orb = cv2.ORB_create()

this resolves version compatibility issues:

TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Midun
  • 21
  • 4