【发布时间】:2022-11-19 08:46:12
【问题描述】:
以下代码段将创建一个测试图像
# Create 3x3x3 image
test_image = []
for i in range(9):
if i < 6:
image.append([255, 22, 96])
else:
image.append([255, 0, 0])
出去:
array([[[255, 22, 96],
[255, 22, 96],
[255, 22, 96]],
[[255, 22, 96],
[255, 22, 96],
[255, 22, 96]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0]]], dtype=int32)
我的目标是为每个零点创建一个单通道图像 [255, 22, 96] 在 test_image 中,我想在新的 single_channel 图像中设置数字 100:
我尝试了以下内容:
test_image = np.array(test_image)
height, width, channels = test_image.shape
single_channel_img = np.zeros(test_image.shape, dtype=int)
msk = test_image == [255, 22, 96] # DOES NOT WORK AS EXPECTED
single_channel_img[msk] = 100
这不起作用,因为屏蔽的结果:
msk = test_image == [255, 22, 96]
回报:
array([[[ True, True, True],
[ True, True, True],
[ True, True, True]],
[[ True, True, True],
[ True, True, True],
[ True, True, True]],
[[ True, False, False],
[ True, False, False],
[ True, False, False]]])
为什么最后 3 个像素的掩码返回 True,我如何确保只有当所有 3 个值都相同时比较才返回 True。我的假设是,当所有 3 个 RGB 值都等于 [255、22、96] 时,我屏蔽的方式应该只返回 True。
【问题讨论】:
-
Numpy 不知道像素。它正在一个一个地比较数组元素。您可以使用
all来减少它。我相信msk.all(axis=2)应该这样做。如果所有部分都为真,则返回真。
标签: python image numpy opencv rgb