Binary Image Analysis
Binary Image Analysis
Chapter 3
Computer Vision 2
Binary Images
Foreground : pixels that belong to
objects of interest
Background : Everything else
Separation is important
Simpler in binary images
OCR :
Scan in gray scale
Convert to binary
Hardware or software
Computer Vision 3
Examples
Computer Vision 4
Binary Image
Pixels are 0 or 1
1 foreground
0 background
Assume mxn image
Indexed from 0 .. m-1 for rows
and 0 …. n-1 for columns
I[i,j] : refers to individual pixels
Computer Vision 5
Binary Image Analysis
00010010001000
00011110001000
00010010001000
Computer Vision 6
What kinds of operations?
Computer Vision 8
Results of analysis
63 separate
objects
detected
Single cells
have area
about 50
Noise spots
Gobs of cells
Computer Vision 9
Useful Operations
Computer Vision 10
Thresholding
Background is black
Healthy cherry is bright
Bruise is medium dark
Histogram shows two
cherry regions (black
background has been
removed)
pixel
counts
0 256
gray-tone values
Computer Vision 11
Thresholding
Common way to convert a gray scale
image to a binary image
Binary Thresholding
Map the intensities in the image into 0 or 1
Multilevel Thresholding
Map the intensities in the image into a
finite number (usually small) of intensities
Often guided by the histogram
Computer Vision 12
Histogramming
Map the frequency of intensities in an image
Table of frequencies
Graphical representation
Shows first order properties of an image
E.g. Bright/dark
Modality
Number of peaks of a histogram
Bimodal, unimodal, etc.
Computer Vision 13
Histogramming
Image Histogram
Computer Vision 14
Histogram-Directed Thresholding
Grp 1 Grp 2
Assumption: the histogram is bimodal
Computer Vision 17
Another Example
Threshold = 180
Computer Vision 18
Neighborhood
How many neighbors does a pixel have?
NW N NE
W P E
SW S SE
Computer Vision 19
4/8 Neighborhood
N NW N NE
W E W E
S SW S SE
4-Neighborhood 8-Neighborhood
Computer Vision 20
Neighborhood Ambiguity
0 0 0 0 0 0 0 0 All 1's are 8-neighbors
Define a ring
0 0 0 0 0 0 0 0 The red 0’s are also 8-
neighbors
0 0 1 1 1 1 0 0 Foreground and background
0 0 1 0 0 1 0 0 objects intersect each other
Not simple objects
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Computer Vision 21
Managing Ambiguity
4-neighborhood for foreground and
8-neighbor for background
Computer Vision 22
Applying Masks
Basic concept is based on convolution
Variety of tasks can be framed in terms
of mask operations
Mask = a set of pixels
Have associated weights
Generally square
Has an origin (usually the center pixel)
Generally 2D or 1D
Computer Vision 23
Example of Masks
1 1 1 -1 0 1
1 1 1 -1 0 1 -1 1
1 1 1 -1 0 1
Computer Vision 24
Mask Operation
Overlay the origin of the mask at a pixel
Pair wise multiply
Pixel intensity of the image
Weight of the mask at the pixel
Sum the terms
Defines the value at that pixel
Normalize
Divide by the size of the mask
Produces a new image
Computer Vision 25
Example
9 9 9 9 1 1 1
9 9 9 9 1 1 1
-1 0 1
9 9 9 9 1 1 1
9 9 9 9 1 1 1 -1 0 1
9 9 9 9 1 1 1 -1 0 1
9 9 9 9 1 1 1
9 9 9 9 1 1 1
Computer Vision 26
Example -1 0 1
-1 0 1
9 9 9 9 1 1 1 Mask
9 9 9 9 1 1 1 -1 0 1
9 9 9 9 1 1 1
9 9 9 9 1 1 1
0 0 -24 -24 0
9 9 9 9 1 1 1
0 0 -24 -24 0
9 9 9 9 1 1 1
0 0 -24 -24 0
9 9 9 9 1 1 1
0 0 -24 -24 0
Image 0 0 -24 -24 0
0 0 -24 -24 0 0 0 -3 -3 0
0 0 -24 -24 0 0 0 -3 -3 0
0 0 -24 -24 0 0 0 -3 -3 0
0 0 -24 -24 0 0 0 -3 -3 0
0 0 -24 -24 0 0 0 -3 -3 0
Computer Vision 28
Example -1 0 1
-1 0 1
9 9 9 9 1 1 1 Mask
9 9 9 9 1 1 1 -1 0 1
9 9 9 9 1 1 1
0 0 0 0 0 0 0
9 9 9 9 1 1 1
0 0 0 -24 -24 0 0
9 9 9 9 1 1 1
0 0 0 -24 -24 0 0
9 9 9 9 1 1 1
0 0 0 -24 -24 0 0
9 9 9 9 1 1 1
0 0 0 -24 -24 0 0
Image 0 0 0 -24 -24 0 0
0 0 0 0 0 0 0
Computer Vision Result 29
Counting Objects
Counting Objects
Number of foreground objects
Counting number of holes
Equivalent problem
Swap the role of foreground and
background
Assumption
Objects are 4-connected
Foreground pixels are 1’s
No interior holes
Computer Vision 31
Counting Objects
External Corners Internal Corners
Three 0’s and one 1 Three 1’s and one 0
0 0 0 0 1 1 1 1
0 1 1 0 1 0 0 1
1 0 0 1 0 1 1 0
0 0 0 0 1 1 1 1
Computer Vision 32
Counting Objects
Computer Vision 33
Counting Objects
Take the 4 external masks and 4
internal masks
Put them at each pixel in the image
Count the number of E’s and I’s
The number of pixels where they match
Determine the number of objects based
on E and I
Computer Vision 34
Counting Objects
Computer Vision 35
Connected Components
Connected Components
• Given
– A binary image B
– A pair of pixels B[r,c] = B[r ' , c' ] = v (0,1)
• Pixel (r,c) is connected to (r ' ,c' ) if
– there is a sequence of pixels
(r,c) = (r0 ,c0 ), (r1, c1 ), L , (rn ,cn ) = (r',c')
– B[ri ,ci ] = v
– (ri ,ci ) and (r i +1, ci +1 ) are adjacent
Computer Vision 38
Connected Components Labeling
CCL is a well –known technique in image
processing that scans an image (binary and
gray level) and labels its pixels connected
components based on pixel connectivity. In
other words all pixels in connected
components share similar pixel intensity
values and these components are actually
connected with respect of either 4 or 8 –
neighborhood definition
Connected Components Labeling
A connected component labeling of a
binary image B is a labeled image in which
the value of each pixel is the label of its
connected component
Background label is 0
Each component has unique label
Consecutive labels starting with 1
Computer Vision 40
Connected Components
Computer Vision 41
Recursive Labeling Algorithm
First negate the binary image
Finding connected component, becomes one
of finding a pixel which values minus one and
assigning a label
Calling procedure search to find its neighbors
that have value minus one and recursive
repeat the process for its neighbors
Algorithm 3.2
Fig 3.7, 3.8
Computer Vision 42
Connected Components Labeling
Classical Algorithm
Top
Left Current
Computer Vision 43
Connected Components Labeling
Classical Algorithm
Top Top
0 0
Left Current Left Current
0 1 1 1
Top Top
1 1
Left Current Left Current
0 1 1 1
Computer Vision 44
Connected Components Labeling
Computer Vision 45
Classical Algorithm
First Pass
currentlabel=1
for (i=0; i<nrows; i++)
for(j=0; j<ncols; j++)
if (Image[i,j]==0) break;
if ((top==0) && (left==1) label (current) = label(left);
if ((top==1) && (left==0) label (current) = label(top);
if ((top==0) && (left==0) {
label (current) = currentlabel;
currentlabel++;
}
if ((top==1) && (left==1) {
label (current) = label(left);
equivelent(label(left), label(top));
}
Computer Vision 46
Classical Algorithm
Second Pass
Computer Vision 47
Classical Algorithm
Union Find
Computer Vision 48
Union Find
Stores a collection of disjoint sets
Efficiently implements some operations
Union: Merges two sets into one
Find: Find which set an element is in
Stores the sets as a tree structure
Node: Label
Stored as a vector (2D array)
Computer Vision 49
Union Find
Computer Vision 50
Union Find
Computer Vision 51
Union Find
Computer Vision 52
Union Find
Computer Vision 53
Classical Algorithm
First Pass
currentlabel=1
for (i=0; i<nrows; i++)
for(j=0; j<ncols; j++)
if (Image[i,j]==0) break;
if ((top==0) && (left==1) label (current) = label(left);
if ((top==1) && (left==0) label (current) = label(top);
if ((top==0) && (left==0) {
label (current) = currentlabel;
currentlabel++;
}
if ((top==1) && (left==1) {
label (current) = label(left);
equivelent(label(left),
PARENT[label(top)] label(top));
= label(left);
}
Computer Vision 54
Classical Algorithm
Second Pass
Computer Vision 55
Classical Algorithm
Computer Vision 56
Connected Components Labeling
Computer Vision 57
Methods for CC Analysis
Computer Vision 58
Equivalent Labels
0 0 0 1 1 1 0 0 0 0111100001
0 0 0 1 1 1 1 0 0 0111100011
0 0 0 1 1 1 1 1 0 0111100111
0 0 0 1 1 1 1 1 1 0111100111
0 0 0 1 1 1 1 1 1 1111100111
0 0 0 1 1 1 1 1 1 1111100111
0 0 0 1 1 1 1 1 1 1111111111
0 0 0 1 1 1 1 1 1 1111111111
0 0 0 1 1 1 1 1 1 0000011111
Computer Vision 59
Equivalent Labels
0001110000222200003 1≡2
0001111000222200033 1≡3
0001111100222200333
0001111110222200333
0001111111111100333
0001111111111100333
0001111111111111111
0001111111111111111
0001111110000011111
Computer Vision 60
Run-Length Data Structure
01234
0 11 11
row scol ecol label
1 11 1
2 1 1 1 1 Binary 0 UNUSED 0
3 Image 1 0 0 1 0
4 1111 2 0 3 4 0
3 1 0 1 0
Rstart Rend 4 1 4 4 0
0 1 2 5 2 0 2 0
1 3 4 6 2 4 4 0
2 5 6 Row Index 7 4 1 4 0
3 0 0
Runs
4 7 7
Computer Vision 61
Run-Length Algorithm
Procedure run_length_classical
{
initialize Run-Length and Union-Find data structures
count <- 0
Computer Vision 62
Case 1: No Overlap
Q Q
/* new label */
count <- count + 1 /* check Q’s next run */
label(P) <- count Q <- Q + 1
P <- P + 1
Computer Vision 63
Case 2: Overlap
Subcase 1: Subcase 2:
P’s run has no label yet P’s run has a label that is
different from Q’s run
Q Q
|///////| |/////| |///////| |/////|
|/////////////| |/////////////|
P P
label(P) <- label(Q) union(label(P),label(Q))
move pointer(s) move pointer(s)
}
Computer Vision 64
Pass 2 (by runs)
}
where union and find refer to the operations of the
Union-Find data structure, which keeps track of sets
of equivalent labels.
Computer Vision 65
Labeling shown as Pseudo-Color
connected
components
of 1’s from
thresholded
image
connected
components
of cluster
labels
Computer Vision 66
Morphology
Morphology
Study of shape (form and structure)
Mathematical morphology was designed
for a set of points
Treat the object as binary
A shape = a set of points
Use shape-based or iconic approach
Computer Vision 68
Mathematical Morphology
Computer Vision 69
Dilation
1. growing features
Computer Vision 70
Erosion
1. shrinking features
Computer Vision 71
Structuring Elements
Morphology operations involve
Binary image
Structuring element
Structuring Element
Binary image
Can be any arbitrary binary image in
general
Usually much smaller
Computer Vision 72
Structuring Elements
box
hexagon disk
something
box(length,width) disk(diameter)
Computer Vision 73
Dilation with Structuring Elements
origin
0 0 1 1 0 0 0 0 0 0
1 erode
0 0 1 1 0 0 0 1 1 0
1
0 0 1 1 0 0 0 1 1 0
1
1 1 1 1 1 0 0 0 0 0
B S B S
Computer Vision 75
Common Structuring Elements
Computer Vision 76
Operations
Intersection
A I B = { p | p ∈ A and p ∈ B}
Union
A U B = { p | p ∈ A or p ∈ B}
Complement
A = { p | p ∈ Ω and p ∉ A}
Ω is a universal binary image (all 1s)
Computer Vision 77
Translation
A : binary image
p : pixel
Translation of A by p is an image
Shifts the origin of A to p
Ap = {a + p | a ∈ A}
Computer Vision 78
Example
Computer Vision 79
Translation
Computer Vision 80
Dilation
A ⊕ B = U Abi
bi ∈B
B = {b1 , b2 ,.....bn }
Dilation of A by B
Ab1 is the translation of A by a pixel of B
Computer Vision 81
Dilation
Commutative
A⊕ B = B ⊕ A
Associative
A ⊕ ( B ⊕ C ) = ( A ⊕ B) ⊕ C
Computer Vision 82
Example
Computer Vision 83
Dilation
Computer Vision 84
Erosion
Opposite of Dilation
AΘB = { p | Bp ⊆ A}
1 at a pixel iff every 1 pixel in the
translation of B to p is also a 1
Structuring element is a probe
Locate where the structuring element is
located in the image
Computer Vision 85
Erosion
Computer Vision 86
Erosion and Dilation
B' = {− p | p ∈ B}
A ⊕ B = AΘB '
AΘB = A ⊕ B '
Computer Vision 87
Complement of A and probe
Computer Vision 88
'
AΘB
Computer Vision 89
A⊕ B '
Computer Vision 90
Opening
Erosion followed by dilation
Remove all pixels in a region too small
to contain the structuring element
Disc shaped probe
Eliminate all convex regions and isolated
points
Computer Vision 91
Example
Computer Vision 92
Example
Computer Vision 93
Closing
Opposite of opening
Dilation followed by erosion
Fill holes smaller than the structuring
element
Computer Vision 94
Example
Computer Vision 95
Example
Computer Vision 96
Example
Computer Vision 97
Gear Tooth Inspection
original
binary
image
How did
they do it?
detected
defects
Computer Vision 98
Example
Computer Vision 99
Example
A= ∑1
( r ,c )∈R
where R is a given region
P4 = {( r , c) ∈ R | N 8 (r , c) − R ≠ φ }
P8 = {(r , c) ∈ R | N 4 (r , c) − R ≠ φ }
P = {k | (ri +1 , ci +1 ) ∈ N 4 (ri , ci )} | +
2 {k | (ri +1 , ci +1 ) ∈ N 8 (ri , ci ) − N 4 (ri , ci )} |
Indices are computed modulo k (wrap
around) Computer Vision 107
Circularity (1)
Degree of similarity with a circle
2
P
Circularity =
A
Based on continuous planar shapes
smallest value for a circle?
Make adjustments for digital shapes
1 k −1
μ R = ∑ (r , ck ) − (r , c)
k i =0
[ ]
1
⎛1 k −1
⎞
2 2
σ r = ⎜ ∑ ( r , ck ) − ( r , c ) − μ R ⎟
⎝ k i =0 ⎠
P
1
E = ∫ | κ (s) | ds
2
P0
κ is the curvature
Minimized by a circle
Slope
Slope
Arc length
Arc length
Circle Computer Vision Square 113
Projections/Signatures
Non-information preserving
Maxima and minima are indications of
landmarks
Horizontal signature
p( x) = ∫ f ( x, y)
y
Vertical signature
p ( y ) = ∫ f ( x, y )
x Computer Vision 114
Bounding Box
Gives a rough idea of the location of the
object
Smallest rectangle to enclose the shape
Extremal points
Leftmost, rightmost, topmost and
bottommost
Lie on the bounding box boundary
May have upto 8 extremal points
Occur in opposite pairs
⎧ 1
⎪⎪ cosθ θ < 45 o
1
Column moments μ cc = ∑ (c − c )
A ( r ,c )∈R
2
1
Mixed moments μ rc = ∑ (
A ( r ,c )∈R
r − r )( c − c )
M pq= ∫ ∫x
p
y r ( x, y) dx dy
q
x y
where
⎧1 if ( x, y) is in the object⎫
r ( x, y) = ⎨ ⎬
⎩0 otherwise ⎭
M pq = ∑∑ x y r ( x, y)
p q
x y
M pq= ∑∑ ( x − xc ) ( y − yc ) r ( x, y)
p q
x y
M 01 = M 10 = 0
xc = yc = 0
Computer Vision 125
Central Moments
Centroid forms the origin
Orientation of on object is given by
1 −1 2M11 π
θ = tan ( ) + n( )
2 M 20 − M 02 2
Eccentricity (elongatedness)
1
(M 20 − M 02 ) 2 + 4M11
e=
M 00
1
M pp= ∑ ( x − xc ) r ( x, y )
p
A x
Column moments
1
M qq= ∑ ( y − yc ) r ( x, y )
q
A x
Computer Vision 127
Ellipses
Circular objects get mapped to ellipses
in imaging process
Defined by their major and minor axes
May be used to approximate elongated
objects
dx + 2exy + fy ≤ 1
2 2
⎛d e⎞ 1 ⎛ μ yy μ xy ⎞
⎜⎜ ⎟⎟ = 2 ⎜
⎜ ⎟
⎝e f ⎠ 4( μ xx μ yy − μ xy ) ⎝ − μ xy μ xx ⎟⎠
μ rc = 0 and μ rr > μ cc
1
Major axis :Orientation = −90 Length = 4 μ rr2
o
1
Minor axis :Orientation = 0 Length = 4 μ cc2
o
1
Major axis :Orientation = 0 Length = 4 μ cc2
o
1
Minor axis :Orientation = −90 Length = 4 μ rr2
o
⎩ ⎝ ⎠⎭
Minor axis :
⎧⎪ − 2 μ ⎫⎪
Orientation = 90 + tan −1 ⎨ rc
1 ⎬
⎪⎩ μ rr − μ cc + [( μ rr − μ cc ) 2 + 4 μ rc2 ] 2 ⎪⎭
1
⎧
Length = ⎨8⎜ μ rr + μ cc − [( μ rr − μ cc ) + 4 μ rc ] 2 ⎞⎟⎫⎬
⎛ 1 2
2 2
⎩ ⎝ ⎠⎭
Computer Vision 133
Ellipse Axes
−1 ⎢
⎣ ⎥⎦
rr cc cc rr rc
Orientation = tan
− 2 μ rc
1
⎩ ⎝ ⎠⎭
Computer Vision 134
Ellipse Axes
Minor axis :
1
⎡ μ + μ + [( μ − μ ) 2 + 4 μ 2 ] 2 ⎤ 1 2
−1 ⎢
⎣ ⎥⎦
rr cc cc rr rc
Orientation = 90 + tan
− 2 μ rc
1
⎩ ⎝ ⎠⎭
Computer Vision 135
Best Axes /
Some objects have natural axis
Pencils, Letters l,I, -
Axis around which there is least second
order moment
Axis of inertia
Line about which the object can be spun
with least energy input
Circle
All axes have same length
1
μ r ,c ,α = ∑ d
A ( r ,c )∈R
2
1
= ∑ ((r − r ) cos β + (c − c) sin β )
A ( r ,c )∈R
2
• gray-tone properties
• color properties
• texture properties
• motion properties
• relationship properties (1 in Ch 3)
Computer Vision 147
Geometric and Shape Properties
• area
• centroid
• perimeter
• perimeter length
• circularity
• elongation
• mean and standard deviation of radial distance
• bounding box
• extremal axis length from bounding box
• second order moments (row, column, mixed)
• lengths and orientations of axes of best-fit ellipse