Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB image display in Matplotlib: plt.imshow returns a blue image [duplicate]

I am trying to display an RGB image using matplotlib. This is the code:

# import libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt

# use opencv to load the image
image = cv2.imread("path/to/file/image.jpg", 1)

# convert it to numpy array
pixels = np.array(image)

And then, when I try to visualize the image with:

plt.imshow(pixels)
plt.show()

It returns a picture that is all blue. I don't understand why, since the image is a normal colored image. I tried with multiple images, and the problem persists. Moreover, with my laptop at work I didn't get any problem.

like image 695
Leevo Avatar asked Mar 02 '19 14:03

Leevo


People also ask

How do I show the RGB image in Matplotlib?

Tutorial: How to Display a Matplotlib RGB ImageA simple call to the imread method loads our image as a multi-dimensional NumPy array (one for each Red, Green, and Blue component, respectively) and imshow displays our image to our screen.

How do I change my color on PLT Imshow?

The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.

What is Pyplot Imshow?

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 return?

As you have already found out, the return type of plt. imshow() is a matplotlib. image.

How to display BGR image in Matplotlib instead of RGB?

You are facing this issue since you are reading the image with opencv and opencv reads and displays an image as BGR format instead of RGB color format. Whereas matplotlib uses RGB color format to display image. Try using: image = cv2.cvtColor (image, cv2.COLOR_BGR2RGB) pixels = np.array (image) plt.imshow (pixels) plt.show ()

What is imshow in matplotlib and how to use it?

Matplotlib is a library in python that is built over the numpy library and is used to represent different plots, graphs, and images using numbers. The basic function of Matplotlib Imshow is to show the image object. As Matplotlib is generally used for data visualization, images can be a part of data, and to check it, we can use imshow.

How do I display an image in Matplotlib?

Matplotlib 's built in function imshow will let you do this. import matplotlib.pyplot as plt def displayImage (image): plt.imshow (image) plt.show () If you aren't saving the image with matplotlib, but want to open the image, you need to add in "plt.show ()".

Why are OpenCV images blue in color?

OpenCV represents the images in BGR as opposed to the RGB we expect. Since it is in the reverse order, you tend to see the blue color in images. Try using the following line (below comment in code) for converting from BGR to RGB: You can also reverse the channels yourself using NumPy syntax: image [...,::-1].


2 Answers

You are facing this issue since you are reading the image with opencv and opencv reads and displays an image as BGR format instead of RGB color format. Whereas matplotlib uses RGB color format to display image. Try using:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

pixels = np.array(image)

plt.imshow(pixels)
plt.show()
like image 93
Apoorv Mishra Avatar answered Oct 07 '22 10:10

Apoorv Mishra


you can directly use this line to convert the BGR image to RGB:

image = cv2.imread("path/to/file/image.jpg")[:,:,::-1]
like image 20
Akhil prasannan Avatar answered Oct 07 '22 11:10

Akhil prasannan