I have a numpy array of size 2000*4000 with binary values in it. I have a template that I would like to match with my source array. Currently I'm running a sliding window over the source array. Although this method works fine, it's very time consuming. I'm guessing a native implementation would be much faster. Also can it match multiple occurrences of the template?
Something like
x = [[0 0 1 1 1 1 1 0 0]
[0 0 1 1 0 0 0 0 0]
[0 0 1 1 0 0 0 0 0]
[0 0 1 1 0 0 0 0 0]]
template = [[1 1 1 1]
[1 0 0 0]
[1 0 0 0]]
cords = np.matchtemplate(x, template)
And printing the cords should ideally give a list of tuples which has the diagonal coordinates of the matching segment.
print(cords)
[[(0, 3), (6, 2)]]
As @MPA suggested, this will provide with a list of candidates:
from scipy import signal
match = np.sum(template)
tst = signal.convolve2d(x, template[::-1, ::-1], mode='valid') == match
candidates = np.argwhere(tst)
This gives (0, 2)
and (0, 3)
for your example.
For binary matrices , one can do as @Paul suggests:
from scipy import signal
match = np.sum(template)
tst = signal.convolve2d(x, (2 * template - 1)[::-1, ::-1], mode='valid') == match
positions = np.argwhere(tst)
This gives (0, 3)
for your example.
A solution which uses OpenCV:
import cv2
result = cv2.matchTemplate(
x.astype(np.float32),
template.astype(np.float32),
cv2.TM_SQDIFF)
positions = np.argwhere(result == 0.0)
This gives (0, 3)
for your example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.