Escolar Documentos
Profissional Documentos
Cultura Documentos
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).
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
Questions
Why does the watershed of the gradient (step 4 above) lead to oversegmentation?
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
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.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
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
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
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:
>>
>>
>>
>>
>>
>>
>>
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
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)
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
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.