NumPy/SciPy: 在图像上移动掩码并检查相等性

6 投票
1 回答
1283 浏览
提问于 2025-04-16 18:46

我正在尝试使用 NumPyscipy 进行图像处理。我有一张模板图像,它对应于一个背景。我想找到输入图像中所有出现模板图像的位置,并把输出数组中相应的位置设置为1,其他位置设置为0。我该怎么做呢?

1 个回答

3

你可以使用scipy.ndimage.correlate这个工具,把你的模板和图片进行对比。然后找出那些亮点,这些亮点就是你要找的匹配部分。举个例子:

import scipy.ndimage
from numpy import mean, std

# a, b contain image and template in numpy arrays
correlation = scipy.ndimage.correlate(a, b)
matches = (correlation-mean(correlation)) > 5*std(correlation) # tune depending on level of noise

撰写回答