简体   繁体   English

如何在opencv python中将特定像素透明化?

[英]How particular pixel to transparent in opencv python?

I have image messi.jpeg .I want to replace pixel with color (0,111,111) to transparent in messi.jpeg . 我有图像messi.jpeg 。我想将颜色(0,111,111)的像素替换为messi.jpeg中的透明像素 now. 现在。 my code is give below. 我的代码在下面给出。

 img[np.where((img == [0,111,111]).all(axis = 2))] = [255,255,255]

I want transparent pixel. 我想要透明像素。 now it converted to white 现在变成白色了

For those arriving from Google the above answer may have been true at the time it was written but as the docs describe it is no longer accurate . 对于那些从Google那里来的人来说,上面的答案在撰写时可能是正确的,但正如文档所描述的那样,不再准确 Passing a negative value to imread returns an array with an alpha channel. 将负值传递给imread将返回具有alpha通道的数组。

In python this does the job: 在python中,它可以完成这项工作:

>>> im = cv2.imread('sunny_flat.png', -1)
>>> im
array([[[ 51,  44,  53, 255],
    [ 46,  40,  46, 255],
    [ 40,  31,  36, 255],
    ..., 
    [ 24,  26,  36, 255],
    [ 26,  28,  39, 255],
    [ 15,  17,  27, 255]]], dtype=uint8)
>>> im[0][0] = np.array([0,0,0,0], np.uint8)
>>> im
array([[[  0,   0,   0,   0],
    [ 46,  40,  46, 255],
    [ 40,  31,  36, 255],
    ..., 
    [ 24,  26,  36, 255],
    [ 26,  28,  39, 255],
    [ 15,  17,  27, 255]]], dtype=uint8)

OpenCV doesn't support transparency (natively), sorry. 抱歉, OpenCV不支持透明性

One approach is to add another channel to the image to represent the alpha channel. 一种方法是向图像添加另一个通道以表示Alpha通道。 However, OpenCV can't display RGBA images, so upon loading an RGBA image ignore the 4th channel to display it correctly. 但是,OpenCV无法显示RGBA图像,因此在加载RGBA图像时,请忽略第4个通道以正确显示它。

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

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