我有图片 messi.jpeg 。我想在 messi.jpeg 中将带有颜色的像素(0,111,111)替换为透明。 现在。我的代码在下面给出。
img[np.where((img == [0,111,111]).all(axis = 2))] = [255,255,255]
我想要透明像素。现在它转换为白色
答案 0 :(得分:4)
对于那些从谷歌来的人来说,上面的答案在编写时可能是正确的as the docs describe it is no longer accurate。将负值传递给imread将返回带有Alpha通道的数组。
在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)
答案 1 :(得分:3)
OpenCV不支持透明度(原生),抱歉。
一种方法是add another channel to the image来表示alpha通道。但是,OpenCV无法显示RGBA图像,因此在加载RGBA image ignore the 4th channel时正确显示它。