The difference in Reading an image using OpenCV and Matplotlib
In My previous blog, I explained about reading images using OpenCV grayscale and color images, Now In this blog, I am going to explain How Matplotlib and OpenCV reads an image and what is the difference while reading an image.
First will start with Matplotlib, Matplotlib Library uses the imread method to read an image and the imshow method to show an image.
import matplotlib.pyplot as plt
image_mat_gray = plt.imread('./images/mandril_gray.tif')
print(image_mat_gray.shape)
plt.imshow(image_mat_gray)
The above code shows the output in the form of heat represented manner
but we need to understand here, we read the grayscale image only but it’s showing heat related image to overcome this we need to use a parameter called cmap = ‘gray’ which shows the original grayscale image.
import matplotlib.pyplot as plt
image_mat_gray = plt.imread('./images/mandril_gray.tif')
print(image_mat_gray.shape)
plt.imshow(image_mat_gray,cmap='gray')
Matplotlib for color images
It’s quite easy to read color images by using imread and imshow
image_mat_gray = plt.imread('./images/mandril_color.tif')
print(image_mat_gray.shape)
plt.imshow(image_mat_gray)
shape will be (512,512,3) which means 512 rows and 512 columns and 3 refers to RGB channels.
OpenCV for grayscale Images
image_gray = cv2.imread('./images/mandril_gray.tif' , 0) # reading an image
print(image_gray.shape)
cv2.imshow('first_image' , image_gray) # showing an image
cv2.waitKey() # waiting
cv2.destroyAllWindows() # till pressing anything from keyboard
in the imread second parameter 0-Grayscale image
OpenCV for color images
image_color = cv2.imread('./images/mandril_color.tif' , 1) # 1 for color image
print(image_color.shape)
cv2.imshow('first_image' , image_color) # showing an image
cv2.waitKey() # waiting
cv2.destroyAllWindows() # till pressing anything from keyboard
Difference Between OpenCV and Matplotlib for grayscale and color images
For Grayscale image, there will be no issues which mean if we read an image using OpenCV using imread and if we show an image using matplotlib imshow then the image will be the same.
import cv2
import matplotlib.pyplot as plt
image_gray = cv2.imread('./images/mandril_gray.tif',0) # reading an image using opencv
print(image_gray.shape)
plt.imshow(image_gray , cmap = 'gray') # showing an image using matplotlib
but for color images, it is quite different which means if we read a color image using matplotlib or OpenCV and if we try to show the image there is a difference, Now let us see if we will read a color image using OpenCV and will try to show it using matplotlib imshow then what happens.
image = cv2.imread('./images/mandril_color.tif',1) # reading an color image using Opencv
print(image.shape)
plt.imshow(image) # showing using matplotlib
The image is not matching to the original image, Now let’s try reading an image using matplotlib and showing the image using OpenCV.
image = plt.imread('./images/mandril_color.tif') # reading an color image
print(image.shape)
cv2.imshow('first_image' , image) # showing an image
cv2.waitKey() # waiting
cv2.destroyAllWindows() # till pressing anything from keyboard
Trying in both ways but the results are not matching the original image,
Then what is the issue? Don’t worry let us try to solve it.
matplotlib reads a color image in RGB manner only but OpenCV reads the color image in BGR format so when reading an image using OpenCV and showing using matplotlib we need to change the channels,
Let’s try it out, reading images using OpenCV and changing channels using matplotlib.
image = cv2.imread('./images/mandril_color.tif',1) # reads an image in BGR format
print(image.shape)
plt.imshow(image[: , : , ::-1]) # : selecting entire rows, : entire columns and ::-1 changing BGR to RGB
plt.imshow(image[: , : , ::-1])
first: -> represents entire rows in the image
second: -> represents entire columns in the image
third::-1 -> represents selecting entire rows and entire columns and
reverse the channels
ex: BGR - if we reverse it - RGB
for complete information about grayscale and color image, You can follow my previous blog https://medium.com/@korlakuntasaikamal10/gray-scale-and-color-images-using-opencv-eda0c13c292a
You can get the complete code from my GitHub repository https://github.com/saikamal3344/OpenCV-Image-Processing-