如何使用matplotlib将灰度图像转换为亮度图像?

问题描述

我已为此任务设置好:

我不知道下面的代码有什么问题:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import cm
from matplotlib.colors import ListedColormap,LinearSegmentedColormap

img = mpimg.imread('Ca.PNG')
imgplot = plt.imshow(img)
img = mpimg.imread('Ca.PNG')
print(img)
lum_img = img[:,:,0]
plt.imshow(lum_img)
plt.show()
print(lum_img)

解决方法

您的代码看起来不错,也许有点混乱。也许添加一些评论,以帮助您跟踪和思考正在做的事情。例如...

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load the image.
img = mpimg.imread('Ca.PNG')

# Take the red channel.
lum_img = img[:,:,0]

# Plot the single-channel array.
plt.imshow(lum_img,cmap='gray')
plt.show()

这应该产生一个灰度图。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...