Learn R Programming

wvtool (version 1.0)

gray2bin: Conversion from Grayscale to Binary Image

Description

A function provides automatic clustering-based thresholding proposed by Ohtsu, and a gray scale image is converted to binary image. Initial histogram and discriminant level of binning are displayed by his=TRUE, dis=TRUE options. A threshold value can be also set manually.

Usage

gray2bin(x, auto=TRUE, th=200, his=FALSE, dis=FALSE)

Arguments

x
A raster image or a matrix
auto
set threshold automatically (Ohtsu method) or manually
th
a threshold value used when auto=FALSE
his
A histogram of initial gray scale image
dis
A plot of variation between classes divided by variation within classes

Value

References

N. Otsu (1979) A threshold selection method from gray-level histograms, IEEE Trans. Sys., Man., Cyber., 9(1), 62-66.

See Also

rgb2gray

Examples

Run this code
data(camphora)
par(mfrow=c(2,3))
image(rot90c(camphora), col= gray((0:255)/255), main="camphora", asp=1, useRaster=TRUE, axes=FALSE)
out <- gray2bin(camphora, his=TRUE, dis=TRUE)
image(rot90c(out), col= gray((0:255)/255), main="binary image, auto", asp=1, 
useRaster=TRUE, axes=FALSE)
image(rot90c(gray2bin(camphora,auto=FALSE,th=100)), col= gray((0:255)/255), main="binary image, 
thresh=100", asp=1, useRaster=TRUE, axes=FALSE)
image(rot90c(gray2bin(camphora,auto=FALSE,th=180)), col= gray((0:255)/255), main="binary image, 
thresh=180", asp=1, useRaster=TRUE, axes=FALSE)

Run the code above in your browser using DataLab