Você está na página 1de 6

ELEC3601/ELEC7608 Introduction to Image Formation

Practical 6: Segmentation, and representation and description


The aim of this practical is to introduce you to the watershed transform, chain codes, signatures, skeletons,
Fourier descriptors, general region descriptors, and the grey-level co-occurrence matrix.

Part I

Segmentation
1

DImPAL
In this part of the practical we will use DImPAL. DImPAL stands for Digital Image Processing and
Analysis Language. It is an interpreted language similar to MATLAB. It is research-grade code and
not a commercial product.
The reason for using DImPAL is that MATLAB does not directly implement the marker-based watershed
transform.
DImPAL can operate on images sampled on either square or hexagonal grids. Boolean, byte, integer and
real data types are supported for image pixels. DImPAL can be used interactively to execute statements,
one at a time, entered at the command prompt, or it can be used to execute a program (written as a plain
text file).

Bi-level thresholding: Automatic threshold selection


One approach to automatic threshold selection is to choose a threshold that partitions the histogram
into two histograms such that the sum of the entropy (information content) of each sub-histogram is
maximised. Recall from the lectures that an estimate of the entropy or information content of an image
f is given by
L1
X
pf (i) log pf (i)
H=
i=0

where pf is its normalised grey-level histogram, i.e. discrete probability density function. For a base 2
logarithm the information content represents bits per pixel.
The function threshold in DImPAL implements this method.
>>
>>
>>
>>
>>

f = read("small-blobs.pgm")
display(f)
b = threshold(f))
display(b)
overlay(f,b[&&]!erode(b))

;Read in image f
;Apply entropy-based thresholding
;Overlay the border of b on the original image

MATLAB also includes an automatic threshold selection function. The function is called graythresh.
The function automatically chooses a threshold value based on Otsus method described in the lectures.
>>
>>
>>
>>
>>

f = imread(small-blobs.pgm);
imshow(f);
figure; imhist(f); ylim(auto);
level = graythresh(f);
figure; imshow(im2bw(f,level));

% Read in image f
% Display its histogram
% Select threshold using Otsus method
% Apply the threshold
1

Watershed transform (classic and marker-based)


Open the file wshed of blobs.txt1 in a text editor. Cut and paste this program into the command
window line by line to gain an understanding of what is happening. The program
1. Reads in an image containing dark blobs on a grey background;
2. Computes the morphological gradient (Beuchers gradient) of the image;
3. Computes the watershed transform of the gradient image (i.e. flooding from the minima);
4. Overlays the resulting watersheds on the original image to demonstrate the oversegmentation;
5. Finds the regional minima of the original image and overlays these on the image to show that they
are suitable internal markers;
6. Finds the watersheds of the original image with respect to the internal markers (the watersheds
become the external marker);
7. Finds the watersheds of the gradient image with respect to both the internal and external markers;
and
8. Shows, for comparison, the result of entropy thresholding.

Questions
Why does the watershed of the gradient (step 4 above) lead to oversegmentation?

Watershed segmentation using the distance transform


Open the file wshed of dt.txt in a text editor. Cut and paste this program into the command window
line by line to gain an understanding of what is happening. The program
1. Reads in a binary image of overlapping blobs;
2. Computes the 5-7-chamfer distance transform (DT);
3. Computes the negative of the DT;
4. Finds the regional minima of the inverted DT;
5. Computes the watershed transform of the inverted DT with respect to the regional minima; and
6. Segments the blob image by taking the set difference between the image and the watersheds.

Questions
1. Why is the segmentation unsatisfactory?
2. How can this be improved/solved?

1 Recall that the distance transform (DT) maps every pixel in a binary image to its shortest distance to a background pixel.
The result is a grey-scale image. The DImPAL function distance transform implements the DT for several different metrics The
function has the syntax:

f = distance transform(X,method,background)
where X is a binary image; method is one of honeycomb, chamfer35, chessboard,
cityblock, chamfer34, chamfer57, chamfer5711, and Euclidean; and background is
either 1 or 0 specifying the value of the pixels beyond the borders of X.

Part II

Representation and description


5

Chain codes
A chain code represents the boundary of a region by a connected sequence of straight-line segments of
specified length and direction.
The simplest approach is to use the connectivity of the grid on which the image is defined.
In the case of the 4-connected square grid, for example, a chain code can be generated by tracking along
the boundary pixels in a clockwise direction and assigning a direction to each line segment connecting two
pixels.
Chain codes based on the directional coding scheme below are called Freeman chain codes.

