'How to detect specific pixel shapes?
Solution 1:[1]
You can use cross correlation scipy.signal.correlate to do template matching. e.g. if you have a template (like the llama in this example) and want to find its position in another image (the canvas), you can cross correlate the two images. You will get maxima (can be multiple) where the same template is found. Finding the maxima will tell you the position of your template.
import scipy.signal
xcorr = scipy.signal.correlate(canvas, pic)
xmax, ymax = np.where(xcorr == xcorr.max())
fig, ax = plt.subplots(1, 3, figsize=(10, 6))
ax[0].imshow(pic)
ax[1].imshow(canvas)
ax[2].imshow(xcorr)
circle1 = plt.Circle((xmax, ymax), 50, color='r', fill=False)
ax[2].add_patch(circle1)
titles = ['Template', 'Canvas', '2D Cross Correlation']
for a, t in zip(ax, titles):
a.set_title(t)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dzang |


