chapter 5 binary vision - griffith university · 2006. 9. 18. · for p:=0 to maxcol {grayval:=...
TRANSCRIPT
Chapter 5 Binary Vision
5.1 Binary ImageA binary image can be obtained from a gray-scale or color image by selecting a subset of the pixels as foreground pixels and leaving the rest as background pixels
Thresholding a gray-scale image
Thresholding based on histogramCompute Histogram H of gray-level image I
procedure histogram (I,H) {
“Initialize all bins to zero”
for i:=0 to MaxVal
H[i]:=0;
“Compute values”
for L:=0 to MaxRow
for P:=0 to MaxCol {
grayval:= I[L,P];
H[grayval]:= H[grayval] + 1;
}
}
5.2 Mask
Applying a mask to an image is a basic concept in image processing – convolution.
(1) What is a mask: A mask is a set of pixel positions with values called weights. A 3x3 mask is as follows.
w1 w2 w3
w4 w5 w6
w7 w8 w9
1 w2 w3
w4 w5 w6
w7 w8 w9
11 1
111
1 1 1
1 w2 w3
w4 w5 w6
w7 w8 w9
21 1
242
1 2 1
w1 w2 w3
w4 w5 w6
w7 w8 w9
(2) How to use a mask: For any point (x,y) in the image, align the center of the mask to it. We have
z1 z 2 z3
z 4 z5 z6
z7 z8 z9
(x,y)
zwyxf ii
inew ∑==
9
1),(
5.3 Counting objectsApplications: E.g., count cells in biology, particles in materials
particles in metal cells in biology
5.3 Counting objects -- method 1
Method 1:
E Masks (1 – foreground, 0 – background) for detecting external corners
0 00 1
0 01 0
1 00 0
0 10 0
I Masks (1 – foreground, 0 – background) for detecting internal corners
1 11 0
1 10 1
0 11 1
1 01 1
5.3 Counting objects -- method 1
Compute the number of foreground objects in binary image B
Count_objects (B) {
E := 0;
I := 0;
for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 {
if external_match (L,P) then E := E + 1;
if internal_match (L,P) then I := I + 1;
}
return ((E-I)/4);
}
5.3 Counting objects -- method 1
Limitations:
• must be 4-connected foregrounds
• no interior holes
5.3 Counting objects -- method 2
Connected Components Labelling:
A connected components labeling of a binary image is a labeled image in which the value of each pixel is the label of its connected components
Example =>
5.3 Counting objects -- method 2
5.3 Counting objects -- method 2
A Recursive Labelling Algorithm:Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L P); }
}
5.3 Counting objects -- method 2A Recursive Labelling Algorithm:Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1x4
2 3
1 2 3x7
4 56 8
4-neighborhood
8-neighborhood
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 1 1 1 0 0 00 0 0 0 0 1 0 1 0 1 10 0 0 0 0 1 0 1 0 0 00 0 1 1 0 1 1 1 0 0 00 1 0 1 0 1 0 1 0 0 00 0 0 1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
-1 0 0 0 0 0 0 0 0 0 0
-1 -1 0 0 0 -1 -1 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 0
-1 -1 0 0 0 -1 -1 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 0
1 -1 0 0 0 -1 -1 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 -1 -1 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 2 -1 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 2 2 -1 0 0 0
0 0 0 0 0 -1 0 -1 0 -1 -1
0 0 0 0 0 -1 0 -1 0 0 0
0 0 -1 -1 0 -1 -1 -1 0 0 0
0 -1 0 -1 0 -1 0 -1 0 0 0
0 0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 2Recursive_connected_components (B, LB) {LB := negate (B);Label := 0;Find_components (LB, label);Print (LB);}
Find_components (LB, label) {for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1 if LB[L,P] == -1 then {label := label + 1;search (LB, label, L, P); }
}
search (LB, label, L, P) {LB[L,P] := label;Nset := neighbors (L, P);For each [L’, P’] in Nset {
If LB[L’, P’] == -1 then search search (LB, label, L’, P’);}
}
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 2 0 2 0 0 00 0 4 4 0 2 2 2 0 0 00 4 0 4 0 2 0 2 0 0 00 0 0 4 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 5 0
Problem: ??
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 1 1 1 0 0 00 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 1 0 0 00 0 1 1 0 1 1 1 0 0 00 1 0 1 0 1 0 1 0 0 00 0 0 1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
-1 0 0 0 0 0 0 0 0 0 0-1 -1 0 0 0 -1 -1 -1 0 0 00 0 0 0 0 -1 0 -1 0 -1 -10 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 0-1 -1 0 0 0 -1 -1 -1 0 0 00 0 0 0 0 -1 0 -1 0 -1 -10 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 -1 -1 -1 0 0 00 0 0 0 0 -1 0 -1 0 -1 -10 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 -1 -1 0 0 00 0 0 0 0 -1 0 -1 0 -1 -10 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 -1 0 -1 0 -1 -10 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 -1 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 2 0 0 00 0 -1 -1 0 -1 -1 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 2 0 0 00 0 4 4 0 5 2 -1 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 2 0 0 00 0 4 4 0 5 2 2 0 0 00 -1 0 -1 0 -1 0 -1 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 2 0 0 00 0 4 4 0 5 2 2 0 0 00 4 0 4 0 5 0 2 0 0 00 0 0 -1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 -1 0
5.3 Counting objects -- method 3Classical Connected Components AlgorithmMakes two passes over the image
Classical_connected_components (B, LB) {LB := negate (B);Label := 0;for L:= 0 to MaxRow - 1
for P:= 0 to MaxCol - 1if LB[L,P] == -1 then {
if L_LAset <> 0 then{LB[L,P] = Label of LAset}else {label := label + 1;LB[L,P] := label;}
}}Print (LB);
1 0 0 0 0 0 0 0 0 0 01 1 0 0 0 2 2 2 0 0 00 0 0 0 0 2 0 2 0 3 30 0 0 0 0 0 0 2 0 0 00 0 4 4 0 5 2 2 0 0 00 4 0 4 0 5 0 2 0 0 00 0 0 4 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 6 0
Problem: overlapping of objects due to segmentation error
5.4 Binary MorphologyFunction: Extract and alter the structure of objects in a binary image.
Why?Binary image is obtained from thresholding. It contains unwanted information, such as noisy particles, objects touching each other, and etc. Morphological operations can address or alleviate these problems.
5.4 Binary MorphologyIllustration?Binary image is obtained from thresholding. It contains unwanted information, such as noisy particles, objects touching each other, and etc. Morphological operations can address or alleviate these problems.