Image Thresholding and Contour Detection

  • Share this:

Code introduction


This function loads an image from a specified path, converts it to grayscale, applies Canny edge detection, then uses a local thresholding method to binarize the image. Finally, it finds and returns the contours in the image.


Technology Stack : scikit-image, numpy

Code Type : Image processing

Code Difficulty : Intermediate


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

def find_contours_and_threshold_image(image_path, threshold):
    # Load the image from the specified path
    image = io.imread(image_path)
    
    # Convert the image to grayscale
    gray_image = feature.canny(image)
    
    # Apply threshold to the image
    binary_image = filters.threshold_local(gray_image, size=15, offset=0)
    
    # Find contours in the binary image
    contours, _ = feature.find_contours(binary_image, threshold)
    
    # Return the contours
    return contours

# Code metadata