0

I have this following function to plot any image.

def draw_image(img, title, height, width):
    plt.figure(figsize = (height,width))  
    plt.title(title)
    imgplot = plt.imshow(img)

Now I want to plot a normal RGB image and its Grayscale version using this function. I have used the rgb2gray() function from scikit-image to convert the main image into grayscale. Then I have sent both of them to the function I previously mentioned. I have written this code.

grayscale = rgb2gray(img)

draw_image(img, "Original", 5, 5)
draw_image(grayscale, "Grayscale", 5, 5)

But surprisingly this produces the following images.

enter image description here enter image description here

This is not what I expected. This has not produced a correct grayscale image. So I have tried to change my code a bit. I have changed the function a bit. The changed function looks like this.

def draw_image(img, title, height, width):
    plt.figure(figsize = (height,width))  
    plt.title(title)
    imgplot = plt.imshow(img, 'gray')

Now this two pictures have been produced.

enter image description here enter image description here

Though this code is producing the correct output, I cannot understand what is going on. Why did the first function produce such a greenish image as grayscale? Why did the second one produce the actual grayscale image? Why is the original image at both cases same? Is there anything wrong with rgb2gray function? Can anyone explain the right procedure to show an image and convert it into a grayscale image?

Protik Nag
  • 511
  • 5
  • 20
  • https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib-pyplot-imshow – wwii Oct 17 '19 at 22:25
  • 1
    The default colormap for scalar image data is "viridis", which maps the lowest value to dark purple and the highest value to yellow. To show scalar image data as grayscale requires to use a gray colormap - which is what you did in the second case. – ImportanceOfBeingErnest Oct 17 '19 at 22:39

0 Answers0