Reputation: 1
I have a three channel image (color image) and I want to use fancy indexing to find pixels of a specific color and then manipulate the color value.
Let's say I want to change every black pixel (0, 0, 0) to be blue (0, 0, 255)
Without fancy indexing it would work like this (and it works):
for x in range(0, width):
for y in range(0, height):
if (image[y, x] == (0, 0, 0)).all():
image[y, x] = (0, 0, 255)
So for fancy indexing I tried this:
image[(image[:, :] == (0, 0, 0)).all()] = (0, 0, 255)
But it does not work because it checks the condition for every single channel instead of the whole "channel tuple". It also didn't work without the .all() or other variations I tried. I somehow need to specify to not further unfold the array after the first two dimensions before evaluating the condition (thats what I tried with the image[:,:] but it does nothing...) Is there a way to solve it with fancy indexing? Since the other solution is very slow.
Upvotes: 0
Views: 56
Reputation: 50453
.all()
reduce the whole array to 1 scalar value. In your case, you just want to reduce each pixel to a scalar (ie. to reduce values along the last axis). You can do that by specifying the axis:
image[(image[:, :] == (0, 0, 0)).all(axis=2)] = (0, 0, 255)
Upvotes: 1