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 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 variable name of the image:
colorThresholder(im)
Select Color Space
Color Thresholder 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. 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: the Y component represents brightness, the Cb component represents the blue-yellow spectrum, and the Cr component 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 the Invert Mask option.
Define a region using the freehand ROI tool. Click the lasso icon in 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 select Delete Freehand.
After drawing the region, Color Thresholder 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.
Refine Automatic Thresholding Using 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 selecting a range of colors is to draw an ROI on the point cloud.
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. Hover over the point cloud and click the ROI button in 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, click New Color Space in the app toolstrip. In the Choose a Color Space tab, 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, 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
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 by clicking Show Binary on the app toolstrip.
Export Results
Save the mask in the workspace. On the 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. Color Thresholder 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 % 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 space I = rgb2hsv(RGB); % Define thresholds for channel 1 based on histogram settings channel1Min = 0.734; channel1Max = 0.921; % Define thresholds for channel 2 based on histogram settings channel2Min = 0.334; channel2Max = 1.000; % Define thresholds for channel 3 based on histogram settings channel3Min = 0.000; channel3Max = 0.868; % 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; end