def find_all_sift(im_source, im_search, min_match_count=<NUM_LIT:4>, maxcnt=<NUM_LIT:0>):
sift = _sift_instance()<EOL>flann = cv2.FlannBasedMatcher({'<STR_LIT>': FLANN_INDEX_KDTREE, '<STR_LIT>': <NUM_LIT:5>}, dict(checks=<NUM_LIT:50>))<EOL>kp_sch, des_sch = sift.detectAndCompute(im_search, None)<EOL>if len(kp_sch) < min_match_count:<EOL><INDENT>return None<EOL><DEDENT>kp_src, des_src = sift.detectAndCompute(im_source, None)<EOL>if len(kp_src) < min_match_count:<EOL><INDENT>return None<EOL><DEDENT>h, w = im_search.shape[<NUM_LIT:1>:]<EOL>result = []<EOL>while True:<EOL><INDENT>matches = flann.knnMatch(des_sch, des_src, k=<NUM_LIT:2>)<EOL>good = []<EOL>for m, n in matches:<EOL><INDENT>if m.distance < <NUM_LIT> * n.distance:<EOL><INDENT>good.append(m)<EOL><DEDENT><DEDENT>if len(good) < min_match_count:<EOL><INDENT>break<EOL><DEDENT>sch_pts = np.float32([kp_sch[m.queryIdx].pt for m in good]).reshape(-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:2>)<EOL>img_pts = np.float32([kp_src[m.trainIdx].pt for m in good]).reshape(-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:2>) <EOL>M, mask = cv2.findHomography(sch_pts, img_pts, cv2.RANSAC, <NUM_LIT>)<EOL>matches_mask = mask.ravel().tolist()<EOL>h, w = im_search.shape[:<NUM_LIT:2>]<EOL>pts = np.float32([[<NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:0>, h-<NUM_LIT:1>], [w-<NUM_LIT:1>, h-<NUM_LIT:1>], [w-<NUM_LIT:1>, <NUM_LIT:0>]]).reshape(-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:2>)<EOL>dst = cv2.perspectiveTransform(pts, M)<EOL>pypts = []<EOL>for npt in dst.astype(int).tolist():<EOL><INDENT>pypts.append(tuple(npt[<NUM_LIT:0>]))<EOL><DEDENT>lt, br = pypts[<NUM_LIT:0>], pypts[<NUM_LIT:2>]<EOL>middle_point = (lt[<NUM_LIT:0>] + br[<NUM_LIT:0>]) / <NUM_LIT:2>, (lt[<NUM_LIT:1>] + br[<NUM_LIT:1>]) / <NUM_LIT:2><EOL>result.append(dict(<EOL>result=middle_point,<EOL>rectangle=pypts,<EOL>confidence=(matches_mask.count(<NUM_LIT:1>), len(good)) <EOL>))<EOL>if maxcnt and len(result) >= maxcnt:<EOL><INDENT>break<EOL><DEDENT>qindexes, tindexes = [], []<EOL>for m in good:<EOL><INDENT>qindexes.append(m.queryIdx) <EOL>tindexes.append(m.trainIdx) <EOL><DEDENT>def filter_index(indexes, arr):<EOL><INDENT>r = np.ndarray(<NUM_LIT:0>, np.float32)<EOL>for i, item in enumerate(arr):<EOL><INDENT>if i not in qindexes:<EOL><INDENT>r = np.append(r, item)<EOL><DEDENT><DEDENT>return r<EOL><DEDENT>kp_src = filter_index(tindexes, kp_src)<EOL>des_src = filter_index(tindexes, des_src)<EOL><DEDENT>return result<EOL>
使用sift算法进行多个相同元素的查找
Args:
    im_source(string): 图像、素材
    im_search(string): 需要查找的图片
    threshold: 阈值，当相识度小于该阈值的时候，就忽略掉
    maxcnt: 限制匹配的数量

Returns:
    A tuple of found [(point, rectangle), ...]
    A tuple of found [{"point": point, "rectangle": rectangle, "confidence": 0.76}, ...]
    rectangle is a 4 points list