segment image and create mask using color thresholder -凯发k8网页登录
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