Canny Edge Detection with Image Preprocessing

  • Share this:

Code introduction


This function detects edges in an image using the Canny edge detector. It first converts the image from RGB to grayscale, then applies Gaussian blur to reduce noise, and finally uses the Canny edge detector to detect edges.


Technology Stack : scikit-image, NumPy

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
import numpy as np
from skimage import io, color, feature, transform

def edge_detection(image_path, sigma=1.5):
    """
    Perform edge detection on an image using the Canny edge detector.
    """
    # Load the image
    image = io.imread(image_path)
    
    # Convert the image to grayscale
    gray_image = color.rgb2gray(image)
    
    # Apply Gaussian blur to reduce noise
    blurred_image = feature.gaussian_filter(gray_image, sigma=sigma)
    
    # Use the Canny edge detector to find edges
    edges = feature.canny(blurred_image)
    
    # Return the edges
    return edges

# Code Information