This:
summing (equivalent to and in bool)
is untrue. "And" is equivalent to multiplication.
As for your code:
Simplification
is it possible to reduce the complexity of the matching part of my code ?
Yes, extremely. I think the algorithm may have been overthought. For example, you go through the trouble of building a zero collection but then never use it. This really boils down to
- find the subset of pixel coordinates for which both the left and right images have foreground (are non-zero);
- reduce to a map from left value to right value at those coordinates;
- demote to background anything not in the map; and
- apply the map.
The results are the same:
def main() -> None:
images = make_samples()
show(images, 'Before matching')
both_foreground = np.logical_and.reduce(images, axis=0)
mapping = np.unique(images[:, both_foreground], axis=1)
for image, value_set in zip(images, mapping):
image[~np.isin(image, value_set)] = 0
for right, left in mapping:
images[1, images[1,:] == right] = left
show(images, 'After matching')
plt.show()