Segmentation based in regions

Threshold

MATLAB has an automatic function that searches for a threshold in the histogram. The calculation is based in the optimization of the average intensity in both sides of the threshold. The function has the following syntax:

T = graythresh(f)

where f is the original image and T is the threshold. The threshold is returned in the range of [0,1], so, in order to calculate the threshold image, we need to use:


T = T *255;
g = f <= T;

This converts to black everything that is bigger than the threshold, and the rest to white. If we wanted the opposite, we just change >= to <=.

Region grow

[g, NR, SI, TI] = regiongrow(f, S, T)

where:

For example, given this original image:

Region grow example 1

First step: Select the seed points:

Region grow example 2

Second step: Select the points that fulfill the limit condition set by T:

Region grow example 3

Third step: Pick the points that have connection 8 with respect to the seed points:

Region grow example 4

Split and merge

g = splitmerge(f, mindim, fun)

where f is the original image, mindim is an integer number, power of 2, which specifies the minimum size of a region, and fun is a function that must be passed to MATLAB, with the following syntax:

flag = predicate(region)

where region is the region for which we want to check that it fulfills a certain condition, and flag is true or false.

This is how it works. First, we divide the image in four regions:

Split-merge example 1

Then, we check if the four regions fulfill the fun condition. If there is a region that results in "false", we divide it in four regions again:

Split-merge example 2

Split-merge example 3

Crests and valleys

L = watershed(D)

This function returns a matrix of the same size than the image we are processing. It marks the points where there are crests.

Watershed example 1

To calculate the "watershed image", we can use this MATLAB function:

D = bwdist(f)

which calculates the distance of every pixel of the image f to the nearest white pixel.

Watershed example 2

You can practice these concepts using snippet06.