What is the difference between plt.show and cv2.imshow in Matplotlib?

MatplotlibPythonData Visualization

A 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 on the screen. Whereas, cv2 represents RGB images as multi-dimensional NumPy arrays, but in reverse order.

Steps

  • Set the figure size and adjust the padding between and around the subplots.

  • Initialize the filename.

  • Add a subplot to the current figure using nrows=1, ncols=2, and index=1.

  • Read the image using cv2.

  • Off the axes and show the figure in the next statement.

  • Add a subplot to the current figure using nrows=1, ncols=2, and index=2.

  • Read the image using plt.

  • Off the axes and show the figure in the next statement.

  • To display the figure, use show() method.

Example

import cv2
from matplotlib import pyplot as plt, image
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
filename = "bird.jpg"
plt.subplot(121)
img = cv2.imread(filename)
plt.axis("off")
plt.imshow(img)
plt.title("with cv2")
plt.subplot(122)
img = image.imread(filename)
plt.axis("off")
plt.imshow(img)
plt.title("with plt")
plt.show()

Output

raja
Published on 01-Jun-2021 11:59:43
Advertisements