13. Segmentation¶
Video Resources
13.1. Thresholding¶
This concept was introduced as one of the Monadic Operations and also mentioned in relation to Color Images. The objective is to form a logical image from gray-scale images such that regions of true values in the image represent sets of consistent pixels. Hopefully, each pixel in a set of connected pixels (a blob) are points covering the same object.
The gray-scale input images may come from simple image intensity, color Chromaticity Coordinates, or other more advanced measurements such as depth measured from Stereo Cameras.
Useful tools for determining threshold levels and criteria include histograms, image intensity lines, or Otsu’s algorithm for automatically finding threshold values. We usually think of a threshold as a fixed value, but it could also be a relationship between corresponding pixels in other gray-scale images, for example (red > green).
Note that with MATLAB’s vector processing capabilities, generating a logical
image only requires use of a logical operator. Since MATLAB uses 1 and 0 to
represent true and false in logical images, the element-wise multiply
can be used to perform an AND operation between two logical images.
In the following line of code, the variable I_logical
is true where the
image im
is greater than 0.5 and less than 0.8.
I_logical = (im > 0.5) .* (im < 0.8);
Note
Thresholding can be fragile. Changes in lighting and background can change
the needed threshold values. If possible, keep things consistent. In
some cases, it may be useful to use Otsu’s algorithm to dynamically pick
threshold values. thresh = otsu(image);
Otsu’s algorithm is tricky
to use for picking more than one threshold value. An automated
option for sorting values into multiple groups
is the k-means clustering algorithm.
13.2. Finding Blobs¶
Recall that blobs are regions of connected (4 or 8 neighborhood) true pixels of a logical image. The algorithm for finding blobs is called connected component analysis or connectivity analysis.
Here is a brief explanation of the algorithm in pseudo-code.
for each row:
for each column:
if pixel is true:
if pixel is connected to a blob: pixel joins the blob
else: start a new blob and pixel joins it.
if pixel is connected to two blobs: merge blobs into one blob
A simple function for finding and labeling blobs is ilabel
.
>> im = iread('shark2.png');
>> [label, m] = ilabel(im);
Read the documentation to find out about additional output
variables from the ilabel
function.
In the labeled image, the pixels inside each blob have the same, unique value. An image containing only one blob is easily found:
>> blob = (label == 2);
The set of coordinates for the blob pixels is given by:
>> [v, u] = find(blob)
From the list of u
and v
values, the geometric center and a bounding
box are easy to find.
>> figure, idisp(blob)
>> plot_box(min(u), min(v), max(u), max(v), 'g')
>> center = [(min(u) + max(u))/2; (min(v) + max(v))/2]
A more advanced function for finding blobs is iblobs
, which also reports
shape, size, and location features of the blobs. We will discuss the
features that iblobs
can find in Image Features.