Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

imseggeodesic

Segment image into two or three regions using geodesic distance-based color segmentation

Syntax

L = imseggeodesic(RGB,BW1,BW2)
L = imseggeodesic(RGB,BW1,BW2,BW3)
[L,P] = imseggeodesic(___)
[L,P] = imseggeodesic(___,Name,Value,...)

Description

example

L = imseggeodesic(RGB,BW1,BW2) segments the input image RGB, which must be a valid RGB image, returning a segmented binary image with segment labels specified by label matrix L. BW1 and BW2 are binary images that specify the location of the initial seed regions, called scribbles, for the two regions (foreground and background). imseggeodesic uses the scribbles specified in BW1 and BW2 as representative samples for computing the statistics for their respective regions, which it then uses in segmentation. The scribbles specified by BW1 and BW2 (regions that are logical true) should not overlap. The underlying algorithm uses the statistics estimated over the regions marked by the scribbles for segmentation. The greater the number of pixels marked by scribbles, the more accurate the estimation of the region statistics, which typically leads to more accurate segmentation. Therefore, it is a good practice to provide as many scribbles as possible. Typically, provide at least a few hundred pixels as scribbles for each region.

example

L = imseggeodesic(RGB,BW1,BW2,BW3) segments the input image RGB, returning a segmented image with three segments (trinary segmentation) with the region labels specified by label matrix L. BW1, BW2, and BW3 are binary images that specify the location of the initial seed regions or scribbles for the three regions. The scribbles specified by BW1, BW2, and BW3 (regions that are logical true) should not overlap.

example

[L,P] = imseggeodesic(___) returns the probability for each pixel belonging to each of the labels in matrix P.

example

[L,P] = imseggeodesic(___,Name,Value,...) segments the image using name-value pairs to control aspects of segmentation. Parameter names can be abbreviated.

Examples

collapse all

Read image into workspace and display it.

RGB = imread('yellowlily.jpg');
imshow(RGB,'InitialMagnification',50)
hold on

Specify the initial seed regions or "scribbles" for the foreground object, in the form [left_topR left_topC bottom_rightR bottom_rightC].

bbox1 = [700 350 820 775];
BW1 = false(size(RGB,1),size(RGB,2));
BW1(bbox1(1):bbox1(3),bbox1(2):bbox1(4)) = true;

Specify the initial seed regions or "scribbles" for the background.

bbox2 = [1230 90 1420 1000];  
BW2 = false(size(RGB,1),size(RGB,2));
BW2(bbox2(1):bbox2(3),bbox2(2):bbox2(4)) = true;

Display seed regions. The foreground is in red and the background is blue.

visboundaries(BW1,'Color','r');
visboundaries(BW2,'Color','b');

Segment the image.

[L,P] = imseggeodesic(RGB,BW1,BW2);

Display results.

figure
imshow(label2rgb(L),'InitialMagnification', 50)
title('Segmented image')

  
figure
imshow(P(:,:,1),'InitialMagnification', 50)
title('Probability that a pixel belongs to the foreground')

Read image into the workspace and display it.

RGB = imread('yellowlily.jpg'); 
imshow(RGB,'InitialMagnification', 50)
hold on

Creates scribbles for three regions. Note that you can specify the scribbles interactively using tools such as roipoly, imfreehand, imrect, impoly, and imellipse. Region 1 is the yellow flower. Region 2 is the green leaves. Region 3 is the background.

region1 = [350 700 425 120]; % [x y w h] format
BW1 = false(size(RGB,1),size(RGB,2));
BW1(region1(2):region1(2)+region1(4),region1(1):region1(1)+region1(3)) = true;
 
region2 = [800 1124 120 230];
BW2 = false(size(RGB,1),size(RGB,2));
BW2(region2(2):region2(2)+region2(4),region2(1):region2(1)+region2(3)) = true;

