问题描述
我已为此任务设置好:
我不知道下面的代码有什么问题:
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()
这应该产生一个灰度图。