从 jpeg 获取灰度图像

get grayscale image from jpeg

我有这张 jpeg 图片

我想将其转换为灰度图像。 但是申请的时候

img = cv2.imread(filename)
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

我得到这张图片,它不是灰度图...

为什么我没有得到灰度图像?我怎样才能得到它?

看起来您可能正在使用 matplotlib 来显示您的图片。执行此操作时,请确保使用灰色颜色图。默认值为 Viridis(请参阅文档),这是您看到的蓝黄色颜色图

plt.imshow(grey_img, cmap = "gray")
plt.show()

imshow docs中所述,当您传递二维数组时,将根据颜色图添加伪彩色:

The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image. For displaying a grayscale image set up the colormapping using the parameters cmap='gray', vmin=0, vmax=255.