9

I am trying to use matplotlib for basic operations but I see that whenever I try to display an image using matplotlib, a blue shade is added to the image.

For example,

Code:

# import the necessary packages
import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("./data/images/cat_and_dog.jpg")
cv2.imshow('image',image) # Display the picture


plt.imshow(image)
plt.show()

cv2.waitKey(0) # wait for closing
cv2.destroyAllWindows() # Ok, destroy the window

And the images shown by opencv gui and matplotlib are different.

enter image description here

Even the histogram is distorted and not just the display

enter image description here

How do I avoid this blue shade on an image

Anoop
  • 5,540
  • 7
  • 35
  • 52

2 Answers2

14

see: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html#using-matplotlib

Warning: Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.

you can replace your plt.imshow(image) line with the following lines:

im2 = image.copy()
im2[:, :, 0] = image[:, :, 2]
im2[:, :, 2] = image[:, :, 0]
plt.imshow(im2)
Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
4

Complementing @ophir answer. You can also use cv2.cvtColor to convert from BGR to RGB

Fred Guth
  • 1,537
  • 1
  • 15
  • 27