Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show grayscale OpenCV image with matplotlib

I use opencv3 of python installed it by anaconda using:
conda install -c menpo opencv3=3.2.0

But when i use it to convert a picture to grayscale, like:

import cv2
import matplotlib.pyplot as plt

%matplotlib inline

image = cv2.imread('opencv_logo.png')

image1 = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

print (image.shape)
print (image1.shape)

plt.imshow(image1)

enter image description here I donnot know why:

I use windows + miniconda. Can anyone know why and help me ? Thanks.

like image 275
Kerwin Xiao Avatar asked Apr 05 '17 10:04

Kerwin Xiao


People also ask

How do I show grayscale image in Matplotlib?

To display a grayscale image in Matplotlib, we use the matplotlib. pyplot. imshow() with parameters cmap set to 'gray' , vmin set to 0 and vmax set to 255 . By default, the value of cmap , vmin and vmax is set to None .

How do I grayscale an image in OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do I read an image in grayscale in Python?

Convert an Image to Grayscale in Python Using the cv2. imread() Method of the OpenCV Library. Another method to get an image in grayscale is to read the image in grayscale mode directly, we can read an image in grayscale by using the cv2. imread(path, flag) method of the OpenCV library.

What is PLT gray () in Python?

matplotlib.pyplot. gray ()[source] Set the colormap to "gray". This changes the default colormap as well as the colormap of the current image if there is one.


1 Answers

You are supposed to add another parameter in plt.imshow() so as to mention that you want a gray scale image.

Modify the last line like this: plt.imshow(img, cmap='gray')

Upon doing so I got the following:

enter image description here

like image 198
Jeru Luke Avatar answered Sep 20 '22 23:09

Jeru Luke