make_binary_image: Convert Image to Binary with Flexible Thresholding

View source: R/m.R

make_binary_imageR Documentation

Convert Image to Binary with Flexible Thresholding

Description

This function converts a color or grayscale image to a binary image using Gaussian smoothing and a user-specified thresholding method, such as Otsu's method.

Usage

make_binary_image(img, sigma = 0, threshold_method = "otsu")

Arguments

img

An image object. This can be a color or grayscale image loaded into R using the EBImage package.

sigma

Numeric value specifying the standard deviation for the Gaussian blur applied to the grayscale image. Higher values result in more smoothing. 0 skips smoothing. Default is 0.

threshold_method

Character string specifying the thresholding method to use. Options are "otsu" for Otsu's method or "mean" for mean thresholding. Default is "otsu".

Details

The function first converts the input image to grayscale using the EBImage::channel() function. If apply_smoothing is TRUE, it applies a Gaussian blur to the grayscale image to reduce noise and smooth the image using EBImage::gblur(). The selected thresholding method (Otsu's method or mean thresholding) is then applied to the processed image to calculate a threshold, which is used to convert the image to a binary format where the foreground is marked as TRUE and the background as FALSE.

The function includes basic error checking to ensure that the input is a valid image object.

Value

A binary image (logical matrix) where pixels are either TRUE (foreground) or FALSE (background), based on the chosen thresholding method applied to the (optionally smoothed) grayscale version of the input image.


theMILOlab/SPATA2 documentation built on Feb. 8, 2025, 11:41 p.m.