Image Thresholding and Saving with Local Thresholding

  • Share this:

Code introduction


This function first loads an image, converts it to a grayscale image, then applies local thresholding, and finally saves the result to the specified file path.


Technology Stack : numpy, scikit-image

Code Type : Image processing

Code Difficulty : Intermediate


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

def threshold_image(input_image_path, output_image_path, threshold_value):
    # Load the image
    image = io.imread(input_image_path)
    
    # Convert the image to grayscale
    gray_image = color.rgb2gray(image)
    
    # Apply thresholding
    binary_image = filters.thresholding.threshold_local(gray_image, threshold_value, offset=10)
    
    # Save the result
    io.imsave(output_image_path, binary_image)