Image Processing: Edge Detection, Dilation, and Rotation

  • Share this:

Code introduction


This function first loads an image, converts it to grayscale, and then detects edges using the Canny edge detector. It then dilates the edges to enhance their visibility. Next, it transforms the image to polar coordinates and rotates it by 45 degrees. Finally, it returns the rotated image.


Technology Stack : scikit-image, NumPy

Code Type : Image processing

Code Difficulty : Intermediate


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

def find_edges_and_apply_dilation(image_path, threshold=0.9):
    # Load the image from the given path
    image = io.imread(image_path)
    
    # Convert the image to grayscale
    gray_image = filters.gaussian(image, sigma=1)
    
    # Detect edges using Canny edge detector
    edges = feature.canny(gray_image, threshold=threshold)
    
    # Dilate the edges to make them more pronounced
    dilated_edges = filters.dilate(edges, np.ones((3,3)), iterations=1)
    
    # Transform the image to polar coordinates to apply rotation
    polar_transform = transform.polar_transform(dilated_edges)
    
    # Rotate the image by 45 degrees
    rotated_image = transform.rotate(polar_transform, angle=45, resize=False)
    
    return rotated_image