与在OpenCV Python中创建RGB图像的蒙版相关的问题 [英] Issues related to creating mask of an RGB image in opencv python
本文介绍了与在OpenCV Python中创建RGB图像的蒙版相关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基于像素值创建RGB图像的蒙版,但以下代码段会引发错误
I want to create a mask of a RGB image based on a pixel value but the following code segment throws error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果有必要,我可以提供图像.
I can provide the image if it is at all required.
这是代码段
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
我想知道代码中的问题是什么?
I wonder what is the problem in the code?
推荐答案
使用for
循环遍历像素非常慢-尝试养成使用Numpy对向量进行矢量化处理的习惯.
Iterating over pixels with for
loops is seriously slow - try to get in the habit of vectorising your processing with Numpy.
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))
因此,从这张图片开始:
So, starting with this image:
您将获得此面具:
这篇关于与在OpenCV Python中创建RGB图像的蒙版相关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文