0% found this document useful (0 votes)
20 views19 pages

3-Binary Image Analysis I

Binary image analysis is crucial for various applications like document analysis and industrial inspection, allowing algorithms to perform tasks such as counting, recognition, and localization. While binary images are easy to acquire and process with low memory requirements, they have limitations including restricted applications and the inability to extend to 3D. Understanding binary image processing techniques, such as thresholding and applying masks, provides foundational knowledge for more complex image analysis tasks.

Uploaded by

luosuochao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views19 pages

3-Binary Image Analysis I

Binary image analysis is crucial for various applications like document analysis and industrial inspection, allowing algorithms to perform tasks such as counting, recognition, and localization. While binary images are easy to acquire and process with low memory requirements, they have limitations including restricted applications and the inability to extend to 3D. Understanding binary image processing techniques, such as thresholding and applying masks, provides foundational knowledge for more complex image analysis tasks.

Uploaded by

luosuochao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Binary Image Analysis I ◼ In many applications, binary images can be used

as the input to algorithms that perform useful


tasks
❑ Document analysis
❑ Industrial inspection

Binary image analysis I 2

◼ These algorithms can handle a wide range of


basic tasks in image analysis, including:
❑ Simple counting tasks
❑ Complex tasks :
◼ recognition
◼ localization
◼ Inspection

◼ By studying binary image analysis before going


on to gray-tone and color images, one can gain
insight into the entire image analysis process

Binary image analysis I 3 Binary image analysis I 4


Binary Image Processing -Advantages Binary Image Processing-
disadvantages
◼ Easy to acquire
◼ Low memory requirement ◼ Limited application
◼ Simple processing ◼ Cannot extend to 3D
◼ Specialized lighting is required

Binary image analysis I 5 Binary image analysis I 6

1. Pixels and Neighborhoods

◼ A binary image B can be obtained from a


gray-scale or color image I through an operation
that selects a subset of the image pixels as
foreground pixels, the pixels of interest in an
Thresholding
image analysis task, leaving the rest as
background pixels to be ignored.

Binary image analysis I 7 Binary image analysis I 8


◼ In many algorithms, not only the value of a
particular pixel, but also the values of its
neighbors are used when processing that pixel.
The pixels of a binary image B are 0s and 1s; The
❑ 4-neighbors
1s will be used to denote foreground pixels, and
the 0s background pixels. ❑ 8-neighbors of a pixel

Binary image analysis I 9 Binary image analysis I 10

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.

Binary image analysis I 11 Binary image analysis I 12


2. Applying Masks to Images (filtering)

◼ Mask: A mask is a small matrix whose values are


called weights
Origins
◼ Each mask has an origin, which is usually one of 1
its positions. 1 1 1 1 2 1 1
❑ The origins of symmetric masks are usually their center 1 1 1 2 4 2 1
pixel position. 1 1 1 1 2 1 1
❑ For non-symmetric masks, any pixel location may be 1
chosen as the origin (depending on the intended use)

Binary image analysis I 13 Binary image analysis I 14

◼ Applying a mask to smooth an image


◼ The application of a mask to an input image
produces an output image of the same size as the ◼ The results are summed together to yield a single
input. output value that is placed in the output image at
❑ For each pixel in the input image, the mask is the location of the pixel being processed on the
conceptually placed on top of the image with its origin
lying on that pixel
input
❑ The values of each input image pixel under the mask is
multiplied by the weight of the corresponding mask
pixel

Binary image analysis I 15 Binary image analysis I 16


h(x,y) = T[f(x,y)]

T operates on a neighborhood of pixels

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

Binary image analysis I 17 Binary image analysis I 18

1 1 1 1 1 1

g[ , ] 1 1 1 g[ , ] 1 1 1

1 1 1 1 1 1

f [.,.] h[.,.] f [.,.] h[.,.]


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 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

Binary image analysis I 19 Binary image analysis I 20


1 1 1 1 1 1

g[ , ] 1 1 1 g[ , ] 1 1 1

1 1 1 1 1 1

f [.,.] h[.,.] f [.,.] h[.,.]


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 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

Binary image analysis I 21 Binary image analysis I 22

1 1 1 1 1 1

g[ , ] 1 1 1 g[ , ] 1 1 1

1 1 1 1 1 1

f [.,.] h[.,.] f [.,.] h[.,.]


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 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

