简体   繁体   English

从jpeg获取灰度图像

[英]get grayscale image from jpeg

I have this jpeg image我有这个 jpeg 图片

在此处输入图像描述

I want to convert it to a grayscale image.我想将其转换为灰度图像。 But when applaying但是玩的时候

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

I get this image, which is not a grayscale image...我得到这张图像,它不是灰度图像......

在此处输入图像描述

Why I didn't get a grayscale image?为什么我没有得到灰度图像? How can I obtained it?我怎样才能得到它?

Looks like you're probably using matplotlib to display your image.看起来您可能正在使用matplotlib来显示您的图像。 When you do that, make sure you use a grey colormap.执行此操作时,请确保使用灰色颜色图。 The default is Viridis (see docs), which is the blue-yellow colormap you're seeing默认值为Viridis (请参阅文档),这是您看到的蓝黄色颜色图

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

As noted in the imshow docs , pseudocolor will be added according to the colormap when you pass a 2D array:imshow docs中所述,当您传递 2D 数组时,将根据颜色图添加伪色:

The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image.输入可以是实际的 RGB(A) 数据,也可以是 2D 标量数据,它们将被渲染为伪彩色图像。 For displaying a grayscale image set up the colormapping using the parameters cmap='gray', vmin=0, vmax=255.为了显示灰度图像,使用参数 cmap='gray'、vmin=0、vmax=255 设置颜色映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM