opencv python中透明的特定像素如何?

ope*_*guy 0 python opencv image-processing python-2.7

我有图像messi.jpeg。我想在messi.jpeg中用颜色 (0,111,111) 将像素替换为透明。现在。我的代码在下面给出。

 img[np.where((img == [0,111,111]).all(axis = 2))] = [255,255,255]
Run Code Online (Sandbox Code Playgroud)

我想要透明像素。现在它转换为白色

小智 5

对于那些来自 Google 的人,上述答案在撰写时可能是正确的,但正如文档所描述的那样,它不再准确。将负值传递给 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)
Run Code Online (Sandbox Code Playgroud)