I have an np.array
which is essentially an image in RGB format. I reshaped it to be more easy to work with. So, its shape is something like: (22618, 3)
with each row corresponding to a pixel. I want to check if there are some pixels that correspond to a specific color(s) and also how many of them are of this color(s).
Let's say colors are (0, 255, 255)
and (255, 0, 0)
defined in a tuple. Also the np.array that contains the data is data
(truly original!).
When I try:
any(data[(data2 == colors[0])])
it works as expected. If there are any pixels (triplets) corresponding to triplet of colors[0]
it returns True
otherwise False
.
But when I try something like:
np.sum(data[(data == colors[1])])
the result is summing up all pixel values (R+G+B) of each pixel found to contain the same color.
I used just the array containing the boolean values (data == colors[1]
) with little success. Also, I tried summing to a specific axis does not help that much:
np.sum(data == colors[1], axis=0)
array([ 373, 22618, 22618])
I could do some tricky maths here knowing that at most 373
pixels could be of the correct color but it's not precise calculation. I could use some functionality like bincount
in this question but the results seem to need extra work and also some experiments did not show to work as expected.
So, how do I count the number of pixels that have the same color? Any ideas?