This has probably been asked before but I couldn't find anything. Is there a efficient way to replace entries in one array with another conditionally?
For example, lets say I have 2 images (3d arrays - (x,y,3)). Lets say I want to replace the pixel value in the first array with the one in the second array, so long as it is not a given value.
Basically this, but with more efficient numpy code if possible:
for i in range(y):
for j in range(x):
if not np.array_equal(arr2[i,j], some_given_rgb_trio):
arr1[i,j] = arr2[i,j]
Btw, I understand that this is a really dumb and inefficient way to do it. But I'm not too familiar with all that is available to me through numpy and python in general. I apologize if its a bad question.
Edit: The operation is pretty similar to things done when masking, but I can't seem to find what I need in the np.ma module. One I get this mask through some conditional, I basically want to replace the values in one array with the values of the other array, where my mask is true.
Creating the mask would be pretty simple:
mask = np.getmask(np.ma.masked_equal(arr2, some_given_rgb_trio))
arr1[mask == False] = ?? #Should be the corresponding value in arr2
I can get the mask, but I don't know to then apply it back to actually overlay the images in the desired way.