为什么 scipy.signal.correlate2d 在这个例子中不起作用? [英] Why does scipy.signal.correlate2d fail to work in this example?

查看:86
本文介绍了为什么 scipy.signal.correlate2d 在这个例子中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对两个图像进行互相关,从而通过找到最大相关值来定位第一张图像上的模板图像.我画了一个带有一些随机形状的图像(第一张图像),然后剪下其中一个形状(模板).现在,当我使用 scipy 的 correlate2d 并在与最大值的相关性中定位点时,会出现几个点.据我所知,不应该只有一个点重叠最大吗?

I am trying to cross-correlate two images, and thus locate the template image on the first image, by finding the maximum correlation value. I drew an image with some random shapes (first image), and cut out one of these shapes (template). Now, when I use scipy's correlate2d, and locate point in the correlation with maximum values, several point appear. From my knowledge, shouldn't there only be one point where the overlap is at max?

此练习背后的想法是取图像的一部分,然后将其与数据库中的一些先前图像相关联.那么我应该能够根据相关性的最大值在较旧的图像上找到这部分.

The idea behind this exercise is to take some part of an image, and then correlate that to some previous images from a database. Then I should be able to locate this part on the older images based on the maximum value of correlation.

我的代码如下所示:

from matplotlib import pyplot as plt
from PIL import Image 
import scipy.signal as sp

img = Image.open('test.png').convert('L')
img = np.asarray(img)

temp = Image.open('test_temp.png').convert('L')
temp = np.asarray(temp)
corr = sp.correlate2d(img, temp, boundary='symm', mode='full')

plt.imshow(corr, cmap='hot')
plt.colorbar()

coordin = np.where(corr == np.max(corr)) #Finds all coordinates where there is a maximum correlation

listOfCoordinates= list(zip(coordin[1], coordin[0]))

for i in range(len(listOfCoordinates)): #Plotting all those coordinates
    plt.plot(listOfCoordinates[i][0], listOfCoordinates[i][1],'c*', markersize=5)

这产生了数字:青色星星是具有最大相关值 (255) 的点.

我希望corr"中只有一分具有相关性的最大值,但出现了几个.我尝试使用不同的关联模式,但无济于事.

I expect there to be only one point in "corr" to have the max value of correlation, but several appear. I have tried to use different modes of correlating, but to no avail.

这是我在关联时使用的测试图像.
这是模板,从原图剪下来.

任何人都可以对我在这里可能做错的事情有所了解吗?

Can anyone give some insight to what I might be doing wrong here?

推荐答案

正在申请

img = img - img.mean()
temp = temp - temp.mean()

在计算 2D 互相关之前 corr 应该给你预期的结果.

before computing the 2D cross-correlation corr should give you the expected result.

清理代码,一个完整的例子:

Cleaning up the code, for a full example:

from imageio import imread
from matplotlib import pyplot as plt
import scipy.signal as sp
import numpy as np

img = imread('https://i.stack.imgur.com/JL2LW.png', pilmode='L')
temp = imread('https://i.stack.imgur.com/UIUzJ.png', pilmode='L')

corr = sp.correlate2d(img - img.mean(), 
                      temp - temp.mean(),
                      boundary='symm',
                      mode='full')

# coordinates where there is a maximum correlation
max_coords = np.where(corr == np.max(corr))

plt.plot(max_coords[1], max_coords[0],'c*', markersize=5)
plt.imshow(corr, cmap='hot')

这篇关于为什么 scipy.signal.correlate2d 在这个例子中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