Binary Images
Binary Images
Binary Images
and Commonly Used Operations
(connected components; distance transform; morphological operators)
Cyrill Stachniss
2
Binary Image Examples
Handwritten digits
Background Subtraction
- =
4
Connected Components
§ For several applications, we need to
identify which components are
connected
§ Example: Separation of characters
5
Connected Components
Two points A, B are connected if there
exists a path from A to B that goes only
through the same component
N4 neighborhood N8 neighborhood
7
N4 Neighborhood
§ The left, right, up, down pixels are
connected
8
N8 Neighborhood
§ The left, right, up, down and the
diagonal pixels
are connected
9
How to Determine Connected
Components?
12
Connected Components
13
Example
2
1
1 1 1 1
1
14
Steps (outer loop, 1st iteration)
outer
loop 1
inner
loop 1
inner
loop 2
inner
loop 3
1
1 1 1 1
1
15
Steps (outer loop, 2nd iteration)
outer
loop 2
inner
loop 1
2
1
1 1 1 1
1
16
Properties of the Algorithm
§ Provides the connected components
§ Work in general graphs
§ Does not exploit the systematic
neighborhood of images
17
Labeling by Exploiting the
Grid Structure of the Image
Idea
§ Process the image in one pass
§ Generate a temporary label for a
foreground pixel based on the already
processed neighbors
§ In case of multiple labels for the same
component, use an equivalence table
18
Connected Components on Grids
§ Process the image from left to NOTE: Example uses
right, top to bottom: max to select the label
If the next pixel to process is 1
A) If none of its (top or left) neighbor
N4 is 1, assign new label.
21
Example (N8 Neighborhood)
24
Distance Transform
25
Distance Transformation
Several problems require to compute
the distance from any pixel to the
border of the components
Examples
§ Nearest neighbor problems
§ Sensor models for range sensor
§ Map visualization
§ User interfaces
26
Distance Transformation
§ Distance transform on the foreground
pixels is defined as
border
27
Examples
N4 N8
1 1 1 1 1 1
1 2 2 2 1 1 1 2 1 1
1 2 3 2 1 1 2 2 2 1
1 2 2 2 1 1 2 2 2 1
1 2 1 1 1 1 1 1 1 1
1 1 1 1
1 1
28
Distance Transformation
§ Distance transform can be computed
similar to the connected components
§ Two passes over the image
§ 1st: top-down, left-right
§ 2nd: down-up, right-left
§ Always store the minimum distance
29
First and Second Pass (N4)
First pass
1 1 1
1 2 2 2 1
1 2 3 3 2
1 2 3 4 3
1 2 3 4 4
1 2
1
1 1 1 1 1 1
1 2 2 2 1 1 2 2 2 1
1 2 3 3 2 1 2 3 2 1
1 2 3 4 3 1 2 2 2 1
1 2 3 4 4 1 2 1 1 1
1 2 1 1
1 1
1 1 1 1 1 1
1 1 2 1 1 1 1 2 1 1
1 2 2 2 1 1 2 2 2 1
1 2 3 2 1 1 2 2 2 1
1 2 3 2 1 1 1 1 1 1
1 2 1 1
1 1
35
N8 Distance Transformation
1. Initialization
36
N4 vs. N8 Neighborhood 1 1 1
1 2 2 2 1
§ The N4 neighborhood 1 2 3 2 1
overestimates the 1 2 2 2 1
Euclidean distance 1 2 1 1 1
§ The N8 neighborhood 1 1
N4
1
underestimates the
1 1 1
Euclidean distance 1 1 2 1 1
1 2 2 2 1
1 2 2 2 1
1 1 1 1 1
1 1
N8
1
37
N4 vs. N8 Neighborhood 1 1 1
1 2 2 2 1
§ The N4 neighborhood 1 2 3 2 1
overestimates the 1 2 2 2 1
Euclidean distance 1 2 1 1 1
§ The N8 neighborhood 1 1
N4
1
underestimates the
1 1 1
Euclidean distance 1 1 2 1 1
§ Can we efficiently 1 2 2 2 1
combine both? 1 2 2 2 1
1 1 1 1 1
1 1
N8
1
38
Combined Distance
§ The real cost of the diagonal is
§ If we approximate
§ The sum provides a better
approximation for twice the distance
§ Thus we can use the average distance
40
Morphological Operators:
Erosion and Dilation
41
Filtering
§ Binary images are often obtained
through thresholding (point operator)
§ Operator:
How to
Fix This?
45
Erosion
Change each foreground (here black)
pixel to a background (here white) pixel
if it has a background pixel as its N4
neighbor
46
Erosion Example
47
Erosion Example
48
Erosion Example
49
Dilation
Change each background (here while)
pixel to a foreground (here black) pixel
if it has a foreground pixel as its N4
neighbor
50
Dilation Example
51
Dilation Example
52
Dilation Example
53
Erosion and Dilation
§ Erosion shrinks the foreground
§ Erosion removes foreground outliers
§ Dilation expands the foreground
§ Dilation fills holes
54
Opening and Closing
Opening
§ Removing stray foreground pixels in
background
§ Step 1: erosion; Step 2: dilation
Closing
§ Remove holes in the foreground
§ Step 1: dilation; Step 2: erosion
55
Original Image
60
Summary
§ Binary images are relevant tools in
several image processing applications
§ Connected components
§ Distance transforms
§ Erosion, dilation, opening, and closing
61
Literature
§ Szeliski, Computer Vision: Algorithms
and Applications, Chapter 3.3
§ Förstner, Scriptum Photogrammetrie I,
Chapter 6
62
Slide Information
§ The slides have been created by Cyrill Stachniss as part of the
photogrammetry and robotics courses.
§ I tried to acknowledge all people from whom I used
images or videos. In case I made a mistake or missed
someone, please let me know.
§ The photogrammetry material heavily relies on the very well
written lecture notes by Wolfgang Förstner and the
Photogrammetric Computer Vision book by Förstner & Wrobel.
§ Parts of the robotics material stems from the great
Probabilistic Robotics book by Thrun, Burgard and Fox.
§ If you are a university lecturer, feel free to use the course
material. If you adapt the course material, please make sure
that you keep the acknowledgements to others and please
acknowledge me as well. To satisfy my own curiosity, please
send me email notice if you use my slides.