Image Segmentation Using Point Clouds in the Color Thresholder App

This example shows how to segment an image to create a binary mask image using point cloud controls in the Color Thresholder app. The example segments the human face from the background.

Load Image into the Color Thresholder App

This part of the example shows how to load an image in the Color Thresholder app.

Read an image into the workspace. For this example, read the sample image mandi.tif into the workspace. The image is a Bayer pattern-encoded image that needs to be converted into an RGB image to use with the app. Display the image.

X = imread('mandi.tif');
rgb = demosaic(X, 'bggr');
imshow(rgb)

Open the Color Thresholder app. From the MATLAB® Toolstrip, open the Apps tab and under Image Processing and Computer Vision, click . You can also open the app using the colorThresholder command.

Bring the image into the Color Thresholder app. Click Load Image. You can load an image by specifying its file name or you can read an image into the workspace and load the variable. You can also acquire an image from a camera (see Acquire Live Images in the Color Thresholder App).

From the Load Image menu, click Load Image from Workspace. In the Import from Workspace dialog box, select the variable you created and click OK.

When it opens, the Color Thresholder app displays the Choose a color space tab which displays your image as a point cloud in several popular color spaces: RGB, HSV, YCbCr, and L*a*b*.

Choose a Color Space

This part of the example shows how to choose the color space to work in while segmenting the image. When you first open the app, you must choose the color space to use to represent the color components of the image. Choose the color space where the colors you are interested in segmenting appear near each other in the color model. You can always change the color space you choose later, using New Color Space.

Choose the color space you want to represent color components in your image. When it opens, the Color Thresholder app displays the Choose a color space tab which displays your image as a point cloud in several popular color spaces: RGB, HSV, YCbCr, and L*a*b*. Using the mouse, choose the color space. Examine the representation of the image in each color space, rotating the 3-D depiction of each color space, to see how well the colors are differentiated. You select the color to segment from this 3-D display, so it is important to choose a representation that allows you select the colors of the area you want to segment. For this example, choose the L*a*b* color space.

When you choose a color space, the app opens a new tab, displaying the image along with a set of controls for each color component of the color space you chose. For the L*a*b* color space, the Color Thresholder displays three histograms representing the three components in the color space and a 3-D point cloud representation of the colors of the image in the color space. You can use these components together to create the segmentation of the image. Other color spaces use different types of controls.

Segment the Image Using the Color Cloud

This part of the example shows how to segment the image using the color cloud.

Rotate the color cloud, using the mouse, to find a view of the color cloud that displays the colors you want to segment in isolation. To select the colors you want to use to segment the image by drawing a polygon around the colors, click the button at the top left of the color cloud and start clicking points to create a polygon around the colors you want to use for the segmentation. When you close the polygon, the Color Thresholder app performs the segmentation based on the colors you selected. You can use the histograms to refine your segmentation, as shown in the following figure.

For information about saving your segmentation or the code required to create it, see Create an Image Mask Using the Color Thresholder.