Display an OpenCV image in Python with Matplotlib?

Display an OpenCV image in Python with matplotlib.

 

This will tell about how to display an image using OpenCV and Matplotlib. It will integrate OpenCV and Matplotlib libraries to showcase images in your Python projects. It will provide clear instructions and code snippets to help you visualize images effectively.

Goal: Learn how to show an image on your computer screen using Python, OpenCV, and Matplotlib.

What You Need:

  • Python: A popular programming language.
  • OpenCV: A tool that helps you work with images.
  • Matplotlib: A tool that helps you make pictures and graphs.

Firstly, when we want to display any image in Python, we have some steps in matplotlib, they are:

  • Import Libraries
  • Load Image
  • Display Image

Steps to Follow:

 

  • Install the Libraries: Before we begin, make sure you have both OpenCV and Matplotlib installed on your computer. You can do this using pip, which is a tool to install Python packages. Open your command prompt (Windows) or terminal (Mac/Linux) and type these commands:
pip install opencv-python
pip install matplotlib

This will download and install the libraries you need.

  • Import the Libraries: Next, we need to import the libraries into our Python script. This is like telling Python which tools we will use. Open your Python editor or IDE (like IDLE, PyCharm, or any other you prefer) and write:
    import cv2  # This imports OpenCV
    import matplotlib.pyplot as plt  # This imports Matplotlib

     

  • Read the Image: Now, we will read an image from our computer using OpenCV. Suppose we have an image file named ‘example.jpg’ in the same folder as our Python script. We will load this image into our program.
    image = cv2.imread('example.jpg')

    The cv2.imread function reads the image and stores it in a variable called image. Make sure the image file is in the same folder as your script, or provide the full path to the image file.

  • Convert the Image Color: OpenCV reads images in BGR (Blue, Green, Red) format, but Matplotlib displays images in RGB (Red, Green, Blue) format. So, we need to convert the image from BGR to RGB. This can be done using the cv2.cvtColor function.
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    This converts the color format of the image and stores the result in image_rgb.

  • Display the Image: Now, we can display the image using Matplotlib. We will use the plt.imshow function to show the image and plt.axis('off') to hide the axes (the x and y lines around the image).
    plt.imshow(image_rgb)
    plt.axis('off')  # This hides the x and y axes
    plt.show()  # This displays the image

Where

  •  ‘cv2’: This library provides functions for image processing, computer vision, and machine learning. It’s commonly used for tasks like reading, writing, and manipulating images.
  • ‘matplotlib.pyplot’: This module from the Matplotlib library library provides a MATLAB-like interface for creating plots and visualizations in Python. It’s particularly used for displaying images loaded with the OpenCV, as it offers versatile plotting capabilities.
               Load Image:

We have to use an OpenCV to load the image you want to display.

image = cv2.imread('your_image.jpg')
               Display Image:

Now display the loaded image using Matplotlib.

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

Complete Code:

Here is the complete code to display an image using OpenCV and Matplotlib:

import cv2  # This imports OpenCV
import matplotlib.pyplot as plt  # This imports Matplotlib

# Read the image from the file
image = cv2.imread('example.jpg')

# Convert the image from BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the image using Matplotlib
plt.imshow(image_rgb)
plt.axis('off')  # Hide the axes
plt.show()  # Show the image

Here we have:

          What Each Line Does:

    • import cv2: Lets you use OpenCV functions.
    • from matplotlib import pyplot as plt: Lets you use Matplotlib functions.
    • image = cv2.imread('your_image.jpg'): Loads your image.
    • image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB): Converts the image color from BGR to RGB.
    • plt.imshow(image_rgb): Prepares the image for display.
    • plt.axis('off'): Hides the numbers around the image.
    • plt.show(): Shows the image on the screen.

Image for the OpenCV

http://www.codespeedy.com

To know more about this, you can find another link to know more about content which it is given here -> https://www.geeksforgeeks.org/how-to-display-an-opencv-image-in-python-with-matplotlib/

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top