Image Segmentation Using the Color Thresholder 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. Segmentation by color thresholding is an iterative process— you can 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. You can create a mask image, save a segmented version of the original image, and get the MATLAB® code used to perform the segmentation.

Open Image in Color Thresholder

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.

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. 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).

For this example, read a color image into the MATLAB workspace and view it.

rgb = imread('peppers.png');


From the app’s Load Image menu, click Load Image from Workspace. In the Import from Workspace dialog box, select the variable you created 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 image on the Choose a Color Space tab, with point clouds representing the image in several popular color spaces: RGB, HSV, YCbCr, and L*a*b*. Select the color space that provides the best color separation for segmentation. Using the mouse, rotate the point cloud representations to see how they represent the colors. For this example, click the YCbCr color space.

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. In addition, the Color Thresholder includes the point cloud rendition of the colors in the image in the YCbCr color space. You can perform segmentations by grabbing the handles at each end of the histograms and moving them across the spectrum of values. You can also rotate the color cloud to find the best isolation of the color you are interested in segmenting.

Segment Image Using Color Selector in Color Thresholder

This part of the example shows how to segment an image automatically by selecting colors in the image. With this option, you draw a freehand region in the image to select a color in either the foreground or the background. This example draws the region on the purple background to segment the vegetables from the background. 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 in Color Thresholder.

To segment the image automatically based on a color selection, click the button to draw a region on the image. When you move the cursor over the image, the cursor changes to a cross-hair shape. Drag the cursor over the image to draw regions to specify the colors you want to segment. You can draw multiple regions. If you want to delete a region you drew and start over, right-click the line you drew and select Delete.

After drawing the regions, the Color Thresholder automatically thresholds the image based on the colors you selected in the region you drew. The color controls change to reflect the segmentation. This automatic segmentation did not create a well-defined edge between foreground and background. The background color is lighter near the bottom of the image. You can refine the thresholding by moving the controls.

Segment Image Using Color Component Controls in Color Thresholder

This part of the 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 in Color Thresholder.

Segment the image interactively using the color component controls. For example, use the histogram sliders to select the colors associated with each 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 image. In this case, you segment the foreground but you can invert the segmentation when you are done. Using the controls, It’s hard to achieve a clean segmentation of the background without including part of the foreground image, as shown in the following figure. Redo the segmentation using 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. The tab also contains the point cloud representation of the colors in the image.

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 Using the Color Thresholder.

Create an Image Mask Using the Color Thresholder

This part of the 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 out the foreground, you can swap the foreground and background by clicking Invert Mask. Inverting the mask can be helpful when, for example, it is easier to get a clean separation working with the foreground but you want a mask of the foreground. Perform the segmentation of the foreground, and then invert the mask.

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

To save the mask image in the workspace, when you are satisfied with the segmentation, click Export and choose the Export Images option.

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

To save the MATLAB code required to recreate the segmentation you just performed, click Export 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 22-Jun-2016

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

% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.713;
channel1Max = 0.911;

% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.049;
channel2Max = 0.971;

% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.005;
channel3Max = 1.000;

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

% 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;