Binary image analysis I 23 Binary image analysis I 24


g[ , ] 1 1 1
1 1 1
1 1 1 ◼ What does it do?
❑ Replaces each pixel with an average of its
f [.,.] h[.,.] neighborhood
0 0 0 0 0 0 0 0 0 0
❑ Achieve smoothing effect (remove sharp features)
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10

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

Binary image analysis I 25 Binary image analysis I 26

◼ Practice with linear filters

?
0 0 0 0 0 0
0 1 0 0 1 0
0 0 0 0 0 0

Original Original Filtered


(no change)

Binary image analysis I 27 Binary image analysis I 28


?
0 0 0 0 0 0
0 0 1 0 0 1
0 0 0 0 0 0

Original Original Shifted left


By 1 pixel

Binary image analysis I 29 Binary image analysis I 30

- -
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

Original Original Sharpening filter


- Accentuates differences with local
average

Binary image analysis I 31 Binary image analysis I 32


Other filters
Sharpening

1 0 -1
2 0 -2
1 0 -1
Sobel

Vertical Edge
Binary image analysis I 33 Binary image analysis I (absolute value) 34

How to treat the image borders?


◼ Step 1: In order to make the output image come
out the same size as the input image, we must add
some virtual rows and columns to the input image
around the edges.
1 2 1
0 0 0
-1 -2 -1
Sobel

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

Binary image analysis I 37 Binary image analysis I 38

140 + 240 + 140 +


240 + 440 + 240 +
140 + 240 + 140
40
1 40
2 40
1 80 80 80 80
1 2 1
40
2 40
4 40
2 80 80 80 80
2 4 2 640 800 1120 1280 1280
40 40 40 80 80 80 80
40
1 40
2 40
1 80 80 80 80 1 2 1
1 2 1 40 40 40 80 80 80 80 640 800 1120 1280 1280
40 40 40 80 80 80 80 40 40 40 80 80 80 80
2 4 2 640 800 1120 1280 1280
40 40 40 80 80 80 80 40 40 40 80 80 80
80 Image
1 2 1
40 40 40 80 80 80 80 smooth 640 800 1120 1280 1280
40 40 40 80 80 80 80
40 40 40 80 80 80 80
40 40 40 80 80 80 80 640 800 1120 1280 1280
40 40 40 80 80 80 80

Binary image analysis I 39 Binary image analysis I 40


◼ Step3: To normalize, we can divide the value 1 2 1
64016 = 40
obtained for each pixel by the sum of the weights 2 4 2
in the mask, in this case 16, obtaining the final 1 2 1
image 16
640 800 1120 1280 1280 40 50 70 80 80

640 800 1120 1280 1280 40 50 70 80 80

640 800 1120 1280 1280 40 50 70 80 80

640 800 1120 1280 1280 40 50 70 80 80

640 800 1120 1280 1280 40 50 70 80 80

Binary image analysis I 41 Binary image analysis I 42

3. Counting the Objects in an Images

◼ Counting the number of background objects is an


equivalent problem that can be performed with 0 0 0 0 1 0 0 1
the same algorithm by merely swapping the roles 0 1 1 0 0 0 0 0
of the two sets: E and I. background
◼ E --- the external corner patterns are 22 masks
that have three 0s and one 1-pixel.
foreground

Binary image analysis I 43 Binary image analysis I 44


Algorithm: Counting foreground
◼ I --- the internal corner patterns are 22 masks
that have three 1s and one 0-pixel. objects
◼ Compute the number of foreground objects of
1 1 1 1 1 0 0 1 binary image B.
1 0 0 1 1 1 1 1 ◼ Objects are 4-connected and simply connected.

foreground
• •
• •

background

Binary image analysis I 45 Binary image analysis I 46

Algorithm: Counting foreground objects

◼ Step1: Counting the number of external corners (E) in procedure count-objects(B);


region of 22 sub-images { E := 0;
I := 0;
◼ Step2: Counting the number of internal corners (I) in for L:= 0 to MaxRow - 1
region of 22 sub-images for P:= 0 to MaxCol - 1
◼ Step3: Counting the number of foreground objects in {
whole image: if external-match(L, P) then E:= E + 1;
if internal-match(L, P) then I:= I + 1;
I −E };
The number of foreground objects = | | return ((I - E) / 4);
4
}

Binary image analysis I 47 Binary image analysis I 48


