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

Posted nmucomputer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv 找金币,按照金字塔缩小尝试匹配相关的知识,希望对你有一定的参考价值。

 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:PycharmProjectsOpenCVpicsuper_maria_coin.jpg")
32 src2 = cv.imread(r"C:PycharmProjectsOpenCVpicsuper_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 找金币,按照金字塔缩小尝试匹配的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV 图像上采样和降采样

OpenCV 图像上采样和降采样

OpenCV--图像金字塔

OpenCV---图像金字塔原理

OpenCV 学习笔记(重采样和图像缩放)

OpenCV金字塔图像分辨率重建与融合