public
Last active

SIFT match keypoints and draw.

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
def matchSIFTKeyPoints(self, template, quality=200):
try:
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:
return
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.