3-Binary Image Analysis I
3-Binary Image Analysis I
The 4-neighborhood N4[r, c] of pixel [r, c] The 8-neighborhood N8 [r, c] of pixel [r, c]
includes pixels [r - 1, c], [r + 1, c], [r, c -1], and [r, includes each pixel of the 4-neighborhood plus
c + 1], which are often referred to, as its north, the diagonal neighbor pixels [r - 1, c -1], [r - 1, c
south, west, and east neighbors, respectively. + 1], [r + 1, c - 1], and [r + 1, c + 1], which can be
referred to as its northwest, northeast,
southwest, and southeast neighbors, respectively.
z1 z2 z3 T
Z5’ w1 w2 w3
z4 z5 z6
w4 w5 w6 z5’ = R = w1z1 + w2z2 + ... + z9w9
z7 z8 z9
w7 w8 w9
1 1 1 1 1 1
g[ , ] 1 1 1 g[ , ] 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0 0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0 0 0 90 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
g[ , ] 1 1 1 g[ , ] 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 10 20 0 0 0 0 0 0 0 0 0 0 0 10 20 30
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0 0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0 0 0 90 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 1 1 1 1 1
g[ , ] 1 1 1 g[ , ] 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0
?
0 0 0 90 0 90 90 90 0 0 0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 50
?
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0 0 0 90 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 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
0 0 0 90 90 90 90 90 0 0 0 30 60 90 90 90 60 30
0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 30 20 10
0 0 90 0 0 0 0 0 0 0 10 10 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
?
0 0 0 0 0 0
0 1 0 0 1 0
0 0 0 0 0 0
- -
0 0 0 1 1 1 0 0 0 1 1 1
0
0
2
0
0
0
1
1
1
1
1
1
? 0
0
2
0
0
0
1
1
1
1
1
1
1 0 -1
2 0 -2
1 0 -1
Sobel
Vertical Edge
Binary image analysis I 33 Binary image analysis I (absolute value) 34
Horizontal Edge
Binary image analysis I (absolute value) 35 Binary image analysis I 36
◼ Step 2: The values in these virtual rows and
columns can be set arbitrarily to zero or some
40 40 80 80 80
other constant or, as has been done here, they can
40 40 80 80 80
merely duplicate the closest row (or column) to
40 40 80 80 80
them.
40 40 80 80 80
40 40 80 80 80
foreground
• •
• •
background
◼ It sequences through the four external masks and ◼ Similarly, the function internal-match(L, P)
returns true if the sub-image with top left pixel returns true if the subimage with top left pixel [L,
[L, P] matches one of them, false otherwise. P] matches one of the four internal masks, and
false otherwise.
0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 1
0 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1
E I
E=4
E = 1, I = 1
E=4
|(I - E) / 4 |= |(2 - 20) /4| 4 objects
E=4
E = 3, I = 1
E=4
◼ There are a number of different algorithms for the ◼ Other algorithms were designed for larger images
connected components labeling operation. that may not fit in memory and work on only two
◼ Some algorithms assume that the entire image can or several rows of the image at a time.
fit in memory and employ a simple, recursive
algorithm that works on one component at a time, ◼ Two different algorithms are commonly used:
but can move all over the image while doing so. ❑ The recursive search algorithm
❑ A row-by-row algorithm that uses a special union-find
data structure to keep track of components
◼ Suppose that B is a binary image with 1: To negate the binary image, so that all the “1”
MaxRow + 1 rows and MaxCol + 1 columns. pixels become “-1”s.
◼ We wish to find the connected components of the ❑ This is needed to distinguish unprocessed pixels (-1)
1-pixels and produce a labeled output image LB from those of component label 1.
in which every pixel is assigned the label of its ❑ This is accomplished with a function called negate that
connected component inputs the binary image B and outputs the negated
image LB, which will become the labeled image.
1 1 0 1 1 1 -1 -1 0 -1 -1 -1
1 1 0 1 0 1 -1 -1 0 -1 0 -1
2: Finding the connected components becomes one 1 1 1 1 0 0 -1 -1 -1 -1 0 0
of B: Binary
image
LB: Negated
image
❑ finding a pixel whose value is -1 in LB,
❑ assigning it a new label 1, and
❑ calling procedure search to find its neighbors that
have value -1 and recursively repeat the process for 1 -1 0 -1 -1 -1
these neighbors -1 -1 0 -1 0 -1
-1 -1 -1 -1 0 0
Attention
◼ The type of neighborhood you choose affects the number of objects
found in an image and the boundaries of those objects. For this reason, ◼ Compute the connected components of a
the results of many morphology operations often differ depending upon binary image.
the type of connectivity you specify.
◼ This algorithm is a set of five procedures:
◼ For example, if you specify a 4-connected neighborhood, this binary
image contains two objects; if you specify an 8-connected recursive-connected-components,
neighborhood, the image has one object.
(1) negate,
0 0 0 0 0 0 (2) find-components,
0 1 1 0 0 0
0 1 1 0 0 0 (3) search,
0 0 0 1 1 0 (4) neighbors
0 0 0 1 1 0
(5) print
A Row-by-Row Labeling Algorithm 1.Scan the image from left to right, top to bottom; if
the pixel is 1 then
a) if only one of the upper or left pixels has a label, copy
◼ The classical algorithm
this label to current pixel
◼ The algorithm makes two passes over the image: b) if both have the same label, copy this label
❑ One pass to record equivalences and assign temporary c) if they have different labels, copy one label and mark
labels. these two labels as equivalent
❑ The second pass to replace each temporary label by the d) if there are no labeled neighbors, assign it a new label
label of its equivalence class.
2. If there are no neighbors, go to 1
3. Scan the labeled image and replace all equivalent
labels with a common label
◼ Union-Find Structure
7
Union: merging two sets into one 3
1. The first pass of the algorithm performs label 3. A second pass through the image then performs
propagation (傳播) to propagate a pixel’s label a translation, assigning to each pixel the label of
to its neighbors to the right and below it. its equivalence class.
2. Whenever a situation arises in which two
different labels can propagate to the same pixel,
the smaller label propagate and each such
equivalence found is entered in the union-find
structure