Digital Image Processing
Labeling Connected Components
PGS.TS. NGUYEN TAN LUY
Course Website: http://www.comp.dit.ie/bmacnamee
2
of
39
3
of
39
Contents
• The concepts discussed thus far are applicable mostly to
all foreground (or all background) individual pixels and
their immediate neighbors.
• In this section we consider the important "middle ground"
between individual foreground pixels and the set of all
foreground pixels.
• This leads to the notion of connected components, also
referred to as objects in the following discussion.
4
of
39
Labeling Connected Components
5
of
39
Labeling Connected Components
N8(p).
N4(p). ND(p).
A pixel p at coordinates (x, y) has
two horizontal and two vertical
neighbors (x + 1, y), (x - 1, y), (x,
y + 1 ), and (x, y - 1), N4(p).
N8(p) (x+1, y+1), (x + 1, y - 1),
(x - 1, y+1 ), (x - 1, y - 1).
N4(p) N8(p)
6
of
39
Two pixels p and q are said to be 4-adjacent if 𝑞 ∈ 𝑁4 (𝑝). Similarly, p and q
are said to be 8-adjacent if 𝑞 ∈ 𝑁8 (𝑝). Figures above illustrate these
concepts.
A path between pixels 𝑝1 and 𝑝𝑛 is a sequence of pixels 𝑝1 , 𝑝2 , • • • , 𝑝𝑛−1 ,
𝑝𝑛 , such that 𝑝𝑘 is adjacent to 𝑝𝑘+1 for 1 ≤ 𝑘 < 𝑛. A path can be 4-
connected or 8-connected, depending on the type of adjacency used.
Two foreground pixels p and q are said to be 4-connected if there exists
a 4-connected path between them, consisting entirely of foreground pixels.
They are 8-connected if there exists an 8-connected path between them.
For any foreground pixel, p, the set of all foreground pixels connected to it is
called the connected component containing p.
7
of
39
Quick Example
A connected component was just
defined in terms of a path, and the
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
definition of a path in turn depends on
the type of adjacency used. This
implies that the nature of a connected
component depends on which form of
adjacency we choose, with 4- and 8-
adjacency being the most common.
Figure on the left illustrates the effect
that adjacency can have on
determining the number of connected
components in an image. Figure (a)
shows a small binary image with four
4-connected components. Figure (b)
shows that choosing 8-adjacency
reduces the number of connected
components to two
a) N4(p) b) N8(p)
8
of
39
clc
f=im2double(rgb2gray(imread('h6.jpg')));
f=im2bw(f,graythresh(f));
imshow(f)
hold on
[L, n] = bwlabel(f);
for k =1:n
[r, c] = find (L==k) ;
rbar = mean(r) ;
cbar = mean(c) ;
plot ( cbar , rbar , 'Marker' , 'o' , 'MarkerEdgeColor', 'k'
,'MarkerFaceColor' , 'k' , 'MarkerSize', 10 ) ;
plot (cbar, rbar , 'Marker' , '*' , 'MarkerEdgeColor', 'w' ) ;
end h6.jpg
9
of
39
Function regionprops
D = regionprops ( L , properties)
D i s a structure of length max(L (:))
- properties