# 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:

• f is the image that we want to segment,
• S can be an array or a scalar: if it is an array, it has the same size than the image, and it specifies where are the seeds from which the region is going to grow. If it is a scalar, it defines an intensity value from which, if a pixel in the image has bigger or equal value of intensity, it is turned in to a seed.
• T can also be an array or a scalar and has the same meaning than S. It indicates if a limit from which a pixel is considered, is associated with a seed or not. Also, for that pixel to be considered part of a region, it needs to have a connectivity of 8, and fulfill the limit condition.
• g is the image with the detected regions,
• NR is the number of regions detected,
• SI is a black and white image, where in white we can see the seed pixels, and
• TI is a black and white image, where in white we can see the pixels that fulfil the limit condition.

For example, given this original image:

First step: Select the seed points:

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

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

## 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:

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:

## 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.

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.

You can practice these concepts using snippet06.