Function external-match(L, P) Function internal-match(L, P)

◼ 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

Binary image analysis I 49 Binary image analysis I 50

◼ Example 4. Connected Components Labeling

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

Binary image analysis I 51 Binary image analysis I 52


◼ Definition --- A connected components labeling
of a binary image B is a labeled image LB in
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0
2
which the value of each pixel is the label of its 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0
2
connected component 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 2
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2
◼ A label is a symbol that uniquely names an entity. 1 1 1 1 0 1 0 1 3 3 3 3 0 4 0 2
0 0 0 3 0 4 0 2
While character labels are possible, positive 0 0 0 1 0 1 0 1
1 1 0 1 0 0 0 1 5 5 0 3 0 0 0 2
integers are more convenient and are most often 1 1 0 1 0 1 1 1 5 5 0 3 0 2 2 2
used to label the connected components.

Binary image analysis I 53 Binary image analysis I 54

◼ 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

Binary image analysis I 55 Binary image analysis I 56


Recursive Labeling Algorithm

◼ 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.

Binary image analysis I 57 Binary image analysis I 58

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

Binary image analysis I 59 Binary image analysis I 60


◼ Function neighbors (L, P):
❑ The utility function neighbors (L, P) is given a pixel
position defined by L and P.
◼ Recursive Algorithm ❑ It returns the set of pixel positions of all of its
1.Scan the image to find an unlabeled -1 pixel and assign it a new neighbors, using either the 4-neighborhood or
label L. 8-neighborhood definition.
2.Recursively assign a label L to all its -1 neighbors.
❑ Only neighbors that represent legal positions on the
3.Stop of there are no unlabeled -1 pixels
binary image are returned.
4.Go to step 1
❑ The neighbors are returned in scan-line order
◼ Features
-Inefficient for sequential processors/general purpose computers 1 1 2 3

-Commonly used on parallel machines. 2 3 4 5


4 6 7 8

Binary image analysis I 61 Binary image analysis I 62

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

Binary image analysis I 63 Binary image analysis I 64


procedure recursive-connected-components (B, LB); procedure search (LB, label, L, P);
{
{
LB[L,P] := label;
LB := negate (B); Nset:= neighbors (L, P);
label := 0; for each [L', P'] in Nset
find-components (LB, label); {
print (LB); if LB[L', P'] == -1
} then search (LB, label, L', P’);
}
procedure find-components (LB, label); }
{
for L:= 0 to MaxRow
for P:= 0 to MaxCol
if LB[L,P] == -1 then
{
label:= label + 1;
search (LB, label, L, P);
}

Binary image analysis I 65 Binary image analysis I 66

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

Binary image analysis I 67 Binary image analysis I 68


The union-find data structure for two sets of labels. For each integer
Label i, the value of Parent[i] is the label of the parent of i or zero if i
is a root node and has no parent.

◼ Union-Find Structure
7
Union: merging two sets into one 3

Find: determining which set a particular element


2
is in 4 8 5 6

◼ Each set is stored as a tree structure


1
1 2 3 4 5 6 7 8
Parent
2 3 0 3 7 7 0 3

Binary image analysis I 69 Binary image analysis I 70

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

Binary image analysis I 72 Binary image analysis I 73


Example 1 1 0 2 2 2 0 3 1 1 0 1 1 1 0 3
1 1 0 2 0 2 0 3 1 1 0 1 0 1 0 3
1 1 1 1 0 0 0 3 1 1 1 1 0 0 0 3
0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 3
1 1 0 1 1 1 0 1
1 1 0 1 0 1 0 1 4 4 4 4 0 5 0 3 4 4 4 4 0 5 0 3
1 1 1 1 0 0 0 1 0 0 0 4 0 5 0 3 0 0 0 4 0 5 0 3
0 0 0 0 0 0 0 1 6 6 0 4 0 0 0 3 6 6 0 4 0 0 0 3
1 1 1 1 0 1 0 1 6 6 0 4 0 7 7 3 6 6 0 4 0 3 3 3
0 0 0 1 0 1 0 1
After Pass 1 After Pass 2
1 1 0 1 0 0 0 1
1 1 0 1 0 1 1 1
1 2 3 4 5 6 7 Parent
0 1 0 0 0 0 3

Binary image analysis I 74 Binary image analysis I 75

Binary image analysis I 76

You might also like