Neither MATLAB nor the Image Processing Toolbox (IPT) directly support chain codes. The following
example makes use of several M-functions defined in Digital Image Processing Using MATLAB known
as the DIPUM toolbox (see References). These functions are: boundaries, bound2im, bsubsamp, and
fchcode. Refer to the book for source listings and more detail.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

f = imread(noisy circular stroke.tif); % Read in image f


imshow(f)
h = fspecial(average,9);
% Define a uniform filter h
g = imfilter(f,h,replicate);
% Filter f
g = im2bw(g,0.5);
% Threshold to obtain a binary image
g = imopen(g,strel(disk,2));
% Perform binary opening
figure; imshow(g)
b = boundaries(g); % Returns a cell array of coordinates of the pixels in the
b = b{1};
% 8-connected exterior boundary in a clockwise direction
[M N] = size(g);
g = bound2im(b, M, N, min(b(:,1)), min(b(:,2))); % Convert to binary image
figure; imshow(g)
[s, su] = bsubsamp(b,50); % Subsample onto a grid with spacing 50 pixels
g2 = bound2im(s, M, N, min(s(:,1)), min(s(:,2))); % Convert to binary image
figure; imshow(g2)
c = fchcode(su); % Compute Freeman chaincode
c.x0y0
% Coordinates of the start of the code
c.fcc
% Freeman 8-directional chain code
c.mm
% Integer of minimum magnitude
c.diff
% First difference of the code
c.diffmm
% First difference of c.mm
3

Signatures
A signature is a one-dimensional (1D) functional representation of a boundary. This reduces the original
2D boundary to a 1D function. Signatures can be generated in a variety of ways.
A simple way of generating a signature is to plot the distance from the centroid to the boundary as a
function of angle. This is what the following example does. The example makes use of several M-functions
from the DIPUM toolbox: boundaries and signature.
The following example
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

f = imread(boundary sq.tif); imshow(f)


b = boundaries(f);
bf = b{1};
g = imread(boundary triangle.tif); figure; imshow(g);
b = boundaries(g);
bg = b{1};
[st, angle, x0, y0] = signature(bf);
figure; plot(angle, st);
[st, angle, x0, y0] = signature(bg);
figure; plot(angle, st);

Skeletons
Another way of simplifying the representation of a region is to thin it down to a set of lines whilst at the
same time preserving the homotopy (connectivity) of the region.
The resulting thin lines are called the skeleton or medial axis of the input region.
The IPT function bwmorph can be used to obtain a medial axis or skeleton of a binary image. The function
uses a homotopic thinning algorithm that guarantees that the connectivity number is preserved.
Example:
>>
>>
>>
>>
>>
>>
>>

f = imread(chromo binary.tif); imshow(f)


s = bwmorph(f,skel,Inf);
% Compute homotopic skeleton
figure; imshow(s)
s1 = bwmorph(s,spur,15);
% Remove end points of lines without removing
figure; imshow(s1)
% small objects completely.
dt = bwdist(~f);
% Skeleton coincides with the ridge line of
figure; imshow(dt,[])
% the Euclidean distance transform

Fourier descriptors
Consider a K -point digital boundary in the Cartesian plane with coordinate pairs (x0 , y0 ), (x1 , y1 ),. . . , (xK1 , yK1 )
corresponding to an anticlockwise traversal of the boundary.
If we use the notation x(k) = xk and y(k) = yk then we can represent each coordinate pair as a complex
number s(k) = x(k) + jy(k).
The discrete Fourier transform (DFT) of s(k) is
a(u) =

K1
1 X
s(k)ej2uk/K .
K
k=0

The complex coefficients a(u) are called the Fourier descriptors of the boundary.
4

The inverse DFT restores s(k); i.e.


s(k) =

K1
X

a(u)ej2uk/K .

u=0

If we instead only use only the first P of the K coefficients then we end up with an approximation
sb(k) =

P
1
X

a(u)ej2uk/K .

u=0

