RGB 转灰度#

此示例将具有 RGB 通道的图像转换为具有单个灰度通道的图像。

每个灰度像素的值计算为对应的红色、绿色和蓝色像素的加权总和,如下所示

Y = 0.2125 R + 0.7154 G + 0.0721 B

这些权重由 CRT 荧光粉使用,因为它们比相等权重更好地代表人眼对红色、绿色和蓝色的感知。[1]

参考文献#

Original, Grayscale
import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2gray

original = data.astronaut()
grayscale = rgb2gray(original)

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].imshow(original)
ax[0].set_title("Original")
ax[1].imshow(grayscale, cmap=plt.cm.gray)
ax[1].set_title("Grayscale")

fig.tight_layout()
plt.show()

脚本的总运行时间:(0 分钟 0.757 秒)

由 Sphinx-Gallery 生成的图库