我试着遵循关于模板匹配的scikit图像教程(请检查它这里)。

使用这个例子,我想在图像中找到所有匹配的硬币(极大值),而不仅仅是这个给出最高分数的硬币。我在考虑用:
maxima = argrelextrema(result, np.greater)但问题是,它也发现了很小的局部最大值,这只是一个噪音。有没有任何方法来筛选numpy数组并找到最强的maxima?谢谢!
发布于 2018-02-12 14:10:42
我一直在挖掘,并找到了一些解决方案,但不幸的是,我不确定我是否知道在脚本中到底做了什么。我稍微修改了脚本,找到了这里
neighborhood_size = 20 #how many pixels
threshold = 0.01 #threshold of maxima?
data_max = filters.maximum_filter(result, neighborhood_size)
maxima = (result == data_max)
data_min = filters.minimum_filter(result, neighborhood_size)
diff = ((data_max - data_min) > threshold)
maxima[diff == 0] = 0
x_image,y_image = [], []
temp_size = coin.shape[0]
labeled, num_objects = ndimage.label(maxima)
slices = ndimage.find_objects(labeled)
x, y = [], []
for dy,dx in slices:
x_center = (dx.start + dx.stop - 1)/2
x.append(x_center)
y_center = (dy.start + dy.stop - 1)/2
y.append(y_center)
fig, (raw,found) = plt.subplots(1,2)
raw.imshow(image,cmap=plt.cm.gray)
raw.set_axis_off()
found.imshow(result)
found.autoscale(False)
found.set_axis_off()
plt.plot(x,y, 'ro')
plt.show()做这件事:

我还意识到,与原始图像相比,发现的峰的坐标被移动。我认为区别在于模板的大小。我会更新时,我会发现更多。
编辑:只要稍微修改代码,我就可以在输入图像上找到位置:
x_image_center = (dx.start + dx.stop - 1 + temp_size) / 2
x_image.append(x_image_center)
y_image_center = (dy.start + dy.stop - 1 + temp_size) / 2
y_image.append(y_image_center)

发布于 2018-10-12 19:02:05
为了找到所有的硬币,文档建议"...you应该使用适当的找峰功能。“其中最简单的可能是peak_local_max (如注释中所建议的),它也来自skimage,并且有一个手动页面这里。在*args中使用一些合理的数字可以从响应图像中获取峰值。
文档中也讨论了关于正在移位的山峰的第二个评论。
注意,match_template输出的峰值对应于模板的原点(即左上角)。
我们可以手动纠正这一点(通过将峰值按模板的边长进行转换),或者您可以将pad_input bool设置为True (来源),这意味着响应函数中的峰值在最大重叠点上与模板中心一致。
将这两个部分组合到一个脚本中,我们可以得到如下内容:
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from skimage.feature import match_template
from skimage.feature import peak_local_max # new import!
image = data.coins()
coin = image[170:220, 75:130]
result = match_template(image, coin,pad_input=True) #added the pad_input bool
peaks = peak_local_max(result,min_distance=10,threshold_rel=0.5) # find our peaks
# produce a plot equivalent to the one in the docs
plt.imshow(result)
# highlight matched regions (plural)
plt.plot(peaks[:,1], peaks[:,0], 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)

https://stackoverflow.com/questions/48732991
复制相似问题