Learn R Programming

imager (version 0.45.8)

threshold: Threshold grayscale image

Description

Thresholding corresponding to setting all values below a threshold to 0, all above to 1. If you call threshold with thr="auto" a threshold will be computed automatically using kmeans (ie., using a variant of Otsu's method). This works well if the pixel values have a clear bimodal distribution. If you call threshold with a string argument of the form "XX%" (e.g., "98%"), the threshold will be set at percentile XX. Computing quantiles or running kmeans is expensive for large images, so if approx == TRUE threshold will skip pixels if the total number of pixels is above 10,000. Note that thresholding a colour image will threshold all the colour channels jointly, which may not be the desired behaviour! Use iiply(im,"c",threshold) to find optimal values for each channel separately.

Usage

threshold(im, thr = "auto", approx = TRUE, adjust = 1)

Value

a pixset with the selected pixels

Arguments

im

the image

thr

a threshold, either numeric, or "auto", or a string for quantiles

approx

Skip pixels when computing quantiles in large images (default TRUE)

adjust

use to adjust the automatic threshold: if the auto-threshold is at k, effective threshold will be at adjust*k (default 1)

Author

Simon Barthelme

Examples

Run this code
cimg.limit.openmp()
im <- load.example("birds")
im.g <- grayscale(im)
threshold(im.g,"15%") %>% plot
threshold(im.g,"auto") %>% plot
threshold(im.g,.1) %>% plot
#If auto-threshold is too high, adjust downwards or upwards
#using "adjust"
threshold(im,adjust=.5) %>% plot
threshold(im,adjust=1.3) %>% plot

Run the code above in your browser using DataLab