我想基于像素值创建RGB图像的蒙版,但是以下代码段引发错误
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果有必要,我可以提供图像。
这是代码段
image = cv2.imread("abcd.png")
for k in range(image.shape[0]):
for l in range(image.shape[1]):
if(image[k][l]==[255,255,255]):
mask[k][l]=255
else:
mask[k][l]=0
我想知道代码中的问题是什么?
最佳答案
使用for
循环遍历像素非常慢-尝试养成使用Numpy对 vector 进行矢量化处理的习惯。
import numpy as np
import cv2
# Load image
image = cv2.imread("start.png")
# Mask of white pixels - elements are True where image is White
Wmask =(im[:, :, 0:3] == [255,255,255]).all(2)
# Save as PNG
cv2.imwrite('result.png', (Wmask*255).astype(np.uint8))
因此,从此图像开始:
您将获得此面具:
关于python - 与在OpenCV Python中创建RGB图像的 mask 有关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785080/