Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SIFT match keypoints and draw.
def matchSIFTKeyPoints(self, template, quality=200):
import cv2
except ImportError:
logger.warning("OpenCV >= 2.3.0 required")
if template == None:
return None
detector = cv2.FeatureDetector_create("SIFT")
descriptor = cv2.DescriptorExtractor_create("SIFT")
img = self.getNumpyCv2()
template_img = template.getNumpyCv2()
skp = detector.detect(img)
skp, sd = descriptor.compute(img, skp)
tkp = detector.detect(template_img)
tkp, td = descriptor.compute(tmeplate_img, tkp)
idx, dist = self._getFLANNMatches(sd, td)
tfs = []
for i, dis in itertools.izip(idx, dist):
if dis < quality:
tfs.append(KeyPoint(template, tkp[i], td, "SIFT"))
idx, dist = self._getFLANNMatches(td, sd)
sfs = []
for i, dis in itertools.izip(idx, dist):
if dis < quality:
sfs.append(KeyPoint(template, skp[i], sd, "SIFT"))
return sfs, tfs
def drawSIFTKeyPointMatch(self, template, quality=200, width=1):
if template == None:
resultImg = template.sideBySide(self,scale=False)
hdif = (self.height-template.height)/2
sfs, tfs = self.matchSIFTKeyPoints(template, quality)
for skp, tkp in itertools.izip(sfs, tfs):
pt_a = (int(tkp.y), int(tkp.x)+hdif)
pt_b = (int(skp.y)+template.width, int(skp.x))
resultImg.drawLine(pt_a, pt_b, color=Color.getRandom(Color()),thickness=width)
return resultImg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment