码迷,mamicode.com
首页 > 其他好文 > 详细

opencv 找金币,按照金字塔缩小尝试匹配

时间:2020-01-23 22:41:59      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ever   project   match   app   pyplot   waitkey   lse   cts   pycharm   

 1 # Author:Winter Liu is coming!
 2 import cv2 as cv
 3 import numpy as np
 4 import matplotlib.pyplot as plt
 5 
 6 
 7 def match_demo(src1, src2):
 8     res = cv.matchTemplate(src2, src1, cv.TM_CCOEFF_NORMED)
 9     # res = cv.matchTemplate(src2, src1, cv.TM_SQDIFF_NORMED)
10     # min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
11     pt = np.where(res > 0.5)
12     h, w, c = src1.shape
13     print(len(pt[0]))
14     # print(max_val)
15     # print(max_loc)
16     # cv.rectangle(src2,max_loc, (max_loc[0]+h, max_loc[1]+w), (0, 0, 255), 2)
17     a = [(-1, -1)]
18     p_zip = zip(pt[1], pt[0])
19     p_n = sorted(p_zip, key=lambda x: x[0], reverse=False)
20     for p in p_n:
21         # print(p)
22         if abs(a[-1][0] - p[0]) > 10 or abs(a[-1][1] - p[1]) > 10:
23             print(p)
24             cv.rectangle(src2, p, (p[0]+w, p[1]+h), (0, 0, 255), 2)
25             a.append(p)
26     cv.imshow("src2", src2)
27     cv.waitKey(0)
28     cv.destroyWindow("src2")
29 
30 
31 src1 = cv.imread(r"C:\PycharmProjects\OpenCV\pic\super_maria_coin.jpg")
32 src2 = cv.imread(r"C:\PycharmProjects\OpenCV\pic\super_maria.jpg")
33 match_demo(src1, src2)
34 down = src1.copy()
35 for i in range(4):
36     down = cv.pyrDown(down)
37     cv.imshow("down", down)
38     cv.waitKey(0)
39     cv.destroyWindow("down")
40     match_demo(down, src2)

opencv 找金币,按照金字塔缩小尝试匹配

标签:ever   project   match   app   pyplot   waitkey   lse   cts   pycharm   

原文地址:https://www.cnblogs.com/nmucomputer/p/12231465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!