Last active
December 11, 2015 06:29
-
-
Save jayrambhia/4559834 to your computer and use it in GitHub Desktop.
SIFT match keypoints and draw.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment