scipy.signal.correlate2d似乎没有按预期工作

2024-05-14 00:41:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将两个图像进行交叉关联,从而通过找到最大相关值来定位第一个图像上的模板图像。 我画了一些随机形状的图像(第一个图像),并剪下这些形状中的一个(模板)。现在,当我使用scipy的correlate2d,并用最大值定位关联中的点时,会出现几个点。据我所知,重叠不应该只有一个点在最大值吗?

这个练习背后的想法是获取图像的一部分,然后将其与数据库中以前的一些图像关联起来。然后我应该能够根据最大的相关值在旧图像上定位这个部分。

我的代码如下所示:

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)

由此得出的结果是: Cyan stars are points with max correlation value (255)

我希望“corr”中只有一个点具有最大的相关值,但是有几个点出现了。我尝试过使用不同的关联模式,但是没有用。 This is the test image i use when correlatingThis is the template, cut from the original image

有谁能告诉我我在这里做错了什么吗?


Tags: thefrom定位test图像imageimportimg
2条回答

申请

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

在计算二维互相关之前,corr应该会给你预期的结果。在

你可能已经溢出了numpy类型的uint8。 尝试使用:

img = np.asarray(img,dtype=np.float32)
temp = np.asarray(temp,dtype=np.float32)

未经测试。在

相关问题 更多 >