I have a matrix with these values (if it helps, this an rgb image):
mat = np.array([
[[0, 0, 0], [123, 0, 255], [0, 0, 0]],
[[0, 0, 0], [123, 0, 255], [45, 0, 54]],
[[0, 0, 0], [100, 0, 100], [45, 0, 54]],
])
I'm trying to find index locations of a pattern like:
pattern = np.array([
[[123, 0, 255], [*, *, *]],
[[*, *, *], [45, 0, 54],
])
So in this example, I'm trying to find the coordinates of [0, 1] and [1, 1]. I can do this with simple nested list iteration, but I'm trying to do this as quickly as possible and hoping there's some numpy method that will let me do this efficiently.
What's the fastest way to accomplish this?
Bonus points if the answer also allows me to specify value ranges, so instead of looking for the exact values of [123, 0, 255], I can look for [120-125, 0, 255].
EDIT
To clarify, here's a working example. I'm looking for some faster method.
mat = np.array([
[[0, 0, 0], [123, 0, 255], [0, 0, 0]],
[[0, 0, 0], [123, 0, 255], [45, 0, 54]],
[[0, 0, 0], [100, 0, 100], [45, 0, 54]],
])
# Look for any any 2x2 sub-matrix such that the upper left pixel is [123, 0, 255]
# and the lower right pixel is [45, 0, 54].
pixels_to_match = [
(0, 0, [123, 0, 255]),
(1, 1, [45, 0, 54]),
]
results = []
for row_ix, row in enumerate(mat):
for col_ix, col in enumerate(row):
view = mat[row_ix:row_ix+2, col_ix:col_ix+2]
found = True
for vals in pixels_to_match:
if not (view[vals[0], vals[1]] == vals[2]).all():
found = False
break
if found:
results.append((row_ix, col_ix))
print(results)