of 111
8/18/2019 Module 2JH
1/111
Binary Image Processing
• Binary Image Generation•
• Blob Coloring• nary orp o ogy
• Binary Template Matching
• Binary Image Compression
1
8/18/2019 Module 2JH
2/111
Binary Images
sampled image intensities: A 10 x 10 image
rows
columns • Each gray level is
quantized: assignedone of a finite set of
numbers [0,…, K -1].
• K = 2 B possible gray
levels: each pixel• Binary images
=
2represented by B bits.
8/18/2019 Module 2JH
3/111
8/18/2019 Module 2JH
4/111
• For printed page (notes, homework, and tests):
‒
Unless otherwise stated, BLACK = 1 = TRUE andWHITE = 0 = FALSE
“ ” ‒ y y
background and this creates a “cleaner” look in print.
Think of a page in a book: the text is printed in blackon a white background.
• For the computer monitor:
– Unless otherwise stated, WHITE = 1 = TRUE = 255dand BLACK = 0 = FALSE = 0d.
– “ ”
4
.
8/18/2019 Module 2JH
5/111
Binary Image Generation
• nary mages can come rom s mp e
sensors with binary output, e.g., tablet,
res s ve pa , or g pen.
• Quite useful for engineering drawing,
5
entering handprinted characters, etc.
8/18/2019 Module 2JH
6/111
-
• en, a nary mage s o a ne y
thresholding a gray-level image.
• Why do this?
- B-fold reduction in required storage- Simple abstraction of information
- - - Good binary compression algorithms
6
8/18/2019 Module 2JH
7/111
Simple Thresholding
• The simplest image processing operation.• .
• Requires the definition of a thresholdT - .
• Every pixel intensity is compared to T , and a
nary ec s on ren ere .• Ex: binary pixel = TRUE if gray-level pixel > T
7
8/18/2019 Module 2JH
8/111
Simple Thresholding
-
K gray-levels: 0, 1,...., K -1
• Select a threshold 0
8/18/2019 Module 2JH
9/111
Binary Image Information
• Artists have long understood that binaryima es contain much information: form
structure, shape, etc.
9
“Don Quixote” by Pablo Picasso
8/18/2019 Module 2JH
10/111
8/18/2019 Module 2JH
11/111
original image, B=8 T =32
T =128 T =192
11
8/18/2019 Module 2JH
12/111
original image, B=8 T =64
T =128 T =160
12
8/18/2019 Module 2JH
13/111
original T =32 T =64 T =96
T =128 T =160 T =192 T =224
13
8/18/2019 Module 2JH
14/111
Threshold Selection
• The usefulness of the binary image J fromthresholding I depends heavily on the threshold T .
• Different thresholds may give different valuable
abstractions of the image (preceding examples).• Some images do not produce any interesting results
when thresholded by any T .
• So: How does one decide if thresholding ispossible? How does one select the threshold T ?
14
8/18/2019 Module 2JH
15/111
-
•
the frequency of occurrence of each gray.
• H I
is a one-dimensional function (array)
, ... , - .
• H I(k ) = n if I contains exactly n pixels with
gray eve ; = , ... - .
15
8/18/2019 Module 2JH
16/111
Histogram Appearance
• The appearance of a histogram can tell youa lot about the ima e:
I (k ) I (k)
0 -1 gray level k 0 -1 gray level k
Predominantly dark image Predominantly light image
• Could be histo rams of underex osed and
16overexposed images, respectively.
8/18/2019 Module 2JH
17/111
• The histogram contains first-order information only. It can tell you:
– e average, m n, an max p xe va ues, as we as t e stan ar
deviation. – The probability that a pixel picked at random from the image has
va ue 32.
• But it does not contain any 2nd or higher-order information ( i.e., how
the pixel values occur together in groups of two or more). – It can tell you variance, but it cannot tell you covariance.
– It can’t tell you the probability that a pixel with value 32 occurs
next to a pixel with value 40.
– If you take an image and scramble it up by moving all the pixelsaround to make a mess, the histogram remains unchanged.
17
8/18/2019 Module 2JH
18/111
Histogram Appearance
• Here is a well-distributed histogram:
I(k )
0 -1 gray level k
too bright nor too dark.
18
8/18/2019 Module 2JH
19/111
•
there are dark objects on a brightback round.
• Or when there are bright objects on a dark
back round.• Images of this type tend to have histograms
with distinct eaks or modes.
• Threshold selection is easier when theeaks are well se arated.
19
8/18/2019 Module 2JH
20/111
I (k ) bimodal histogram
poorly separated;threshold selection
-
I (k) bimodal histogram
well separated peaks;
threshold selection
0 -1 gray level k eas er.
• Often, the peaks (modes) correspond to the
20
o ec s an o e ac groun .
8/18/2019 Module 2JH
21/111
• Suppose the histogram is bimodal and well separated.
• ac ng t e t res o etween t e mo es can o ten
yield a binary image that is TRUE on the object and FALSEon the background (or vice-versa).
• The exact optimal threshold placement is usually difficult
to determine. threshold
T
I (k )
hresholdselection
21
0 -1 gray level k
8/18/2019 Module 2JH
22/111
-• The histogram may have multiple modes. Different values for T
will give very different results.
• In some cases, the multiple modes may be from multiple objects.
–
out the objects in some cases.
– This is called “multi-thresholdin .”
T ? T ?
I (k ) multi-modal
histogram
22
0 -1
gray level k
8/18/2019 Module 2JH
23/111
• The histogram may be "flat," making
I (k) flat histogram
0 -1 gray level k
23
8/18/2019 Module 2JH
24/111
• We'll use the histo ram extensivel for ra -level
processing later.• Some general observations for now:
– Bimodal histograms often imply objects and background
of different average brightness. – Bimodal histograms are easiest to threshold.
– The ideal result is a simple binary image showing
– Ex: printed text characters, blood cells in solution,
machine parts on an assembly line.
24
8/18/2019 Module 2JH
25/111
Discussion of Histogram Types
• Multi-modal histograms often occur in images ofmultiple objects with different average.
• “Flat” or level histograms usually imply more
- background, etc.
• Thresholding rarely gives perfect results. Usually,
some kind of region correction must be applied.• We will study a region correction technique later
25
.
8/18/2019 Module 2JH
26/111
• Matlab provides a built-in function “hist” to compute
.
• But to use a Matlab built-in function correctly, you MUSTread the documentation!
Incorrect!! Correct:
h = hist(Lena); h = sum(hist(Lena,0:255)’);
26
8/18/2019 Module 2JH
27/111
// the ima e is in unsi ned char arra x
// the total number of pixels is n
// the histogram will go into int array hist
since K=256 hist has 256 bins
for (i=0; i
8/18/2019 Module 2JH
28/111
In C: In Matlab:
// x is the input image
// y is the output image
// n is the number of pixels
// T is the threshold
% x is the input image
% y is the output image
y = 255 * (x >= T);
for (i=0; i
8/18/2019 Module 2JH
29/111
• Suppose we have obtained some binary
images:
• nce e p xe s ave og ca va ues(TRUE/FALSE), we can perform logical
29
opera ons e , , an on em.
8/18/2019 Module 2JH
30/111
Logical Operations - Notation
• Suppose X1,..., Xn are binary variables. Forexam le binar ixel values.
• Here is the notation we will use:
1 1
AND(X1, X2) = X1 ∧ X2
OR(X1, X2) = X1 ∨ X2
30
8/18/2019 Module 2JH
31/111
More Notation
• Multivariate logical operations:= · · ·, ..., n n
OR(X1
, ..., Xn
) = X1
∨ · · · ∨ Xn
• nary a or ty:
MAJ(X1, ..., Xn) = ‘1’ if more 1’s than 0’s.
Else MAJ(X1, ..., Xn) = ‘0’.
31
8/18/2019 Module 2JH
32/111
Binary Majority
• Defined for an odd number of binary variablesonly. X X X MAJ X , X , X
0 0 0
0 0 1
0
0 Three-Variable
0 1 1
1
1 0 0 0
Truth Table
for MAJ
1 1 01 1 1
1 1
32
• We will define some very useful binary image
filters using MAJ later on.
8/18/2019 Module 2JH
33/111
Image Complement
• We will now define several point-wiselo ical ima e o erations.
• Image complement:
, , ,
33
8/18/2019 Module 2JH
34/111
Image AND
• Image AND:= =,
if J (m, n) = AND[I1(m, n), I2(m, n)] ∀ (m, n)
• ows over ap etween o ects n 1 an 2:
34
I 1 I 2 J
= I1I
2
8/18/2019 Module 2JH
35/111
• Image OR :
J = OR(I , I ) = I ∨ I
if J (m, n) = OR[I1(m, n), I2(m, n)] ∀ (m, n)
1 2
35
I 1 J
= 1
8/18/2019 Module 2JH
36/111
Comments on AND, OR, MAJ
• As we will see later, AND, OR, and MAJare ver useful when a lied to local ima e
regions rather than the whole image.
• - ,and MAJ is quite limited.
...
36
8/18/2019 Module 2JH
37/111
Assembly Line Example
• An assembly line image inspection system.
I
computer
stored model image
I model conveyer
37
8/18/2019 Module 2JH
38/111
• Objective: Meaningfully compare the acquired
mage o e s ore mage model
Note that I is
shifted slightly
measure of
overlap (match)
I model I ∧ Imodel I
• goo measuremen o m sma c : .
XOR(I, Imodel) = OR{AND[Imodel, NOT(I)], AND[NOT(Imodel), I]}
XOR(I, Imodel)
38• Not done yet…
8/18/2019 Module 2JH
39/111
• XOR gives an image of mismatch.
• To decide if there is a problem or flaw, the
' 'mo e
model# of '1' pixels in
=I
• This may be compared to a pre-determined
tolerance percentage Q.
• If PERCENT > Q, then the part may beflaw or inc rr ctl lac .
39
8/18/2019 Module 2JH
40/111
8/18/2019 Module 2JH
41/111
“ ” • In a binary image, a blob or connected component is a group of “1”
“ ” .
• This means that there is a connected path of “1” pixels between anytwo pixels in the blob.
“ ”
by the toplogy that we impose on the image.
• The topology specifies which neighbor pixels are connected to each
’ .
• There are two common topologies:
4-connected 8-connected
ne g or oo s ne g or oo s
Unless otherwise specified,assume the 4-connected
41
8/18/2019 Module 2JH
42/111
Blob Coloring with Minor Region
Removal• In our resent context, the assum tion is that we have a ra scale
image with one main object against a background.
• Our goal is to use thresholding to produce a binary image that is ‘1’
on the ob ect and ‘0’ on the back round.
• The thresholding result will generally be imperfect:
– Some object pixels will be misclassified as background
– Some background pixels will be misclassified as object
• Blob coloring with minor region removal is a simple technique to
accomplish region classification and correction after thresholding.
Initial
thresholdingFinal
with minorregion removal
42
result
8/18/2019 Module 2JH
43/111
Blob Coloring with Minor Region
Removal• Wh mi ht we want to do this?
– It is often used as a “low level” image processing step that is performed prior to
some “higher level” computer vision process. – The “higher level” process could be, e.g., target tracking, biometric person
, , .
– The higher level process will generally take both the binary image and the original
grayscale image as input.
– The binary image is used as a map that tells the higher level process where theobject of interest is located in the grayscale image.
• How it works:
1. Perform blob coloring to label the blobs.
2. Perform blob counting to compute the size of each blob.
3. Delete all but the largest blob (minor region removal).
4. There will still be extraneous holes. So, invert and repeat steps 1-3.
43
. .
• This will make more sense after we go through the details!
8/18/2019 Module 2JH
44/111
• For binary image I, define a "region color" array R .
R m, n) = region number o p xe I m, n)
• Set R = 0 (all zeros) and k = 1 (k = counter for “next available region number”)• While scanning the image left-to-right and top-to-bottom do
if I(m, n) = 1 and I(m, n-1) = 0 and I(m-1, n) = 0 then // new blob found!
set R(m, n) = k and k = k + 1; // use new color!
if I(m, n) = 1 and I(m, n-1) = 0 and I(m-1, n) = 1 then // part of an old blobset R(m, n) = R(m-1, n); // use old color
if I(m, n) = 1 and I(m, n-1) = 1 and I(m-1, n) = 0 then // part of old blob
set R(m, n) = R(m, n-1);
if I(m, n) = 1 and I(m, n-1) = 1 and I(m-1, n) = 1 then
set R(m, n) = R(m-1, n); // all 3 are in the same blob!
if R(m, n-1) ≠ R(m-1, n) then // oops! 2 old blobs are really the same!
44
Record that R(m, n-1) and R(m-1, n) are equivalent;
8/18/2019 Module 2JH
45/111
• " "
each blob. Counting the pixels in each blob.
blob coloring
1
2 blob counting
793 11346
esu t3
4
5
resu t 1327 171
312
• “Color” of largest blob: 2
45
Colored blob display
8/18/2019 Module 2JH
46/111
Minor Region Removal
• Let q = "color" of largest region (blob)• - -
top-to-bottom do
, ,
set I(m, n) = 0 ;
minor regionremoval
46
8/18/2019 Module 2JH
47/111
• Not finished! To remove the holes, we must
repeat the procedure on the WHITE pixels:
complement
126471 673
blob coloring/counting 821
complement
47
removal
8/18/2019 Module 2JH
48/111
•
binary image operators we will consider.•
mathematical morphology
=• Morphological operations affect the shapes
.
• All processing done on a local basis - region
48
manner.
8/18/2019 Module 2JH
49/111
• Mor holo ical o erators:
- Expand (dilate) objects
-
- Smooth object boundaries and
eliminate small regions or holes
- Fill a s and eliminate ' eninsulas'• All is accomplished using local logical
49
8/18/2019 Module 2JH
50/111
• Also known as ‘Windows”
• Some examples:
• A structuring element has a reference point (usually in the
cen er – s own n green a ove
• When the reference point is placed over a pixel in an
ima e a set of ixels are selected the ones that are
50
covered by the structuring element)
8/18/2019 Module 2JH
51/111
• To implement a morphological filter, the structuring
element is passed over the image pixels, row-by row,
column-by-column (moving window concept).
• Along the way, the structuring element is referenced
to each pixel in the image, selecting a set of input
.
• A logical operation is performed on the selected set
.
• Usually, the structuring element is approximately
51
commutes with object/image rotation.
8/18/2019 Module 2JH
52/111
.
.
.
.A structuring
element moving
. . over an mage.
52
8/18/2019 Module 2JH
53/111
• The terms ‘window’ and ‘structuring element’ are
.
• In morphology, the term ‘structuring element’ is usuallyused.
• Some typical windows with a 1-D structure:
ROW(3) ROW(5)
OL(5) COL(3)
i.e. ROW 2 +1 and COL 2 P +1
• The window usually covers an odd number of pixels (2 P +1)
so that it is s mmetric about the reference oint and there
53
is no phase shift between the input and output images.
8/18/2019 Module 2JH
54/111
• Some windows with a 2-D structure:
SQUARE(9) CROSS(5)
SQUARE(25) CROSS(9) CIRC(13)
SQUARE(2 P +1), CROSS(2 P +1), CIRC(2 P +1)
54
8/18/2019 Module 2JH
55/111
“ ” • Consider a 1-D LTI system H 1 with impulse response
• The output is
1[ ] [ 1] [ ] [ 1].
4 2 4
h n n n nδ δ δ = + + + −
1 1
1
[ ] [ ] [ ]
[ ] [ ]
y n x n h n
h k x n k
∞
= ∗
= −1 1 1
[ 1] [ ] [ 1]4 2 4
k
x n x n x n
=−∞
= + + + −
• Thus, at each point n, the number y1[n] is a weightedaverage of three surrounding inputs that is centered .
55
• o e a 1 s no causa , s nce e curren ou pu y1 n
depends on the future input x[n+1].
Y thi k f th filt H h i 3 i t i d th t lid
8/18/2019 Module 2JH
56/111
• You can think of the filter H 1 as having a 3-point window that slides
• Because the window is centered, there is no shift in the output signal
56
relative to the input signal; i.e., the filter introduces no delay .
• Because h [ ] is real and even the frequency response H ( jω) is also
8/18/2019 Module 2JH
57/111
• Because h1[n] is real and even, the frequency response H 1(e jω ) is also
.
• The frequency response is given by
j j n H e h n eω ω ∞
−=
1 1 14 2 4
n
j je eω ω
=−∞
−= + +
•
1 1cos .
2 2ω = +
1 .
• This again shows that, because the window is centered, there is no
shift (delay) between the input signal and the output signal.
.
• To implement the filter in real-time, you would have to shift h1[n] to
the right by one sample to make it causal.
57
• So now consider a 2nd 1 D LTI system H with impulse response
8/18/2019 Module 2JH
58/111
• So, now consider a 2nd 1-D LTI system H 2 with impulse response
• Notice that H 2 is causal, because h2[n] = 0 ∀ n < 0.
2 11 1 1[ ] [ 1] [ ] [ 1] [ 2].4 2 4
h n h n n n nδ δ δ = − = + − + −
• The output of H 2 is given by
2 2[ ] [ ] [ ] y n x n h n
∞
= ∗
2[ ] [ ]
1 1 1
k
h k x n k =−∞
= −
= − −
• The output signal is still a weighted average of the input signal, but it
is no longer centered.
4 2 4
• T e output signa is s i te to t e rig t y one samp e re ative to t e
input signal.
• In other words, the filter H 2 introduces delay – a phase shift .
58
• “Sliding window” picture for H :
8/18/2019 Module 2JH
59/111
• Sliding window picture for H 2:
• The current output is a weighted average of the current input and the
two previous inputs; the output signal is delayed or shifted by one
59
sample relative to the input signal.
• Because h [n] does not have symmetry the frequency response
8/18/2019 Module 2JH
60/111
• Because h2[n] does not have symmetry, the frequency responseω
2 .
• It is given by
2 2( ) [ ] j j n
n
H e h n eω ω ∞
−
=−∞
=
21 1 1
4 2 4
j j
e e
ω ω − −= + +
1
4 2 4
( )
j j j
j j
e e e
H e e
ω ω ω
ω ω
− −
−
= + +
=
• The phase response is arg H 2(e jω ) = -ω .
• The filter H 2 is causal and it could be implemented in real-time.
• But ecause t e win ow is not centere , t e output signa is s i te
to the right by one sample relative to the input signal.
60
• In image processing, such shifting is usually undesirable.
8/18/2019 Module 2JH
61/111
•
image – we usually want them to be “lined up.”
• Moreover, in image processing we usually receive the whole image at once;
– So there is no reason to restrict ourselves to causal processing!
– Mathematically, for the output pixel J(m, n), the input pixels I(m-1, n) and
“ ”, - , ,
I(m, n+1) are non-causal or “future” inputs.
– A filter that uses I(m+1, n) and I(m, n+1) to compute J(m, n) is non-
.
– But that’s usually not a problem in image processing – because we
usually get all the pixels at the same time.
• or ese reasons, n mage process ng e w n ow or s ruc ur ng e emen
is usually symmetric, usually covers an odd number of pixels, and thereference point is usually in the center.
61
• s ensures a e ou pu mage s no s e re a ve o e npu mage.
If the filter is linear, then it has a zero phase response.
8/18/2019 Module 2JH
62/111
•
coordinates (or “offsets”) Bi = ( p
i, q
i) of the covered
pixels relative to the reference pixel (0, 0):
B = {B1, ..., B2 P +1} = {( p1, q1), ..., ( p2 P +1, q2 P +1)}
Windows with a 1-D structure:
B = ROW(2 P +1) = {(0, - P ), ..., (0, P )}
= + = - , , ..., ,
62
or examp e, = = , - , , , ,
8/18/2019 Module 2JH
63/111
= = , , , , , ,
(0, -1) , (0, 0), (0, 1),
(1, -1) , (1, 0), (1, 1)}
For exam le B = R 5 = -1 0
(0, -1), (0, 0), (0, 1),
63
,
8/18/2019 Module 2JH
64/111
• ,
windowed set at (m, n) by:BI m n = I m- n- ∈ B
• It is the set of input pixels from the image that
located at pixel (m, n).
• -
simple concept will enable us to write simpleand flexible definitions for binary filters.
64
• B = ROW(3):
8/18/2019 Module 2JH
65/111
m, n = m, n- , m, n , m, n• B = COL(3):
m, n = m- , n , m, n , m+ , n
• B = SQUARE(9):
BI(m, n) = {I(m-1, n-1) , I(m-1, n), I(m-1, n+1),
I(m, n-1) , I(m, n), I(m, n+1),
I(m+1, n-1) , I(m+1, n), I(m+1, n+1)}
• B = CROSS(5):
B
I(m, n) = { I(m-1, n),I(m, n-1), I(m, n), I(m, n+1),
65I(m+1, n) }
8/18/2019 Module 2JH
66/111
General Binary Filter
• Denote a logical operation G on thewindowed set BI(m, n) by
J(m, n) = G{BI(m, n)} = G{I(m- p, n-q); ( p, q) ∈ B}
• By performing this at every pixel in the image,
J = G[I, B] = [J(m, n); 0 ≤ m ≤ M -1, 0
8/18/2019 Module 2JH
67/111
• What happens near the edges of the image
when the window "hangs over" ?
,
“empty space” by
making extra copies of
the image pixels on the
outside rows & columns
• This is called “handlin
the edge effects byreplication”
67
8/18/2019 Module 2JH
68/111
,
define the morphological filter= ,
by
J(m, n) = OR{BI(m, n)}
= - -, ,
• Often written as J = I ⊕ B
68
8/18/2019 Module 2JH
69/111
Dilation Filter-
size of logical ‘1’ objects.
Example of a local
OR
computation.
I J
= og ca
= logical ‘0’
= logical ‘1’
chan ed b
69
= DILATION
8/18/2019 Module 2JH
70/111
Dilation Filter
• Global effect of DILATION:
It is useful to think of thestructuring element asrolling along all of the
boundaries of all BLACK
The center point of thestructuring elementtraces out a set of paths.
That form the boundariesof the dilated image.
70
.
8/18/2019 Module 2JH
71/111
Qualitative Properties of Dilation
• Dilation also removes gaps or bays that are
too narrow:
DILATE
71
8/18/2019 Module 2JH
72/111
• For a window B and a binary image I, we
define the morphological filter
= ,
by
, = ,
= AND{I(m- p, n-q); ( p, q) ∈ B}
• Often written as J = I B
72
8/18/2019 Module 2JH
73/111
8/18/2019 Module 2JH
74/111
Erosion Filter
• Global effect of EROSION:
It is useful to think of the
structuring element asrolling inside of the
boundaries of all BLACKobjects in the image.
The center point of the
structuring elementtraces out a set of paths.
That form the boundaries
of the eroded image.
74
8/18/2019 Module 2JH
75/111
Qualitative Properties of Erosion
• ros on removes o ects t at are too sma :
ERODE
• Erosion also removes peninsulas that are
75
8/18/2019 Module 2JH
76/111
8/18/2019 Module 2JH
77/111
Erosion of the BLACK
same as dilation of the
WHITE part!
OT
OT
DILATE RODE
77
8/18/2019 Module 2JH
78/111
• NOT(I ⊕ B) = NOT(I) B (dual operations)
• However, erosion and dilation are only
approximate inverses of one another.• Di ation en arges t e o jects.
– An erosion will return the objects to their original size
–• Erosion shrinks the objects
– A dilation will return the objects to their original size
– but it cannot restore the objects/peninsulas that wereremoved by erosion
78
8/18/2019 Module 2JH
79/111
• For a window B and a binar ima e I we
define the morphological filter
= ,
or
J = MAJORITY(I, B)
by
J(m, n) = MAJ{BI(m, n)}= - -
79
, ,
8/18/2019 Module 2JH
80/111
• Removes both objects and holes of
insufficient size, as well as both gaps (bays)
and eninsulas of insufficient width.
• But generally does not change the overall
them).
-
complementation:
80
=
8/18/2019 Module 2JH
81/111
Median Filter
MAJ
A
C
B
I JB =
The median removed the small object A and the small holehole B, but did not change the boundary (size)
81
.
8/18/2019 Module 2JH
82/111
•
that are too small, as well as both gaps
MEDIAN
82
8/18/2019 Module 2JH
83/111
3-D Median Filter Example
• The following example is a 3-D Laser
Scannin Confocal Microsco e LSCM
image (binarized) of a pollen grain.
Ma nification » 200x
83
Examples of 3-D windows: CUBE(125) and CROSS3-D(13)
8/18/2019 Module 2JH
84/111
84
LSCM image of pollen grain
8/18/2019 Module 2JH
85/111
85
median filter
8/18/2019 Module 2JH
86/111
• Define new morphological operations by
performing the basic ones in sequence.
• Given an ima e I and window B define
OPEN(I, B) = DILATE [ERODE(I, B), B]
or I ο B = (I B) ⊕ B
CLOSE I B = ERODE DILATE I B B
or I ● B = (I ⊕ B) B
86
8/18/2019 Module 2JH
87/111
• In other words,
OPEN = erosion (by B) followed by dilation (by B)
CLOSE = dilation (by B) followed by erosion (by B) ,
holes that are too small, OPEN and CLOSE are“asymetric” smoothers that remove either objects or holes:
– OPEN removes small objects fingers better thanMEDIAN), but not holes, gaps, or bays.
– CLOSE removes small holes/gaps (better thanMEDIAN) but not objects or peninsulas.
• OPEN and CLOSE generally do not affect object size.
87
.
d d
8/18/2019 Module 2JH
88/111
OPEN and CLOSE vs. Median
OPEN
CLOSE
88
8/18/2019 Module 2JH
89/111
8/18/2019 Module 2JH
90/111
C i OPEN CLOSE d CLOSE OPEN
8/18/2019 Module 2JH
91/111
Comparing OPEN-CLOSE and CLOSE-OPEN
CLOSE-OPEN
CLOSE-OPEN
OPEN-CLOSE
91
PEN-CLOSE
8/18/2019 Module 2JH
92/111
• Often, it is of interest to find instances of a certain object
n a nary mage.
• This can be accomplished with simple logical operationsand a tem late.
• The template is similar to a structuring element, but the
pixels of the template have logical values (‘1’ or ‘0’).
a tem late for the letter “P”
the template is like a “mini image”
92
8/18/2019 Module 2JH
93/111
• For two binary variables A and B, define MATCH(A,B) by
A B MATCH(A,B)
0 0 1
0 1 0
1 0 0
1 1 1
.
• Define the MATCH between two images I and J by
= =
93
, , , , , ,
8/18/2019 Module 2JH
94/111
• Let K = MATCH(I, J).
• Define:
M1(I, J) = AND[ K (m,n) ∀ (m,n) ]revea s per ec ma c es on y
M2(I, J) = SUM[ K (m,n) ∀ (m,n) ]
(measures how good the match is)
1 , =
M2(I, J) = 29
94I J K =MATCH(I, J)
8/18/2019 Module 2JH
95/111
• When the template T is centered at pixel (m,n), we define
, ; m, n
to be the MATCH of T and that part of I that is covered by T. It is a
“mini-image” of the same size and shape as T.
• The binary template matching operation can then apply either match
measure M1 or M2 to this “mini-image.”
• By doing this at every pixel (m, n), we obtain a full-size match measure
image M1(I, T; m, n) or M2(I, T; m, n).
• M1(I, T; m, n) is ‘1’ where the neighborhood about pixel (m,n) matches
the template exactly and is ‘0’ elsewhere.
• Each pixel of M2(I, T; m, n) is an integer that gives the count of how
many pixels in the neighborhood about (m,n) match the template. Thiscount quantifies how well the neighborhood “matches” the template.
95
8/18/2019 Module 2JH
96/111
• Because it is a filtering operation that is based on matching, template
.
• It is used often in target detection applications.
• The goal is to find instances of the template T in the image I; i.e. topro uce a nary map mage a s a p xe s w ere con a ns an
instance of T and is ‘0’ elsewhere.
• If we are only interested in detecting exact matches, then the binary
mage 1 , ; m, n can e a en as e na resu .• Except in controlled environments, this is usually impractical because
of noise and variations in the actual target appearance relative to the
, , , .
• Thus, we often threshold the integer image M2(I, T; m, n) instead toobtain a binary “map” image that is ‘1’ where the match is “close
”
96
.
8/18/2019 Module 2JH
97/111
• As usual, choosing the best value for the threshold can be difficult.
•
target in I, then it suffices to set the binary “map” image to ‘1’ at the
pixel where M2(I, T; m, n) is maximized (and to ‘0’ elsewhere).•
I, one common approach is to convert the integer match image
M2(I, T; m, n) to a floating point “percentage match” image.
– , , – The “percentage match” image can then be threhsolded with a threshold
that is between zero and one.
• There are two advanta es to this a roach:
1. It tends to make the task of choosing the threshold conceptually easier;
for example: “declare a detection if the match is 95% or better.”
2. It saves us from having to continually adjust the threshold in applications
97
where the size of the target (and the template) may be changing over
time ̶ as in a video sequence where the target is moving.
Binary Image Compression
8/18/2019 Module 2JH
98/111
Binary Image Compression
• In later Modules we will discuss gray-level
and color ima e and video com ression at
length.
•images.
.
98
-
8/18/2019 Module 2JH
99/111
• The number of bits required to store an M × N nary mage s .
• This can usually be significantly reduced by.
– This kind of coding is called source coding; theoal is to achieve com ression.
– It is different from channel coding, where thegoal is to add bits in order to achieve errorres ency.
• Run-length coding works well if the image‘ ’ ‘ ’
99
.
How Run-Length Coding Works
8/18/2019 Module 2JH
100/111
How Run-Length Coding Works
• Algorithm: For image row m:
(1) Store the first pixel value ('0' or '1') in row m as a
(2) Set run counter c = 1
3 For each pixel in the row:
- Examine the next pixel to the right
- If same as current pixel, set c = c + 1
- eren rom curren p xe , s ore c an se c =
-
Continue until end of row is reached• Each run-length is stored using b bits.
100
Run-Length Code
8/18/2019 Module 2JH
101/111
Run-Length Code
Example
'
row m
stored: '1' 7 5 8 3 1
Comments on Run-Length Coding
.
• If the image contains many runs of 1's and 0's.
101
• ase ne uses o co e e coe c en s.
Run-Length Code
8/18/2019 Module 2JH
102/111
Run-Length Code
• If the image contains only short runs, run-length coding can
increase the required storage.
row m
what'sstored: '1'1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
• Each count is stored with b bits!
• Worst-case exam le: stora e increased b-fold!
• Rule of thumb: average run-length L
>b.
102
T f bi i i i d t i
8/18/2019 Module 2JH
103/111
• Two types of binary images: region images and contour images.
region image contour image
-and/or curved) and single points.
• Each ‘1’ pixel in a contour image can have at most two 8-neighborsthat are also ‘1’.
Example of a neighborhood
103
rom a con our mage
8/18/2019 Module 2JH
104/111
• A binar re ion ima e can be converted into an a roximate contour ima e
by the following procedure.
• Let I be a region image. Define a new binary image J according to:
J(m, n) = ‘1’ if I(m, n) = ‘0’ and I(m, n) has one or more 4-neighbors that are ‘1’
• A ‘0’ ixel and it’s 4-nei hbors: one of them is ‘1’
• Intuitively, this algorithm traces out a contour of ‘1’ pixels around any region(object) of ‘1’ pixels in the input image.
• A ‘1’ pixel is output any time the window is on a ‘0’ pixel in the input image
but touches a ‘1’ pixel.
104
pprox ma e on our enera on
8/18/2019 Module 2JH
105/111
pprox ma e on our enera on• xamp e:
region image contour image
105
8/18/2019 Module 2JH
106/111
•
• The output image is not guaranteed to be a true contour image.
• There may be some ‘1’ pixels in the output image that have more thantwo 8-nei hbors which are also ‘1’.
• Example:
• When this occurs, post processing such as edge thinning mustenerall be a lied to make corrections.
• For this course, we won’t be too concerned about it.
• Later, we will study more complicated edge detection algorithms thatcan be guaranteed to produce a true contour image as output.
106
8/18/2019 Module 2JH
107/111
• The chain code is a highly efficient method
for coding binary contour images.
• -
contour is specified, then the rest of the
sequence of direction codes for how to
107
contour initial pointand directions
• We use the following 8 neighbor direction codes:
8/18/2019 Module 2JH
108/111
• We use the following 8-neighbor direction codes:
0
1 2
3
5 6 7
• Since the numbers 0, 1, 2, 3, 4, 5, 6, 7 can be coded bytheir 3-bit binary equivalents:
000 001 010 011 100 101 110 111
the location of each point on the contour after the initialpoint can be coded by 3 bits.
108
8/18/2019 Module 2JH
109/111
8/18/2019 Module 2JH
110/111
• The end of the code is marked b a “fla ”. One o tion ̶
give the direction that “goes back” on itself: e.g., a ‘4’ and
then a ‘0’.• e compress on can e qu e s gn can : co ng y -
coordinates (B = 9 for 512 x 512 images) requires 6x as
much storage.
• Chain codes are effective for many computer vision and
pattern recognition applications, e.g. character recognition.
• omment: or c ose contours, t e n t a coor nate can e
chosen arbitrarily. If the contour is open, then it should bean end point.
110
Module 2 Comments
8/18/2019 Module 2JH
111/111
Module 2 Comments• any ngs can e accomp s e w
binary images – since shape is often well-preserve w en a graysca e mage s
thresholded.
• However, grayscale images are alsoimportant. We will deal with gray scales
next, but not shape – onward to Module 3.
111