Skip to content

Commit

Permalink
support sift in opencv3
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Apr 24, 2017
1 parent 67fa781 commit d479e49
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions aircv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ def find_all_template(im_source, im_search, threshold=0.5, maxcnt=0, rgb=False,
return result


def _sift_instance(edge_threshold=100):
if hasattr(cv2, 'SIFT'):
return cv2.SIFT(edgeThreshold=edge_threshold)
return cv2.xfeatures2d.SIFT_create(edgeThreshold=edge_threshold)


def sift_count(img):
sift = cv2.SIFT(edgeThreshold=100) if hasattr(cv2, 'SIFT') else cv2.xfeatures2d.SIFT_create(edgeThreshold=100)
sift = _sift_instance()
kp, des = sift.detectAndCompute(img, None)
return len(kp)

Expand Down Expand Up @@ -191,7 +197,7 @@ def find_all_sift(im_source, im_search, min_match_count=4, maxcnt=0):
A tuple of found [{"point": point, "rectangle": rectangle, "confidence": 0.76}, ...]
rectangle is a 4 points list
'''
sift = cv2.SIFT(edgeThreshold=100) if hasattr(cv2, 'SIFT') else cv2.xfeatures2d.SIFT_create(edgeThreshold=100)
sift = _sift_instance()
flann = cv2.FlannBasedMatcher({'algorithm': FLANN_INDEX_KDTREE, 'trees': 5}, dict(checks=50))

kp_sch, des_sch = sift.detectAndCompute(im_search, None)
Expand Down Expand Up @@ -311,24 +317,24 @@ def main():
print(brightness(imsch))

pt = find(imsrc, imsch)
mark_point(imsrc, pt)
show(imsrc)
#mark_point(imsrc, pt)
#show(imsrc)
imsrc = imread('testdata/2s.png')
imsch = imread('testdata/2t.png')
result = find_all_template(imsrc, imsch)
print(result)
pts = []
for match in result:
pt = match["result"]
mark_point(imsrc, pt)
#mark_point(imsrc, pt)
pts.append(pt)
# pts.sort()
show(imsrc)
#show(imsrc)
# print pts
# print sorted(pts, key=lambda p: p[0])

imsrc = imread('testdata/yl/bg_half.png')
imsch = imread('testdata/yl/q_small.png')
imsrc = imread('yl/bg_half.png')
imsch = imread('yl/q_small.png')
print(result)
print('SIFT count=', sift_count(imsch))
print(find_sift(imsrc, imsch))
Expand Down

0 comments on commit d479e49

Please sign in to comment.