List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Perform Edge Detection and Image Transformation Using OpenCV in Python?

Edge Detection and Transformation using OpenCV

Condition for Edge Detection and Transformation of an Image Using OpenCV

  • Description: Edge detection is a technique used to identify the boundaries or outlines within an image, highlighting areas of significant intensity change. It is a fundamental tool in image processing, object detection, and computer vision. Transformations, on the other hand, refer to operations that alter the structure, orientation, or properties of an image (e.g., scaling, rotation, translation). This document explores the process of edge detection and transformation using OpenCV, a popular open-source library for computer vision tasks.
Why Should We Choose Edge Detection and Transformation?
  • Object Detection: Edge detection helps identify boundaries of objects, useful in tasks like facial recognition, object tracking, and autonomous vehicles.
  • Image Enhancement: Edge detection improves image clarity and focus, especially in areas of high contrast.
  • Geometric Transformations: Crucial for image registration, object alignment, and augmentation, particularly in fields like robotics and Augmented Reality (AR).
  • Data Preprocessing: Many machine learning models require preprocessed images emphasizing important features (like edges) and transformations.
Step-by-Step Process
  • Read the Image: Load the image you want to process.
  • Preprocess the Image: Convert the image to grayscale to simplify the process and reduce computational cost.
  • Edge Detection: Use algorithms like Sobel, Canny, or Laplacian for edge detection.
  • Apply Transformations: Apply geometric transformations such as scaling, rotation, or translation to manipulate the image.
  • Display Results: Show the processed image and the result of edge detection.
Sample Source Code
  • import cv2
    import numpy as np
    import matplotlib.pyplot as plt

    # Load the image (make sure to provide the correct path to the image)
    image = cv2.imread('/home/soft15/soft15/Python/py_Exercises/python_Machine_Learning/25-11-2024/36.detect edge and transformation/img/cat2.jpeg')

    # Check if the image is loaded properly
    if image is None:
        raise ValueError("Image not found! Please check the path.")

    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Apply GaussianBlur to reduce noise
    blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

    # Apply Canny edge detection
    edges = cv2.Canny(blurred_image, 100, 200)

    # Display the original and edge-detected images
    plt.figure(figsize=(10, 5))
    plt.subplot(1, 2, 1)
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
    plt.title("Original Image")
    plt.axis('off')
    plt.subplot(1, 2, 2)
    plt.imshow(edges, cmap='gray')
    plt.title("Edge Detected Image")
    plt.axis('off')
    plt.show()

    # Apply rotation
    rows, cols = image.shape[:2]
    rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1) # Rotate by 45 degrees
    rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))

    # Apply translation
    translation_matrix = np.float32([[1, 0, 50], [0, 1, 100]]) # Translate by 50 pixels along X, 100 along Y
    translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))

    # Apply scaling (resizing)
    scaled_image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR) # Resize to 50% of the original

    # Apply horizontal flipping
    flipped_image = cv2.flip(image, 1) # Flip horizontally

    # Display the transformed images
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 2, 1)
    plt.imshow(cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
    plt.title("Rotated Image")
    plt.axis('off')
    plt.subplot(1, 2, 2)
    plt.imshow(cv2.cvtColor(translated_image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
    plt.title("Translated Image")
    plt.axis('off')
    plt.show()

    # Display the scaling and flipping results
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 2, 1)
    plt.imshow(cv2.cvtColor(scaled_image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
    plt.title("Scaled Image")
    plt.axis('off')
    plt.subplot(1, 2, 2)
    plt.imshow(cv2.cvtColor(flipped_image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
    plt.title("Flipped Image")
    plt.axis('off')
    plt.show()
Screenshots
  • Edge Detection and Transformation Screenshot