简体   繁体   中英

get grayscale image from jpeg

I have this jpeg image

在此处输入图像描述

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. 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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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