Digital Image Processing allows you to manipulate color images as conveniently and easily as monochrome images. Color images may be represented in three different color formats and two different color interleaving methods, allowing you to select the most appropriate format for any color processing application.

<< ImageProcessing`

Color image segmentation

We use amplitude thresholding and masking to segment an example image according to color features. In particular, we are interested in extracting the yellow beans from the “bean” image.

This reads a color image.

B = ImageRead["beans.tif"];

Here are the histograms of the individual color channels.

ShowImageHistogram[ImageHistogram[B, {0, 255, 10}]];

This applies thresholding to the individual color channels. The threshold values were chosen based on a visual inspection of the histograms.

T = Threshold[B, {170, 150, 150}];

Show[Graphics[PlanarImageData[T]]];

We now find all the image regions that do not have yellow-colored pixels. We use the ImageProcessing function Where.

notYellow = 
        Where[T,  _?(#1 ≠ {1, 1, 0} &)];

We now use RegionProcessing to set all non-yellow pixels in the original “bean” image to the background color. This is basically a masking operation, but it may also be implemented as a matrix multiplication operation.

tmp = 
        RegionProcessing[{189 &, 200 &, 174 &}, B, notYellow];

We “clean” the resulting color image with a compact morphological filter to reduce small, isolated color patches.

Y = 
        GrayscaleClose[tmp, {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}];

Here we display the original and segmented images.

Show[GraphicsArray[Graphics /@ {B, Y}]];