Function File: im2bw (img)
Function File: im2bw (X, cmap)
Function File: im2bw (…, threshold)
Function File: im2bw (…, method)

Convert image to binary, black and white, by threshold.

The input image img can either be a grayscale or RGB image. In the later case, img is first converted to grayscale with rgb2gray. Input can also be an indexed image X in which case the colormap cmap needs to be specified.

The value of threshold should be in the range [0,1] independently of the class of img. Values from other classes can be converted to the correct value with im2double:

bw = im2bw (img_of_class_uint8, im2double (thresh_of_uint8_class));

For an automatic threshold value, consider using graythresh. The argument method is a string that specifies a valid algorithm available in graythresh. The following are equivalent:

bw = im2bw (img, "moments");
bw = im2bw (img, graythresh (img(:), "moments"));

See also: graythresh, ind2gray, rgb2gray.

Package: image