Mata kuliah : T0283 - Computer Vision
Tahun : 2010
After carefully listening this lecture, students will be able to do the following : show how connected component labeling is performed and its uses in shape classifier demonstrate object area and perimeter calculation based on binary images
January 20, 2010 T0283 - Computer Vision 3
Localized perspective in binary image processing
Low-level vision
PHIL
High-level vision
January 20, 2010
Holistic perspective in binary image analysis
T0283 - Computer Vision 4
X’
Y’
Y
X
January 20, 2010
X and Y are connected X’ and Y’ are NOT connected a and b are connected if there exists a path from a to b
Notation: if a and b are connected, we write a ~ b
T0283 - Computer Vision 5
Connected Components in Digital Images
A set S of pixels is a CC if there is at least one path that joins every pair {p,q} of pixels in S, and contains exclusively of pixels in S.
Two types of connectivity: 4 - (edge) connectivity and 8-
(vertex) connectivity
January 20, 2010 T0283 - Computer Vision 6
Two pixels are c-adjacent (c=4 or 8) if they share at least an edge (c=4), or a vertex (c=8).
Two pixels are
(c=4,8) pixels.
c-connected (c=4 or 8) if it is possible to find a path between these two pixels through pairs of c-adjacent
A c-connected component is a maximal connected set where each pixel is c-connected to other pixels in the set.
January 20, 2010 T0283 - Computer Vision 7
q p p ~ q no matter 4-neighbors or 8-neighbors q p p ~ q only when 8-neighbors is considered
January 20, 2010 T0283 - Computer Vision 8
original binary image
January 20, 2010
If 4-neighbors, three connected components
T0283 - Computer Vision
If 8-neighbors, two connected components
9
Connected Component Labeling
1
January 20, 2010
2
4
T0283 - Computer Vision
3
5
10
>help bwlabel
BWLABEL Label connected components in binary image.
L = BWLABEL(BW,N) returns a matrix L, of the same size as BW, containing labels for the connected components in BW. N can have a value of either
4 or 8 , where 4 specifies 4-connected objects and 8 specifies
8-connected objects; if the argument is omitted, it defaults to 8 .
The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one object, the pixels labeled 2 make up a second object, and so on.
[L,NUM] = BWLABEL(BW,N) returns in NUM the number of connected objects found in BW .
See also bwareaopen, bweuler, bwlabeln, bwselect, label2rgb.
January 20, 2010 T0283 - Computer Vision 11
EN=0 EN=-1
EN=-3
Euler Number EN=number of connected components – number of holes
January 20, 2010 T0283 - Computer Vision 12
CC Algorithm
Process the image row by row
Assign a label to the first pixel of each CC
Otherwise assign its label by propagating from left or top
January 20, 2010
1
1
1
1
1
1 1
2
2
2
?
T0283 - Computer Vision
Clash!
(equivalence)
13
Propagate the smaller label in case of clash
Record the equivalence in a table
After the entire image is processed, find the set of equivalence classes
Second pass replaces each label with its equivalent class
Two passes!
January 20, 2010 T0283 - Computer Vision 14
January 20, 2010
X
X=X-(X B) or
X
X=(X B) – B
T0283 - Computer Vision 15
Chain Codes Boundary Representation
4-directional chain code:
0033333323221211101101
January 20, 2010 T0283 - Computer Vision
8-directional chain code:
076666553321212
16
Chain code representation is conceptually appealing, yet has the following two problems
Dependent on the starting point
Dependent on the orientation
To use boundary representation in object recognition, we need to achieve invariance to starting point and orientation
Normalized codes
Differential codes
January 20, 2010 T0283 - Computer Vision 17
33001122
January 20, 2010
33001122
30011223
00112233
01122330
11223300
12233001
22330011
23300112
Sort rows
00112233
01122330
11223300
12233001
22330011
23300112
33001122
30011223
T0283 - Computer Vision
First row gives the normalized chain code
00112233
18
90 o
January 20, 2010
33001212 normalize
00121233
Differential coding:
33010122 normalize
01012233 d k
=c k
-c k-1
(mod 4) for 4-directional chain codes d k
=c k
-c k-1
(mod 8) for 8-directional chain codes
T0283 - Computer Vision 19
Shape Numbers= Normalized Differential
Chain Codes
Differential code: d k
=c k
-c k-1
(mod 4)
33001212 differentiate normalize
01011311
33010122 differentiate
10113110 normalize
01011311
Note that the shape numbers of two objects related by 90 o rotation are indeed identical
January 20, 2010 T0283 - Computer Vision 20
Examples : Chain Encoding
2 unit pixel
1 unit pixel
January 20, 2010 y
Encoding start point x
T0283 - Computer Vision 21
Perimeter Calculation
Start
3
4
5
2
P
1
Direction 0
7
6
1 1 0 0 0 0 6 0 6 6 6 4 6 4 4 4 4 3 3 2
Perimeter P = S
E
+ V2 S
O units
= 16 + 4 V2 = 21.66 units
January 20, 2010 T0283 - Computer Vision 22
Area Calculation
Y Y
Direction 0
Additive comp. = 1 x y
Direction 5
Subtractive comp = (1 x y) – 0.5
Y
Y
Direction 1
Subtrac. comp. = (1 x y) + 0.5
January 20, 2010
Direction 2 dan 6
Zero component (neutral)
T0283 - Computer Vision 23
Area Calculation (cont’d)
3
2
1
Subtractive
4
P
0
Additive y-coordinate
7
6
5
6
5
Start
5 6 7
4
1 1 0 0 0 0 6 0 6 6 6 4 6 4 4 4 4 3 3 2
4
3
2
3
Area = 5.5 + 6.5 + 7 + 7 + 7 + 7 + 0 + 6 + 0 + 0 + 0 – 3 + 0 – 2 – 2
– 2 – 2 – 2.5 – 3.5 + 0
= 29 square units
January 20, 2010 T0283 - Computer Vision 24
January 20, 2010 T0283 - Computer Vision 25
Run Length Encoding (RLE)
0
0
0
0
0
0
0
0
0
0
0
0
Segmen citra biner
0 0
# #
# #
# #
0 0
0 0
0
#
0
0
0
# #
#
0
0
0
#
0
#
# #
0 0
0
0
0
0
0
0
10(0), 3(1), 1(0), 1(1), 3(0), 4(1), 4(0), 3(1), 1(0), 1(1), 6(0), 2(1), 9(0)
January 20, 2010 T0283 - Computer Vision 26
Chord Encoding baris
January 20, 2010
Segmen citra biner
0
0
0
0
0
0
0
0
0
0
0
#
#
#
0
0
#
#
#
0
0
#
#
#
0
0
0
#
0
#
0
#
0
#
#
0
0
0
0
0
0 0 0 0 0 0 0 0
1 (2,4) (6,6); 2 (2,5); 3 (2,4) (6,6); 4 (5,6).
kolom
T0283 - Computer Vision 27
Matlab Implementation on Shapes Classifier
Step 1: Read image
Step 2: Convert image from rgb to gray
Step 3: Threshold the image
Step 4: Invert the Binary Image
Step 5: Find the boundaries Concentrate
Step 6: Determine Shapes properties
Step 7: Classify Shapes according to
January 20, 2010 T0283 - Computer Vision 28
Step 1: Read image
RGB = imread('test.bmp'); figure, imshow(RGB), title('INPUT IMAGE');
January 20, 2010 T0283 - Computer Vision 29
Step 2: Convert image from rgb to gray
GRAY = rgb2gray(RGB); figure,imshow(GRAY),title('GRAY IMAGE');
January 20, 2010 T0283 - Computer Vision 30
Step 3: Threshold the image. threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold); figure, imshow(BW), title('BINARY IMAGE');
January 20, 2010 T0283 - Computer Vision 31
Step 4: Invert the Binary Image
BW = ~ BW; figure,imshow(BW),title('INVERTED BINARY IMAGE');
January 20, 2010 T0283 - Computer Vision 32
Step 5: Find the boundaries
Concentrate only on the exterior boundaries.
Option 'noholes' will accelerate the processing by preventing bwboundaries from searching for inner contours.
[B,L] = bwboundaries(BW, 'noholes');
%[L, N] = bwlabel(BW,8);
Step 6: Determine objects properties
STATS = regionprops(L, 'all');
% we need 'BoundingBox' and 'Extent'
January 20, 2010 T0283 - Computer Vision 33
Step 7: Classify Shapes according to properties
% Square = 3 = (1 + 2) = (X=Y + Extent = 1)
% Rectangular = 2 = (0 + 2) = (only Extent = 1)
% Circle = 1 = (1 + 0) = (X=Y , Extent < 1)
% UNKNOWN = 0 for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3) - STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 ); centroid = STATS(i).Centroid; switch W(i) case 1 plot(centroid(1),centroid(2),'wO'); case 2 end end plot(centroid(1),centroid(2),'wX'); case 3 plot(centroid(1),centroid(2),'wS');
January 20, 2010 T0283 - Computer Vision 34
Step 7: Classify Shapes according to properties (cont’d) figure, imshow(RGB), title('HASIL IDENTIFIKASI SQUARE, CIRCLE &
RECTANGLE'); hold on
January 20, 2010 T0283 - Computer Vision 35