gpt4 book ai didi

Python OpenCV : Using matchTemplate

转载 作者:行者123 更新时间:2023-12-02 17:09:31 27 4
gpt4 key购买 nike

我有两个表面,一个大的 surface image

和一个较小的 enter image description here

我正在尝试调整 template matching tutorial通过将它们视为灰度图像来处理这些表面。

我需要更新教程以在 x 和 y 中独立缩放,我已经这样做了,但添加了一个额外的循环。我的代码是:

import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# If True shows each iteration of the template matching
Visualise = True

# Load in image and template
image = pd.read_excel('TemplateMatching_exampleData.xlsx',sheetname="radial_template").as_matrix().astype(np.float32)
template = pd.read_excel('TemplateMatching_exampleData.xlsx',sheetname="radial_image").as_matrix().T.astype(np.float32)

# Save a raw copy of the template
template_raw = template

# Rescale the template to approximate the same range in values as the image
template = template - np.mean(template)
template = (template/np.max(template)) * np.max(image)

# Get the height and width of the template
(tH, tW) = template.shape[:2]

# initialize the bookkeeping variable to keep track of the matched region
found = None

# If visualise = True then initialise the figure to show the iteration
if Visualise:
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)

# loop over the scales of the image
for scale_width in np.linspace(0.1, 2, 20):
for scale_height in np.linspace(0.1, 3, 20)[::-1]:
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = cv2.resize(image, ( int(image.shape[0] * scale_height), int(image.shape[1] * scale_width) ))
r_h = image.shape[0] / float(resized.shape[0])
r_w = image.shape[1] / float(resized.shape[1])

# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break

# apply template matching to find the template in the image
result = cv2.matchTemplate(resized, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

# check to see if the iteration should be visualized
if Visualise:
ax1.clear()
ax1.imshow(resized)
ax1.add_patch(patches.Rectangle((maxLoc[0], maxLoc[1]), tW, tH, fill=False, edgecolor = 'red'))
plt.show()
plt.draw()
plt.pause(0.05) # fig1.waitforbuttonpress()

# if we have found a new maximum correlation value, then update
# the bookkeeping variable
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r_w, r_h, scale_width, scale_height)

# unpack the bookkeeping varaible and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
(_, maxLoc, r_w, r_h, scale_width, scale_height) = found
(startX, startY) = (int(maxLoc[0] * r_w), int(maxLoc[1] * r_h))
(endX, endY) = (int((maxLoc[0] + tW) * r_w), int((maxLoc[1] + tH) * r_h))

# draw a bounding box around the detected result and display the image
figure = plt.figure()
ax1 = figure.add_subplot(111)
ax1.imshow(image)
ax1.add_patch(patches.Rectangle((startX,startY), endX-startX, endY-startY, fill=False, edgecolor = 'red'))
plt.show()
plt.draw()

# show the matching image segment and template together
plt.figure()
plt.subplot(121)
plt.imshow(image[startX:endX, startY:endY])
plt.title('Image')
plt.subplot(122)
plt.imshow(template)
plt.title('Template')

然而,我发现“最佳拟合”总是在最大的 scale_width 处找到。值,无论我将此值设置为什么,但我不知道为什么。我猜这是如何测量适合度的结果,但我对 cv2.matchTemplate 了解不够为了能够纠正它,我已经为此苦恼了好几天。

请你能帮我修复我的代码吗?

我已将数据从 Excel 文件复制到 this GoogleDocs document .

最佳答案

从你能解决的问题开始

所以首先需要做一些簿记——似乎 imagetemplate在 Excel 文档中切换,进一步看来 template实际上是旋转了 90 度。为了得到有效的东西,我将手动将模板缩放到大致合适的大小,并确保可以找到模板。请注意,我将每张工作表导出为 .csv文件并更正名称。

此外,我将图像设置为均值为零,标准差为一,只需减去均值并除以标准差即可。这应该使图像保持大致相同的分布,以便沿着原始数组中高度不同的值进行良好匹配。

import cv2
import numpy as np

img = np.genfromtxt('radial_img.csv', delimiter=',').astype(np.float32)
tmp = np.genfromtxt('radial_tmp.csv', delimiter=',').astype(np.float32)
tmp = np.rot90(tmp)
tmp = cv2.resize(tmp, None, fx=0.5, fy=0.33)

img = (img - np.mean(img))/np.std(img)
tmp = (tmp - np.mean(tmp))/np.std(tmp)

ccorr = cv2.matchTemplate(img, tmp, cv2.TM_CCORR)
tl = cv2.minMaxLoc(ccorr)[3]
h, w = tmp.shape[:2]
br = (tl[0]+w, tl[1]+h)

matched = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
matched = cv2.merge([matched, matched, matched])
cv2.rectangle(matched, tl, br, (0, 255, 0))
cv2.imshow('matched0.png', matched)
cv2.waitKey()

Matched with manual resize

伟大的!有用!请注意,这里的模板实际上非常小,比它应该的要小得多,但我们仍然得到了大致的位置,所以我们知道我们在正确的轨道上。

循环遍历多种尺寸

现在我们只需要创建您制作的那些循环并多次调整模板大小,直到我们获得最佳匹配。但是请注意,模板匹配的方法都有与模板大小相关的比例,因此较小的模板将具有较小的错误。因此我们需要使用 _NORMED确保结果大多是尺度不变的方法。而不是创建多个 for在内部循环和调整大小,我认为使用列表推导来构建所有调整大小的图像,然后遍历所有这些图像会更清楚。我们可以在进行过程中存储最佳结果,然后在完成后显示最佳结果。请注意,这里的所有规范化都是为了可视化;不需要使用 OpenCV 功能:
import cv2
import numpy as np

img = np.genfromtxt('radial_img.csv', delimiter=',').astype(np.float32)
tmp = np.genfromtxt('radial_tmp.csv', delimiter=',').astype(np.float32)
tmp = np.rot90(tmp)

img = (img - np.mean(img))/np.std(img)
tmp = (tmp - np.mean(tmp))/np.std(tmp)

sz_ranges = np.linspace(0.1, 2.0, 19)
resized_tmps = [cv2.resize(tmp, None, fx=i, fy=j)
for i in sz_ranges for j in sz_ranges]
n_tmps = len(resized_tmps)

for rs_tmp, k in zip(resized_tmps, range(n_tmps)):
ccorr = cv2.matchTemplate(img, rs_tmp, cv2.TM_CCORR_NORMED)
match_val, match_loc = cv2.minMaxLoc(ccorr)[1::2]
if k == 0:
best_match_val = match_val
if match_val > best_match_val:
best_match_val = match_val
best_match_loc = match_loc
best_match = k

best_match_tmp = resized_tmps[best_match]
best_match_tmp = cv2.normalize(best_match_tmp, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
best_match_tmp = cv2.merge([best_match_tmp, best_match_tmp, best_match_tmp])

h, w = best_match_tmp.shape[:2]
best_match_loc_end = (best_match_loc[0]+w, best_match_loc[1]+h)
matched = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
matched = cv2.merge([matched, matched, matched])
cv2.rectangle(matched, best_match_loc, best_match_loc_end, (0, 255, 0))

cv2.imshow('matched1.png', matched)
cv2.waitKey()

cv2.imshow('besttmp.png', best_match_tmp)
cv2.waitKey()

再一次,它有效!

Matched after auto-resizing

这是最匹配的模板:

Best matched template

通过目视检查看起来是正确的。最佳匹配模板的高度和宽度是 (85, 99)这基本上只是带有一点水平拉伸(stretch)的原始模板。

关于Python OpenCV : Using matchTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com