Main Content

# imsegfmm

Binary image segmentation using fast marching method

## Syntax

``BW = imsegfmm(W,mask,thresh)``
``BW = imsegfmm(W,C,R,thresh)``
``BW = imsegfmm(W,C,R,P,thresh)``
``````[BW,D] = imsegfmm(___)``````

## Description

````BW = imsegfmm(W,mask,thresh)` segments a weight array `W` using the fast marching method. `mask` is a logical array that specifies seed locations. `thresh` specifies the threshold level that separates foreground from background pixels.```

example

````BW = imsegfmm(W,C,R,thresh)` segments a 2-D image, with seed locations specified by column and row indices `C` and `R`, respectively.```
````BW = imsegfmm(W,C,R,P,thresh)` segments a 3-D image, with seed locations specified by column, row, and plane indices `C`, `R`, and `P`, respectively.```
``````[BW,D] = imsegfmm(___)``` also returns the normalized geodesic distance map `D` computed using the fast marching method. `BW` is a thresholded version of `D`, where all the pixels that have normalized geodesic distance values less than or equal to `thresh` are considered foreground pixels and set to `true`. You can obtain different segmentation results by thresholding `D` at different levels.```

## Examples

collapse all

This example shows how to segment an object in an image using Fast Marching Method based on differences in grayscale intensity as compared to the seed locations.

Read image.

```I = imread('cameraman.tif'); imshow(I) title('Original Image')```

Create mask and specify seed location. You can also use `roipoly` to create the mask interactively.

```mask = false(size(I)); mask(170,70) = true;```

Compute the weight array based on grayscale intensity differences.

`W = graydiffweight(I, mask, 'GrayDifferenceCutoff', 25);`

Segment the image using the weights.

```thresh = 0.01; [BW, D] = imsegfmm(W, mask, thresh); figure imshow(BW) title('Segmented Image')```

You can threshold the geodesic distance matrix `D` using different thresholds to get different segmentation results.

```figure imshow(D) title('Geodesic Distances')```

This example segments the brain from MRI data of the human head.

Load the MRI data.

```load("mri") V = squeeze(D);```

Visualize the data.

```sizeO = size(V); figure slice(double(V),sizeO(2)/2,sizeO(1)/2,sizeO(3)/2); shading interp colormap("gray") title("Original")```

Set the seed locations.

```seedR = 75; seedC = 60; seedP = 10;```

Compute weights based on grayscale intensity differences.

`W = graydiffweight(V,seedC,seedR,seedP,"GrayDifferenceCutoff",25);`

Segment the image using the weights.

```thresh = 0.002; BW = imsegfmm(W,seedC,seedR,seedP,thresh);```

Visualize the segmented image using an isosurface.

```figure p = patch(isosurface(double(BW))); p.FaceColor = "red"; p.EdgeColor = "none"; daspect([1 1 27/64]); camlight lighting phong```

## Input Arguments

collapse all

Weight array, specified as a non-negative numeric array. You can compute the weight array by using the `graydiffweight` or `gradientweight` functions. Large values in `W` identify the foreground (object) and small values identify the background.

Data Types: `single` | `double` | `uint8` | `int8` | `int16` | `uint16` | `int32` | `uint32`

Seed locations mask, specified as a logical array of the same size as `W`. Locations where `mask` is `true` are seed locations. If you use `graydiffweight` to create the weight matrix `W`, it is recommended that you use the same value of `mask` with `imsegfmm` that you used with `graydiffweight`.

Data Types: `logical`

Threshold level used to obtain the binary image, specified as a number in the range [0, 1]. Small threshold values typically result in small foreground regions in `BW`, and large threshold values produce large foreground regions.

Example: `0.5`

Data Types: `double`

Column index of reference pixels, specified as a numeric vector. `C` must contain values that are valid pixel indices in `W`.

Example: `[50 75 93]`

Data Types: `double`

Row index of reference pixels, specified as a numeric vector. `R` must contain values that are valid pixel indices in `W`.

Example: `[48 71 89]`

Data Types: `double`

Plane index of reference pixels, specified as a numeric vector. `P` must contain values that are valid pixel indices in `W`.

Example: `[2 4 7]`

Data Types: `double`

## Output Arguments

collapse all

Segmented image, returned as a logical array of the same size as `W`.

Data Types: `logical`

Normalized geodesic distance map, returned as a numeric array of the same size as `W`. If `W` is of data type `single`, then `D` is of data type `single`. Otherwise, `D` is of data type `double`.

Data Types: `double` | `single`

## Tips

• `imsegfmm` uses double-precision floating point operations for internal computations for all classes except class `single`. If `W` is of data type `single`, `imsegfmm` uses single-precision floating point operations internally.

• `imsegfmm` sets pixels with `0` or `NaN` weight values to `Inf` in the geodesic distance image `D`. These pixels are part of the background in the segmented image `BW`.

## References

[1] Sethian, J. A. Level Set Methods and Fast Marching Methods: Evolving Interfaces in Computational Geometry, Fluid Mechanics, Computer Vision, and Materials Science, Cambridge University Press, 2nd Edition, 1999.

## Version History

Introduced in R2014b

expand all