Notes:
1. This is equivalent to setting a(u) = 0 for u > P 1 in the first equation above.
2. The same number of points exist in the reconstructed boundary, but not as many coefficients are
used to reconstruct them.
The following example makes use of several M-functions from the DIPUM toolbox: boundaries, bound2im,
frdescp, and ifrdescp.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

f = imread(chromo binary.tif);
imshow(f)
[M N] = size(f);
b = boundaries(f);
b = b{1};
bim = bound2im(b, M, N);
figure; imshow(bim);
z = frdescp(b);
length(z)
z546 = ifrdescp(z, 546);
z546im = bound2im(z546, M, N);
figure; imshow(z546im)
z14 = ifrdescp(z, 14);
z14im = bound2im(z14, M, N);
figure; imshow(z14im)
z8 = ifrdescp(z, 8);
z8im = bound2im(z8, M, N);
figure; imshow(z8im)

% Obtain boundary coordinates


% Convert to binary image
% Compute Fourier descriptors
% Reconstruct using approx. first 50%

% Reconstruct using approx. first 1.25%

% Reconstruct using approx. first 0.7%

Region descriptors
The function regionprops is the IPTs principal tool for computing region descriptors. The function has
the syntax:
props = regionprops(L, properties)
where L is a label matrix (typically produced by bwlabel) and properties are the properties to be measured
for each connected component; e.g. Area, BoundingBox, Centroid, EulerNumber
Example:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

bw = imread(text.png); imshow(bw)
L = bwlabel(bw);
% Label each 8-connected component
figure; imshow(L,[]);
% Display the labelled image
colormap(colorcube);
% Display as false colour
s = regionprops(L, centroid);
% Compute the centroid of each region
centroids = cat(1, s.Centroid);
% Create an nx2 array of coordinates
figure; imshow(bw)
hold on
plot(centroids(:,1), centroids(:,2), b*) % Plot a blue asterisk at
hold off
% each centroid

The Minkowski functionals can be computed using the hit-or-miss transform. The IPT function bwhitmiss
function implements this transform.
5

10

The grey-level co-occurrence matrix

A drawback of texture measures derived from the grey-level histogram is that they do not carry any
information regarding the spatial relationship between pixels in a texture.
One solution is to consider not only the distribution of intensities, but also the positions of pixels with
equal or nearly equal intensity values.
This is the basis of the well-known grey-level co-occurrence matrix (GLCM).
The GLCM is a square matrix C constructed such that element cij is the relative frequency with which
a pixel of grey-level i 1 occurs at a fixed distance and direction from a pixel of grey-level j 1.
The MATLAB Image Processing Toolbox provides the function graycomatrix for constructing GLCMs.
The function has the basic syntax
glcm = graycomatrix(f,Offset,[row offset col offset],
GrayLimits,[low high],
NumLevels,num levels)
where f is an input grey-scale image, row offset is the number of rows between the pixel-of-interest
and its neighbour, col offset is the number of columns between the pixel-of-interest and its neighbour,
[low high] specifies how the grey-scale values in f are linearly scaled into grey levels, and num levels
is an integer specifying the number of grey levels to use when scaling the grey-scale values in f. NOTE:
The function actually returns a matrix of counts rather than relative frequencies. The true grey-level
co-occurrence matrix can be obtained by dividing glcm element-wise by sum(glcm(:)).
0 0 0 1
1 1 0 1
Example: Let f = 2 2 1 0
1 1 0 2
0 0 1 0
(i.e. for a given pixel we consider
follows:

2
1
0 . The GLCM for a distance d of one pixel and a direction = 45
0
1
one pixel to the right and one down) can be obtained in MATLAB as

>> f = [0 0 0 1 2; 1 1 0 1 1; 2 2 1 0 0; 1 1 0 2 0; 0 0 1 0 1]
>> glcm = graycomatrix(f,Offset,[-1 -1],GrayLimits,[],NumLevels,3)
The IPT function graycoprops can be used to compute texture descriptors from an unnormalised GLCM
(i.e. a GLCM of counts rather than relative frequencies). The function has the basic syntax:
stats = graycoprops(glcm,properties)
where properties include: Contrast, Correlation, Energy, Homogeneity, or All. Consult doc graycoprops for more information. NOTE: The function actually normalises the GLCM before computing
properties.

References
R.C. Gonzalez, R.E. Woods, S.L. Eddins (2004), Digital Image Processing Using MATLAB. Prentice Hall.

Você também pode gostar