python-3.x - 比较来自 OpenCV matchTemplate 的 maxVal 以进行动态模板匹配
问题描述
我正在调整模板的大小和角度,然后将它们抓取maxVal
并存储在maxLoc
其中作为键。然后我从. 当我遍历所有键并将它们绘制在图像上时,有些人发现米奇头比.cv2.matchTemplate
dict
maxVal
max(data.keys())
result = cv2.matchTemplate(image, resized_template, cv2.TM_CCORR_NORMED)
max(data.keys())
这是“最佳”匹配的区域:
#Find best match from scores
best_match = max(scores.keys())
#Output image
image_copy = color_image.copy()
#Create rectangle around best match
cv2.rectangle(image_copy, (data[best_match][1][0], data[best_match][1][1]), (data[best_match][1][0] + data[best_match][0][0], data[best_match][1][1] + data[best_match][0][1]), (255, 255, 255), 2)
plt.imshow(image_copy)
但这个区域似乎更准确地定位它:
image_copy = color_image.copy()
for n in list(data.keys())[69:70:1]:
#Create rectangle around best match
cv2.rectangle(image_copy, (data[n][1][0], data[n][1][1]), (data[n][1][0] + data[n][0][0], data[n][1][1] + data[n][0][1]), (255, 255, 255), 2)
cv2.imwrite('output_1.jpg', image_copy)
plt.imshow(image_copy)
我是否错过了解释maxVal
fromcv2.matchTemplate
实际代表的内容?如果是这样,我如何根据某些值或标准选择实际的“最佳”区域?这是完整的代码:
#Import packages
import numpy as np
import matplotlib.pyplot as plt
import cv2
import imutils
import os
#Import template
template = cv2.imread(templates_dir + '\\' + os.listdir(templates_dir)[3])
gray_template = cv2.cvtColor(template, cv2.COLOR_RGB2GRAY)
#Import image
image = cv2.imread(images_dir + '\\' + os.listdir(images_dir)[5])
color_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
#Image dims
image_h = image.shape[1]
image_w = image.shape[0]
#Processing loop scaling the template
#Scores dict
scores = {}
#Data dict of structure (maxVal: (template dims, locs, angle))
data = {}
#Edge images
template = cv2.Canny(gray_template, 80, 200)
image = cv2.Canny(gray_image, 80, 200)
#Loop through different template scales
for scale in np.linspace(0.2, 1.5, 15):
#Loop through different template rotations
for angle in np.linspace(0, 360, 25)[:-1]:
#Rotate template
template_rotated = imutils.rotate(template, angle)
#Resize template
resized_template = imutils.resize(template_rotated, width = int(template_rotated.shape[0] * scale))
#Dims of resized template
template_h = resized_template.shape[1]
template_w = resized_template.shape[0]
#Break from loop if the template becomes bigger than the image
if template_h > image_h or template_w > image_w:
break
#Run template through image
result = cv2.matchTemplate(image, resized_template, cv2.TM_CCORR_NORMED)
#Get matching score and location
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
#Add correlation value and result to scores
scores[str(maxVal)] = result
#Add info to data
data[str(maxVal)] = ((template_h, template_w), (maxLoc[0], maxLoc[1]), angle)
#Find best match from scores
best_match = max(scores.keys())
#Output image
image_copy = color_image.copy()
#Create rectangle around best match
cv2.rectangle(image_copy, (data[best_match][1][0], data[best_match][1][1]), (data[best_match][1][0] + data[best_match][0][0], data[best_match][1][1] + data[best_match][0][1]), (0, 0, 255), 2)
plt.imshow(image_copy)
解决方案
推荐阅读
- python - 自定义损失函数 keras TypeError
- swift - PBItemCollectionServicer 连接断开 UIDragInteraction
- django - 将文章传输到呈现为字符串的标签
- image - Codeigniter 4 image watermark
- javascript - ScrollIntoView() 可以找到html元素
- python - Django rest框架中带参数的原始sql查询
- git - 从多个 git 提交中获取文件的文件哈希(sha256sum)
- javascript - 在控制台中获取数组值
- cors - 如何让我的反应前端与我的 nextjs 后端对话
- kibana - 带有 lucene 的 kibana 中的正则表达式