templateimage的数据库
我正在尝试编写一个python脚本,它使用cv2库来模板匹配图像。我已经弄清楚了如何在图像中检测模板的一个示例,考虑到模板是否具有不同的比例,但我似乎无法弄清楚如何在图像中找到模板的多个示例,同时考虑到比例。
我试着找到第一个匹配,阻止它,然后再次运行扫描,但第二次扫描无法正确找到模板。image with bounding boxesimage to find
有谁知道我能做些什么来检测这两个示例?
下面是我运行的代码:
import numpy as np
import imutils
import glob
import cv2
from imutils.object_detection import non_max_suppression
imagen_a_detectar = r"C:\Users\OMEN\OneDrive\Documentos\TEC\PROYECTO RESIDEO\REPO\SCRUMsinCUM\software\Deteccion Iconos\flecha50.jpg"
imagen_a_comparar = r"C:\Users\OMEN\OneDrive\Documentos\TEC\PROYECTO RESIDEO\REPO\SCRUMsinCUM\software\Deteccion Iconos\HomeAuto3.png"
template = cv2.imread(imagen_a_detectar)
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
for imagePath in glob.glob(imagen_a_comparar):
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found = None
threshold = 0.9
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
scalem = scale
resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
if resized.shape[0] < tH or resized.shape[1] < tW:
break
edged = cv2.Canny(resized, 50, 200)
result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if found is None or max_val > found[0]:
found = (max_val, max_loc, r)
(_, max_loc, r) = found
(startX, startY) = (int(max_loc[0] * r), int(max_loc[1] * r))
(endX, endY) = (int((max_loc[0] + tW) * r), int((max_loc[1] + tH) * r))
m1 = cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), -1)
gray2 = cv2.cvtColor(m1, cv2.COLOR_BGR2GRAY)
resized2 = imutils.resize(gray2, width = int(gray.shape[1] * scalem))
r2 = gray.shape[1] / float(resized.shape[1])
edged2 = cv2.Canny(resized2, 50, 200)
result2 = cv2.matchTemplate(edged2, template, cv2.TM_CCOEFF_NORMED)
min_val2, max_val2, min_loc2, max_loc2 = cv2.minMaxLoc(result2)
found2 = (max_val2, max_loc2, r2)
(_, max_loc2, r2) = found2
(startX2, startY2) = (int(max_loc2[0] * r2), int(max_loc2[1] * r2))
(endX2, endY2) = (int((max_loc2[0] + tW) * r2), int((max_loc2[1] + tH) * r2))
m2 = cv2.rectangle(m1, (startX2, startY2), (endX2, endY2), (0, 0, 255), -1)
cv2.imshow("Image", m2)
cv2.waitKey(0)
print(str(endX) + ' ' +str(endY)+ ' ' +str(r))
字符串
1条答案
按热度按时间q1qsirdb1#
基本上是来自this answer的代码,略有修改:
字符串
的数据