Main Content

Segment Image and Create Mask Using Color Thresholder

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.

Image segmentation by color thresholding can be an iterative process. For example, you can try segmenting the image in different color spaces 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.

This example also shows how to create a binary mask image, save the results of your work, and export MATLAB® code that enables you to reproduce the segmentation.

Open Image in Color Thresholder

Read a color image into the workspace.

im = imread("peppers.png");

Open Color Thresholder from the MATLAB toolstrip. On the Apps tab, in theImage Processing and Computer Visionsection,clickColor Thresholder.

Load the image into the Color Thresholder app. ClickLoad Image, and then select加载图像从工作区。In the Import From Workspace dialog box, select the image from the workspace, and then clickOK.

You can also open the app from the command line by using thecolorThresholderfunction, specifying the variable name of the image:

colorThresholder(im)

Select Color Space

Color Thresholder displays the image in theChoose a Color Spacetab, 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. For this example, start by selecting the YCbCr color space.

Segment Image

When you choose a color space, Color Thresholder 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 app displays three histograms representing the three color components: theYcomponent represents brightness, theCbcomponent represents the blue-yellow spectrum, and theCrcomponent represents the red-green spectrum.

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

Automatic Thresholding

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

Define a region using the freehand ROI tool. Click the lasso iconin the axes toolbar at the top-right corner of the image and draw an ROI on the background. You can draw multiple regions. If you want to delete a region you drew and start over, right-click anywhere in the region and selectDelete Freehand.

After drawing the region, Color Thresholder automatically thresholds the image based on the colors you selected in the region you drew. TheY,Cb, andCrcolor 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.

Refine Automatic Thresholding Using Color Controls

To fine tune the automatic thresholding, use the color controls. For eachY,Cb, andCrcolor 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 selecting a range of colors is to draw an ROI on the point cloud.

On the app toolstrip, clickReset Thresholdsto 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. Hover over the point cloud and click the ROI buttonin the top left corner of the point cloud. Color Thresholder 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, clickNew Color Spacein the app toolstrip. In theChoose a Color Spacetab, choose the HSV color space.

Color Thresholder creates a new tab that displays the image and the color component controls for the HSV color space. In this color space,Hrepresents hue,Srepresents saturation, andVrepresents value. The HSV color space uses a dual-direction knob for theHcomponent and two histogram sliders for theSandVcomponents. 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 theHcontrol, 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

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

View the binary mask image by clickingShow Binaryon the app toolstrip.

Export Results

Save the mask in the workspace. On the toolstrip, clickExportand selectExport 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, clickExportand selectExport Function. Color Thresholder opens the MATLAB Editor with the code that creates the segmentation. To save the code, clickSaveon 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% range for each channel of the colorspace were set within the app. The% segmentation mask is returned in BW, and a composite of the mask and% original RGB images is returned in maskedRGBImage.% Auto-generated by colorThresholder app on 01-Jan-2023%------------------------------------------------------% Convert RGB image to chosen color spaceI = rgb2hsv(RGB);% Define thresholds for channel 1 based on histogram settingschannel1Min = 0.734; channel1Max = 0.921;%为通道2基于histogr定义阈值am settingschannel2Min = 0.334; channel2Max = 1.000;%为通道3基于histogr定义阈值am settingschannel3Min = 0.000; channel3Max = 0.868;% Create mask based on chosen histogram thresholdssliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) &...(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) &...(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max); BW = sliderBW;% Invert maskBW = ~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;end

See Also

Related Topics