region3 = [20 1320 480 200; 1010 290 180 240]; 
BW3 = false(size(RGB,1),size(RGB,2));
BW3(region3(1,2):region3(1,2)+region3(1,4),region3(1,1):region3(1,1)+region3(1,3)) = true;
BW3(region3(2,2):region3(2,2)+region3(2,4),region3(2,1):region3(2,1)+region3(2,3)) = true;

Display the seed regions.

visboundaries(BW1,'Color','r');
visboundaries(BW2,'Color','g');
visboundaries(BW3,'Color','b');

Segment the image.

[L,P] = imseggeodesic(RGB,BW1,BW2,BW3, 'AdaptiveChannelWeighting', true);

Display results.

figure
imshow(label2rgb(L),'InitialMagnification', 50)
title('Segmented image with three regions')

  
figure
imshow(P(:,:,2),'InitialMagnification', 50)
title('Probability that a pixel belongs to region/label 2')

Input Arguments

collapse all

Image to be segmented, specified as a valid RGB image. imseggeodesic converts the input RGB image to the YCbCr color space before performing the segmentation.

Example: RGB = imread('peppers.png');

Data Types: double | uint8 | uint16

Scribble image, specified as a logical matrix. BW1 must have the same number of rows and columns as the input image RGB. To specify the scribbles interactively, use roipoly, imfreehand, imrect, impoly, or imellipse.

Example: bbox1 = [700 350 820 775]; BW1 = false(size(RGB,1),size(RGB,2)); BW1(bbox1(1):bbox1(3),bbox1(2):bbox1(4)) = true;

Data Types: logical

Scribble image, specified as a logical matrix. BW2 must have the same number of rows and columns as the input image RGB. To specify the scribbles interactively, use roipoly, imfreehand, imrect, impoly, or imellipse.

Example: bbox2 = [1230 90 1420 1000]; BW2 = false(size(RGB,1),size(RGB,2)); BW2(bbox2(1):bbox2(3),bbox2(2):bbox2(4)) = true;

Data Types: logical

Scribble image, specified as a logical matrix. BW3 must have the same number of rows and columns as the input image RGB. To specify the scribbles interactively, use roipoly, imfreehand, imrect, impoly, or imellipse.

Example: bbox3 = [20 1320 480 200; 1010 290 180 240]; BW3 = false(size(RGB,1),size(RGB,2)); BW3(bbox3(1,2):bbox3(1,2)+bbox3(1,4),bbox3(1,1):bbox3(1,1)+bbox3(1,3)) = true;

Data Types: logical

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: [L,P] = imseggeodesic(RGB,BW1,BW2,BW3, 'AdaptiveChannelWeighting', true);

collapse all

Use adaptive channel weighting, specified as a logical scalar. When true, imseggeodesic weights the channels proportional to the amount of discriminatory information they have that is useful for segmentation (based on the scribbles provided as input). When false (the default), imseggeodesic weights all the channels equally.

Example: [L,P] = imseggeodesic(RGB,BW1,BW2,BW3, 'AdaptiveChannelWeighting', true);

Data Types: logical

Output Arguments

collapse all

Label matrix, returned as a double matrix where the elements are integer values greater than or equal to 0. Pixels labeled 0 are the background and pixels labeled 1 identify a segmented region. Pixels labeled 2 identify another segmented region in trinary segmentation.

Probability a pixel belongs to a labeled region, specified as an M-by-N-by-2 matrix for binary segmentation or an M-by-N-by-3 matrix for trinary segmentation. M and N are the number of rows and columns in the input image. P(i,j,k) specifies the probability of pixel at location (i,j) belonging to label k. P is of class double.

Tips

  • The scribbles for the two (or three) regions should not overlap each other. Each scribble matrix (BW1, BW2, and BW3) should be nonempty, that is, there should be at least one pixel (although the more the better) marked as logical true in each of the scribbles.

Algorithms

imseggeodesic uses a geodesic distance-based color segmentation algorithm (similar to [1]).

References

[1] A. Protiere and G. Sapiro, Interactive Image Segmentation via Adaptive Weighted Distances, IEEE Transactions on Image Processing, Volume 16, Issue 4, 2007.

Introduced in R2015a

Was this topic helpful?