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.
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.
The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.
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() .
As you have already found out, the return type of plt. imshow() is a matplotlib. image.
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 ()
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.
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 ()".
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].
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()
you can directly use this line to convert the BGR image to RGB:
image = cv2.imread("path/to/file/image.jpg")[:,:,::-1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With