Main Content

Segment Image and Create Mask Using Color Thresholder App

This example shows how to segment an image and create a binary mask image using the Color Thresholder app. The example segments the foreground (the peppers) from the background (the purple cloth) based on color values.

In the Color Thresholder app, image segmentation can be an iterative process. For example, try segmenting the image in several of the color spaces supported by the app because one color space might isolate a particular color better than another. In any of the supported color spaces, you can initially perform an automatic segmentation by selecting a region in the foreground or background. Then, you can refine the segmentation by using color component controls provided by the app.

The last part of this example shows how to save the results of your work, create a mask image, and get the MATLAB® code the app used to perform the segmentation.

Open Image in Color Thresholder

Read a color image into the workspace.

rgb = imread('peppers.png');

Open the Color Thresholder app from the MATLAB toolstrip. On the Apps tab, in the Image Processing and Computer Vision section, click Color Thresholder .

Load the image into the Color Thresholder app. Click Load Image, and then select Load Image from Workspace. In the Import From Workspace dialog box, select the image from the workspace, and then click OK.

You can also open the app from the command line by using the colorThresholder function, specifying the name of the image: colorThresholder(rgb);. You can also acquire an image Acquire Live Images in the Color Thresholder App.

The Color Thresholder app displays the image in the Choose a Color Space tab, with point clouds representing the image in these color spaces: RGB, HSV, YCbCr, and L*a*b*. For color-based segmentation, select the color space that provides the best color separation. Using the mouse, rotate the point cloud representations to see how they isolate individual colors. Segmentation using the Color Thresholder app can be an iterative process—try several different color spaces before you achieve a segmentation that meets your needs. For this example, start the process by selecting the YCbCr color space.

Segment YCbCr Image in Color Thresholder App

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 and the point cloud representation. The color controls vary depending on the color space. For the YCbCr color space, the Color Thresholder app displays three histograms representing the three color components: the Y component represents brightness, the Cb component represents the blue-yellow spectrum, and the Cr component represents the red-green spectrum.

To explore the image, move the cursor over the image to access the pan and zoom controls.

Automatic Thresholding

First, segment the image using automatic thresholding. Because the background color (purple cloth) is close to a uniform color, segment it rather than the foreground objects (the peppers). You can always invert the mask later using the Invert Mask option.

Define a region using the freehand ROI tool. Click the button in the upper-left corner of the image and draw an ROI on the background. You can draw multiple regions.

After drawing the region, the Color Thresholder app automatically thresholds the image based on the colors you selected in the region you drew. The Y, Cb, and Cr color controls change to reflect the segmentation. This automatic thresholding does not create a clean segmentation of the background and foreground, especially at the lower border between the foreground and background. For this example, the background color is lighter near the bottom of the image. If you want to delete a region you drew and start over, right-click anywhere in the region and select Delete Freehand.

Refine the Automatic Thresholding with the Color Controls

To fine tune the automatic thresholding, use the color controls. For each Y, Cb, and Cr color control, you can set the range of values by dragging the lower and upper bounds in that histogram. Using these color controls you can significantly improve the segmentation of the foreground.

Threshold Image Color Values Using Point Cloud

Another approach to segmenting the image in the YCbCr color space is to draw an ROI on the point cloud to select a range of colors.

On the app toolstrip, click Reset Thresholds to revert back to the original image. In the bottom-right pane of the app, click and drag the point cloud to rotate until you isolate the view of the color you are interested in thresholding. Click the button in the upper left corner of the point cloud. The Color Thresholder app converts the 3-D point cloud into a 2-D representation and activates the polygon ROI tool. Draw an ROI around the color you want to segment (purple). This method can create a better segmentation than the initial automatic thresholding approach.

Segment Image in Another Color Space

To segment the image in another color space, click New Color Space in the app toolstrip. In the Choose a Color Space tab, choose the HSV color space.

The Color Thresholder app creates a new tab displaying the image and the color component controls for the HSV color space. In this color space, H represents hue, S represents saturation, and V represents value. The HSV color space uses a dual-direction knob for the H component and two histogram sliders for the S and V components. The tab also contains the point cloud representation of the colors in the image.

As in the previous iteration, you can use all of the same techniques: automatic thresholding and interactive use of the color component controls, including the point cloud. When you use the color controls, you can see the segmentation in progress. In the pane with the H control, change the range of the hue by clicking and dragging one arrow at a time. Experiment with the controls until you have a clean separation of the background from the foreground. You can clean up small imperfections after you create the mask image using toolbox functions, such as morphological operators.

Create Mask Image Using Color Thresholder App

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.

Because the example segmented the background (the purple cloth) rather than the foreground objects (the peppers), swap the foreground and background by clicking Invert Mask.

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

Save the mask image in the workspace. On the mask toolstrip, click Export and select Export Images.

In the Export To Workspace dialog box, specify variable names for the binary mask image. You can also save the original input RGB image and the segmented version of the original image.

To save the MATLAB code required to recreate the segmentation, 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 on the MATLAB Editor toolstrip. You can run this code, passing it an RGB image, to 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;


See Also