Contenuto principale

Get Started with Image Segmenter

R2026b

The Image Segmenter app provides access to many different ways to perform a binary segmentation of an image. These are the steps for segmenting an image in Image Segmenter.

  • Load an image to be segmented into Image Segmenter. You can optionally also load a binary segmentation mask to add to or refine using Image Segmenter.

  • Create the segmented mask or add regions to the mask. Performing segmentation using Image Segmenter can be an iterative process where you try several of the segmentation options. Some segmentation techniques might work better with certain types of images than others.

  • Refine the boundaries of the segmentation mask.

  • Save the binary mask. You can also retrieve the code that Image Segmenter used to create the mask.

Open Image Segmenter and Load Image

You can open Image Segmenter from the command line. When you open Image Segmenter from the command line, you can optionally specify a workspace variable containing the image.

I = imread("coins.png");
imageSegmenter(I)

Alternatively, open the app from the Apps tab, under Image Processing and Computer Vision. Then, from the Load Image menu, choose an image file or a workspace variable containing an image. Image Segmenter can open any file that can be read by imread.

After you load an image into the app, you can optionally load an existing binary mask. For example, you might have previously created a mask of an RGB image in the Color Thresholder app and you want to refine the segmentation. To load an existing mask, click Load Mask. The segmentation mask image must be a logical image of the same size as the image you are segmenting.

Create and Add Regions to Segmented Mask

To create an initial mask, use any of the tools in the Create Mask and Add to Mask menus. If you want to start a new segmentation after creating a mask, click New Segmentation. You can perform multiple segmentations using the app. Each segmentation appears, with a thumbnail, in the Data Browser.

To add segmented regions to an existing mask, use tools in the Add to Mask menu. The app displays the steps you take while creating the segmentation in the History panel of the Data Browser.

ToolDescriptionSample Segmentation
Segment Anything Model

An AI-assisted automatic technique that enables you to instantaneously segment selected objects or perform full image segmentation. For an example, see Segment Objects Using Segment Anything Model (SAM) in Image Segmenter.

Preview of selected pears from an automatic segmentation of the entire image using SAM.

Threshold

An automatic technique where you specify a global threshold that separates the foreground from background. This technique can be useful if the objects you want to segment are all noticeably brighter than the background. For an example, see Segment Image Using Thresholding.

Preview of segmented pears using global thresholding.

Graph Cut

A semi-automatic technique where you draw lines on the image, called scribbles, to identify what you want in the foreground and what you want in the background. You can select multiple regions to include in the ROI, and refine the segmentation interactively. For an example, see Segment Image Using Graph Cut in Image Segmenter.

The Graph Cut algorithm is based on graph theory. This algorithm creates a graph of the image where each pixel is a node connected by weighted edges. Larger weights indicate pixel pairs that have high probability of belonging to the same region. The algorithm separates foreground from background by cutting the graph along weak edges. Image Segmenter uses a particular variety of the Graph Cut algorithm called lazy snapping.

Preview of segmented pears using Graph Cut, with foreground and background scribbles.

Auto Cluster

An automatic technique where the app performs k-means clustering into two clusters. For an example, see Segment Image Using Auto Cluster in Image Segmenter. For more information about k-means clustering, see imsegkmeans.

The app converts RGB images into the L*a*b* color space before performing k-means clustering.

Preview of segmented pears using auto cluster.

Find Circles

An automatic technique where you specify the minimum and maximum diameter of the circular objects you want to detect. For an example, see Segment Image Using Find Circles in Image Segmenter

Preview of segmented coins with diameters of around 48 pixels using Find Circles.

Local Graph Cut

A semi-automatic technique where you draw scribbles to identify the foreground and background within a singular rectangular or polygonal ROI. Everything outside the ROI is considered background. For an example, see Segment Image Using Local Graph Cut (Grabcut) in Image Segmenter.

The Local Graph Cut algorithm is similar to the Graph Cut method. By limiting the graph to the region within an ROI, the Local Graph Cut algorithm can run more quickly than the Graph Cut algorithm.

Preview of segmented pear using Local Graph Cut, with foreground and background scribbles within a rectangular ROI.

Flood Fill

An automatic technique where the app segments a contiguous region with similar intensity values to a point that you select.

Preview of segmented pear using flood fill.

Draw ROIs

A manual technique where you draw shapes that outline the region the objects you want to segment. You can draw rectangles, ellipses, polygons, or freehand shapes. For an example, see Segment Image by Drawing Regions.

Preview of segmented pears using Assisted Freehand and Circle ROI shapes.

Paint Brush

A manual technique where you paint over regions that you want to segment. Click and drag over the image to label pixels. Adjust the Brush Size to change the size of the paint brush.

Preview of segmented pear using paint brush.

When using the Auto Cluster, Graph Cut, and Flood Fill segmentation tools, you can also include texture as an additional consideration in your segmentation. Texture filtering can help distinguish foreground from background. To turn the texture option on and off, click Include Texture Features. When enabled, Image Segmenter uses Gabor filters to analyze the texture of the image as a preprocessing step in the segmentation. For more information, see Use Texture Filtering in Image Segmenter. For more information about Gabor filters, see Texture Segmentation Using Gabor Filters.

Refine Segmented Mask

Image Segmenter provides access to several tools that you can use to refine the mask you created.

ToolDescription
Segment Anything Model

An automatic technique to refine a mask by selecting an ROI around the mask, or clicking on image areas to add to or remove from the mask. For an example, see Segment Objects Using Segment Anything Model (SAM) in Image Segmenter.

Morphology

Many morphological techniques, such as dilation and erosion. For an example, see Refine Segmentation Using Morphology in Image Segmenter.

Active contours (also known as snakes)

An automatic, iterative method that grows or shrinks regions in the mask. For an example, see Refine Segmentation Mask Using Active Contours in Image Segmenter. For more information about the active contours algorithm, see activecontour.

Clear borders

A fast way to remove small regions on the edge of the image.

Fill holes

A fast way to fill small holes in foreground regions. For an example, see Refine Segmentation Using Morphology in Image Segmenter.

Invert mask

Sometimes the segmentation is easier to evaluate if you invert the foreground and background. For an example, see Segment Image Using Auto Cluster in Image Segmenter

Export Segmentation Results

When you find an acceptable segmentation, you can export to the workspace the final segmentation mask image and the segmented version of the original image. To export the mask and segmentation to the workspace, click Export and select Export Images.

You can also generate the code used to perform the segmentation (requires Statistics and Machine Learning Toolbox™.) Use the code to apply the same segmentation algorithm to similar images. To get the code, click Export and select Generate Function. The app opens the MATLAB® editor containing a function with the autogenerated code. To save the code, click Save in the MATLAB editor.

See Also

Topics