python - 如何使用pillow的python lib实现图像 channel 下降

标签 python computer-vision python-imaging-library

我需要随机将枕头图像的 0、1 或 2 个 channel 归零。这意味着我需要将图像的 0、1 或 2 个 channel 随机设置为 0。

我怎样才能用 pil 做到这一点?

最佳答案

这是一种简单的原生 PIL 方法,通过乘以变换来实现此目的。我设置了默认变换,所以数学看起来像这样:

newRed   = 1*oldRed  +  0*oldGreen  +  0*oldBlue  + constant
newGreen = 0*oldRed  +  1*OldGreen  +  0*OldBlue  + constant
newBlue  = 0*oldRed  +  0*OldGreen  +  1*OldBlue  + constant

然后我只需将 1 更改为 0,我希望 channel 归零。

#!/usr/bin/env python3

from PIL import Image

# Open image
im = Image.open('input.png').convert("RGB")

# Pre-set R, G and B multipliers to 1
Rmult, Gmult, Bmult = 1, 1, 1

# Select one (or more) channels to zero out, I choose B channel here
Bmult=0

# Make transform matrix
Matrix = ( Rmult, 0, 0, 0,
           0, Gmult, 0, 0,
           0, 0, Bmult, 0)

# Apply transform and save
im = im.convert("RGB", Matrix)
im.save('result.png')

所以,如果你从这个开始:

enter image description here

并将蓝色乘数 (Bmult) 设置为零,您将得到:

enter image description here

如果将红色和蓝色归零:

Rmult = Bmult = 0 

你会得到:

enter image description here

关于python - 如何使用pillow的python lib实现图像 channel 下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310058/

相关文章:

opencv - 检测手的算法

python - 枕头 Image.convert TypeError : argument 1 must be str, 不是 int

python - 使用crontab执行程序

machine-learning - TensorFlow:实现类加权交叉熵损失?

python - 在matplotlib颜色栏中隐藏每个第n个刻度标签的最干净方法?

python - 如何使用Python和Opencv计算叶子显微图像中的气孔数量?

python - 使用 Python/Matlab 在图像中人为地合并非刚性运动以生成数据

Python - Numpy RGB 像素数组到图像

python - 你如何将 csrf 验证添加到 Pyramid ?

python - 如何将 ssh.exec_command 的输出重定向到 Python paramiko 模块中的文件?