Reputation: 409
I have an image(2D array) with 3 color channels. Something like this:
[[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]
[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]
[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]
...
[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]
[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]
[[128 197 254]
[128 197 254]
[128 197 254]
...
[182 244 255]
[182 244 255]
[182 244 255]]]
I want to get the indexes of the colors that are [255, 255, 255] for example. I tried using np.where()
or np.argwhere()
, but it compared values not arrays. What is the fastest and most efficient way to do it?
Upvotes: 2
Views: 405
Reputation: 10366
A numpy way to do this with np.where would be
import numpy as np
# Generating an example array
width = 100
height = 100
channels = 3
img = np.random.rand(width, height, channels) * 255
# Defining the three value channels
r=0
g=1
b=2
# Defining the query values for the channels, here [255, 255, 255]
r_query = 255
g_query = 255
b_query = 255
# Print a 2D array with the coordinates of the white pixels
print(np.where((img[:,:,r] == r_query) & (img[:,:,g] == g_query) & (img[:,:,b] == b_query)))
This gives you a 2D-Array with the coordinates of the white pixels [255, 255, 255] in your original array (image).
Note: Another way would be using OpenCV
mask = cv2.inRange(img, [255, 255, 255], [255, 255, 255])
output = cv2.bitwise_and(img, img, mask = mask)
Upvotes: -1
Reputation: 59264
IIUC, you may use np.nonzero
np.nonzero((arr==255).all(axis=2))
That will return a tuple of arrays, which represent the indexes. If you do
arr[ind]
where ind
is the return from the first expr, you may access/modify all rows with all 255.
Upvotes: 3