简体   繁体   English

我如何使用opencv python仅更改白色的黑色而无像素操作

[英]How can i change only black color in white using opencv python without pixel operation

This is the sample Image 这是示例图片

I want to change the black color in the image into white. 我想将图像中的黑色更改为白色。 I tried using threshold and bitwise operations but these operations distorts the other color present in the image. 我尝试使用阈值和按位运算,但是这些运算会扭曲图像中存在的其他颜色。 I don't want to use pixel operations as these are expensive. 我不想使用像素操作,因为这些操作很昂贵。

Apply a threshold operation on your source image using cv2.THRESH_INV argument, to ensure that only the black pixels change to white and the other pixels turn black. 使用cv2.THRESH_INV参数在源图像上应用阈值运算,以确保只有黑色像素变为白色,其他像素变为黑色。

Convert the binary mask to a 3 channel image by using: 使用以下命令将二进制蒙版转换为3通道图像:

src1_mask=cv2.cvtColor(src1_mask,cv2.COLOR_GRAY2BGR)#change mask to a 3 channel image

use cv2.addWeighted method to add the mask and the original image. 使用cv2.addWeighted方法添加遮罩和原始图像。 The black part of the mask will be replaced by the original image and the white part will remain white, since the black pixels will be approximately 0 in the addWeighted operation 蒙版的黑色部分将替换为原始图像,而白色部分将保持白色,因为在addWeighted操作中黑色像素大约为0

Load your source image as an array named 'img' and then do this: 将您的源图像加载为名为“ img”的数组,然后执行以下操作:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = cv2.compare(gray,5,cv2.CMP_LT)
img[mask > 0] = 255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM