# mmdleaf
Segment a leaf from the background

## Description

This experiment demonstrate the use of the toggle to segment an image with ill defined borders.

## Demo Script

### Reading

The gray scale image to be processed is read.

    >>> f = mmreadgray('leaf.tif')

    >>> mmshow(f)

f |

### Simple thresholding

Although the leaf was pictured on a light background, it is not possible to fully segment the leaf using a simple thresholding

    >>> f_low=mmthreshad(f,100)

    Warning: Converting input image from int32 to uint8.

    >>> f_med=mmthreshad(f,128)

    Warning: Converting input image from int32 to uint8.

    >>> f_high=mmthreshad(f,160)

    Warning: Converting input image from int32 to uint8.

    >>> mmshow(f_low)

    >>> mmshow(f_med)

    >>> mmshow(f_high)

f_low | f_med |

f_high |

### Toggle

The toggle is a non-linear image enhancement that changes the pixel value to the maximum or the minimum in the neighborhood given by the structure element, depending which one is the nearest value. The result of the toggle is that near the edges, the image is better defined.

    >>> f1=mmtoggle(f,mmero(f,mmsedisk(7)),mmdil(f,mmsedisk(7)))

    >>> mmshow(f1)

f1 |

### Thresholding and area open

The thresholding can now be applied resulting a good definition of the leaf boarder. Small white spots can be removed by the area open filter.

    >>> f2=mmthreshad(f1,100)

    Warning: Converting input image from int32 to uint8.

    >>> f3=mmareaopen(f2,80)

    >>> mmshow(f2)

    >>> mmshow(f3)

f2 | f3 |

### Final display

For illustration, the contour of the segmented leaf is overlayed in red in the original image

    >>> f4=mmgradm(f3)

    >>> mmshow(f,f4)

f,f4 |

[`mmdholecenter`] [Up] [`mmdlith`] |
Copyright (c) 2003, Roberto A. Lotufo, UNICAMP-University of Campinas; Rubens C. Machado, CenPRA-Renato Archer Research Center.