OpenCV for Image Processing in Python

Abstract green matrix code background with binary style.

OpenCV (Open Source Computer Vision Library) is a powerful library for real-time computer vision and image processing. With Python bindings, OpenCV enables developers to perform a wide range of image processing tasks efficiently and effectively.

Key Features of OpenCV
  1. Image Reading and Writing: Load, modify, and save images in various formats.
  2. Image Transformations: Resizing, cropping, rotating, and flipping.
  3. Filtering and Enhancement: Blurring, sharpening, edge detection, and noise removal.
  4. Geometric Transformations: Scaling, affine transformations, and perspective corrections.
  5. Object Detection: Detect faces, shapes, and other features in images or videos.
  6. Color Space Conversion: Convert between RGB, HSV, grayscale, etc
Installing OpenCV

To use OpenCV, install it using pip:

pip install opencv-python

Reading and Displaying an Image

import cv2

# Read an image
image = cv2.imread('example.jpg')

# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)  # Wait for a key press to close the window
cv2.destroyAllWindows()

import cv2:
This imports the OpenCV library. The cv2 module contains functions for reading, processing, and displaying images and videos. It’s essential for performing any image processing task using OpenCV.

cv2.imread():

  • Reads an image file from the specified path (example.jpg) into memory.
  • The function returns the image as a NumPy array, where each pixel is represented by its RGB/BGR values (for color images) or intensity value (for grayscale images).

Arguments:

  • 'example.jpg': The file name or file path of the image.
  • By default, the image is read in color (BGR) mode.
    • You can specify additional flags:
      • cv2.IMREAD_COLOR (default): Reads the image in color (ignores transparency).
      • cv2.IMREAD_GRAYSCALE: Reads the image as a grayscale image.
      • cv2.IMREAD_UNCHANGED: Reads the image with alpha/transparency channel if available.

Example Return: If example.jpg is a 100×100 pixel image, image would be a 3D array with dimensions (100, 100, 3) for RGB (or BGR) values.

cv2.imshow():

  • Displays the image in a window.
  • Arguments:
    • 'Image': Title of the window where the image is displayed.
    • image: The image data to be displayed, returned by cv2.imread().

Behavior:

  • Opens a GUI window with the title 'Image'.
  • The image appears in this window.

cv2.waitKey():

  • Pauses the program and waits for a key press.
  • Arguments:
    • 0: Wait indefinitely until a key is pressed.
    • A positive integer (e.g., 1000) specifies the wait time in milliseconds.

Behavior:

  • When 0 is passed, the image window will remain open until a key is pressed.

cv2.destroyAllWindows():

  • Closes all OpenCV GUI windows opened by cv2.imshow().

Behavior:

  • Frees resources used by the GUI windows.
  • Prevents hanging or unresponsive image windows after the program ends.

Watch the video to learn How to Read and Display Image using OpenCV

To buy Image Processing books
Digital Image Processing
Image Processing

Leave a Comment

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