Image Segmentation Using the Color Thesholder App

This example shows how to segment an image to create a binary mask image using the Color Thresholder app. The example has several parts. The first part shows how to open an image in the Color Thresholder. The next part of the example shows how to use the color selection option to segment the image automatically. The next part shows an iterative approach to thresholding using color component controls. In a typical scenario, you perform an initial segmentation using color selection and then refine that segmentation using color component controls. The last part of this example shows what you can do after you complete the segmentation, such as creating a mask image, saving a segmented version of the original image, and getting the MATLAB® code used to perform the segmentation.

Open Image in Color Thresholder App

This example shows how to open an image in the Color Thresholder app. 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.

Read a color image into the MATLAB workspace and view it.

I = imread('peppers.png');

imshow(I)

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 an image into the Color Thresholder app. Click Load Image. Since you have already read the image into the workspace, select Load Image from Workspace.

Select the variable that contains the image you read into the workspace in the Import From Workspace dialog box and click OK.

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 dialog box. This dialog box displays your image as it is represented in several popular color spaces: RGB, HSV, YCbCr, and L*a*b*. Using the mouse, choose the color space. For this example, choose the YCbCr color space, as shown.

The app opens, displaying the image along with a set of controls for each color component. For the YCbCr color space, the Color Thresholder displays three histograms representing color components of the image. In this color space, the Y component represents brightness, the Cb component represents the blue-yellow spectrum, and the Cr component represents the red-green spectrum. Other color spaces use different types of controls.

Segment Image Using Color Selector

This example shows how to use the Color Selection option to segment an image automatically. With this option, you select a color in the foreground or background by drawing a freehand region. You can draw multiple regions. After you segment your image using color selection, you can refine your result using the individual color component controls. See Segment Image Using Color Component Controls.

To segment the image automatically based on a color selection, click Select Colors. When you move the cursor of the image, it changes shape to a cross-hairs which you can use to draw regions to specify the colors you want to segment.

Draw a freehand region using the mouse to select the color you want to use for segmentation. You can draw multiple regions.

After drawing the regions, click Find Thresholds to threshold the image based on the color in the region you drew. To delete the regions that you drew, right click on the line and select Delete.

Segment Image Using Color Component Controls

This example shows how to use the Color Thresholder app to segment an image interactively using color component controls. Segmentation using the Color Thresholder is an iterative process—you might need to try several different color spaces before you achieve a segmentation that meets your needs. You can also perform an initial segmentation automatically using the color selection option and then refine the results using the color component controls. See Segment Image Using Color Selector.

Segment the image interactively using the color component controls. Move the slider associated with each histogram over the colors in the histogram. You can see the segmentation in progress. For this example, moving the slider on the Y component has the greatest effect on segmenting the background, but it's hard to cleanly segment the background without including part of the foreground image, as shown in the following figure. Since this is an iterative process, try another color space.

To use another color space, click New Color Space. The app displays the Choose a color space dialog box again.

Select a new color space in the Choose a Color Space dialog box. For this example, choose the HSV color space. The Color Thresholder creates a new tab displaying the image and the color component controls for this color space. The HSV color space uses a dual-direction knob for the H component and two histogram sliders for the S and V components. In this color space, H stands for hue, S for saturation, and V for value.

As you did before with the YCbCr color space, use the color component controls to segment the image interactively. As you use the controls, you can see the segmentation in progress. Using the mouse, grab one of the handles on the H control and move it in the direction of the arrow. Experiment with the controls until you have a clean separation of the background from the foreground. In this color space, you can achieve a good segmentation using the H control, as shown in the following figure. You can clean up small imperfections after you create the mask image using other toolbox functions, such as morphological operators. For information about saving your segmented image, see Create an Image Mask.

Create an Image Mask

This example shows how to create a mask image after segmentation. You can also get the segmented image and the MATLAB code used to create the mask image.

After segmenting the foreground and background, you can swap the foreground and background by clicking Invert Mask. Inverting the mask can be helpful when, for example, you can achieve a cleaner segmentation working with the background but you want a mask of the foreground. Perform the segmentation with the background, and then invert the mask.

View the binary mask image that you created by clicking Show Binary.

When you are satisfied with the segmentation, click Export Images and select Export Images to save the mask image in the workspace. you can also save the segmented original image.

In the Export to Workspace dialog box, specify the name of the variables for the binary mask image and the segmented version of the original image.

To save the MATLAB code required to recreate the segmentation you just performed, click Export Images and select Export Function. The Color Thresholder app opens the MATLAB Editor with the code that creates the segmentation. To save the code, click Save in the MATLAB Editor. You can run this code, passing it an RGB image, and create the same mask image programmatically.

function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using 
% auto-generated code from the colorThresholder App. The colorspace and
% minimum/maximum values for each channel of the colorspace were set in the 
% App and result in a binary mask BW and a composite image maskedRGBImage,
% which shows the original RGB image values under the mask BW.

% Auto-generated by colorThresholder app on 03-Jun-2014
%------------------------------------------------------


% Convert RGB image to chosen color space
I = rgb2hsv(RGB);

% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.739;
channel1Max = 0.940;

% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.328;
channel2Max = 0.671;

% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.122;
channel3Max = 0.803;

% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);

% Invert mask
BW = ~BW;

% Initialize output masked image based on input image.
maskedRGBImage = RGB;

% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
Was this topic helpful?