Images is not converting to gray scale

Viewed 339

I have read all the images in a folder through OpenCV. Then converting to gray scale, using pyplot I have shown all the images. But instead of grayscale images look like yellowish.

import glob
import cv2
import matplotlib.pyplot as plt
from skimage.color import rgb2gray

images = [cv2.imread(file) for file in glob.glob(r"C:\Users\USER\Handcrafted dataset/*.jpg")]
for img in images:
    img = rgb2gray(img)
    plt.figure(figsize=(10,10))
    plt.imshow(img)

Sample output:

enter image description here

What should I do to convert them to proper grayscale images?

1 Answers

This happens because Matplotlib uses a colormap when plotting a single channel. In their docs there are a statement saying:

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

So, change your last line to:

plt.imshow(img, cmap='gray')

This will plot the channel using the gray mapping.

Related