Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between plt.show and cv2.imshow?

Tags:

Why is there a difference in the output image when calling the same image using plt.show & cv2.imshow()?

Here is my code:

import cv2 import numpy as np from matplotlib import pyplot as plt  src=cv2.imread('fruits1.jpg') # Source image  plt.subplot(211),plt.imshow(src),plt.title('image') plt.xticks([]),plt.yticks([]) plt.show()  cv2.imshow('image',src) cv2.waitKey(0) cv2.destroyWindow() 

Here is the image from plt.show:

image output for plt.show

and the second one is the original image:

image output from cv2.show

Is there some modification required with the plt.show()?

like image 543
Aman Garg Avatar asked Jul 26 '16 19:07

Aman Garg


People also ask

What is cv2 Imshow?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size. Syntax: cv2.imshow(window_name, image)

What is PLT Imshow?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .

What is Imshow used for?

imshow( I ) displays the grayscale image I in a figure. imshow uses the default display range for the image data type and optimizes figure, axes, and image object properties for image display.


2 Answers

Because OpenCV stores images in BGR order instead of RGB.

Try plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

See here for an example.

like image 171
laks Avatar answered Sep 18 '22 07:09

laks


                            OpenCV - BGR and Matplotlib - RGB 

OpenCV:

https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html

enter image description here

Matplotlib:

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html

enter image description here

like image 45
Code J Avatar answered Sep 18 '22 07:09

Code J