Learn R Programming

imager (version 0.15)

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)

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)

Value

  • a thresholded image

Examples

Run this code
im <- load.image(system.file('extdata/Leonardo_Birds.jpg',package='imager'))
im.g <- grayscale(im)
threshold(im.g,"15%") %>% plot
threshold(im.g,"auto") %>% plot
threshold(im.g,.1) %>% plot

Run the code above in your browser using DataLab