我如何使用 `np.where()` 来比较数组而不是单个值
How do I use `np.where()` to compare the arrays not individual values
我有一个带有 3 个颜色通道的图像(二维数组)。像这样:
[[[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]]]
我想获取颜色的索引,例如 [255, 255, 255]。我尝试使用 np.where()
或 np.argwhere()
,但它比较的是值而不是数组。最快最有效的方法是什么?
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)))
这为您提供了一个二维数组,其坐标为原始数组(图像)中的白色像素 [255, 255, 255]。
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)
IIUC,你可以用np.nonzero
np.nonzero((arr==255).all(axis=2))
这将 return 一个数组元组,代表索引。如果你这样做
arr[ind]
其中 ind
是来自第一个表达式的 return,您可以 access/modify 所有行都是 255。
我有一个带有 3 个颜色通道的图像(二维数组)。像这样:
[[[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]]]
我想获取颜色的索引,例如 [255, 255, 255]。我尝试使用 np.where()
或 np.argwhere()
,但它比较的是值而不是数组。最快最有效的方法是什么?
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)))
这为您提供了一个二维数组,其坐标为原始数组(图像)中的白色像素 [255, 255, 255]。
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)
IIUC,你可以用np.nonzero
np.nonzero((arr==255).all(axis=2))
这将 return 一个数组元组,代表索引。如果你这样做
arr[ind]
其中 ind
是来自第一个表达式的 return,您可以 access/modify 所有行都是 255。