Beruflich Dokumente
Kultur Dokumente
and Quantization
4/1/2021 1
Outlines
• Image Acquisition
• Sampling and Quantization
• Image Sensors
4/1/2021 2
Image Acquisition
4/1/2021 3
Image description
4/1/2021 4
Sampling and Quantization
quantization
sampling
4/1/2021 5
Sampling and Quantization
Sampling: Digitization of the spatial coordinates (x,y)
Quantization: Digitization in amplitude (also called gray-
level quantization)
8 bit quantization: 28 = 256 gray levels (0: black, 255: white)
Binary (1 bit quantization): 2 gray levels (0: black, 1: white)
4/1/2021 6
Sampling and Quantization
. . . . . .
. . . . . .
. . . . . .
f ( M 1,0) f ( M 1,1) . . . f ( M 1, N 1)
N : No of Columns
M : No of Rows
4/1/2021 7
Sampling and Quantization
Image coordinate convention (not valid for MATLAB!)
4/1/2021 8
Sampling and Quantization
4/1/2021 9
Digital Images
4/1/2021 10
Sampling
4/1/2021 11
Sampling
4/1/2021 COMSATS Institute of Information Technology, Lahore Digital Image Processing CSC331 12
Effect of Sampling and Quantization
4/1/2021 14
Effect of Sampling and Quantization
4/1/2021 15
Effect of Sampling and Quantization
4/1/2021 16
imread() – reading an image with different
postfixes
figure;imshow(im1)
figure;imshow(im2)
figure;imshow(im3)
figure;imshow(im4)
figure;imshow(im5)
figure;
subplot(2,3,1);imshow(im1);subplot(2,3,2);imshow(im2)
subplot(2,3,3);imshow(im3);subplot(2,3,4);imshow(im4)
subplot(2,3,5);imshow(im5);subplot(2,3,6);imshow(im6)
Quantization
im=imread('obelix.jpg');
im=rgb2gray(imread('obelix.jpg'));
im1=imresize(im, [1024 1024]);
im2= gray2ind(im1,2^7);
im3= gray2ind(im1,2^6);
im4= gray2ind(im1,2^5);
im5= gray2ind(im1,2^4);
im6= gray2ind(im1,2^3);
im7= gray2ind(im1,2^2);
im8= gray2ind(im1,2^1);
figure;
subplot(2,4,1);imshow(im1,[]);subplot(2,4,2);imshow(im2,[])
subplot(2,4,3);imshow(im3,[]);subplot(2,4,4);imshow(im4,[])
subplot(2,4,5);imshow(im5,[]);subplot(2,4,6);imshow(im6,[])
subplot(2,4,7);imshow(im7,[]);subplot(2,4,8);imshow(im8,[])
RGB (color) Images
4/1/2021 22
Image acquisition
Line sensor
Array sensor
4/1/2021 23
Image Sensors
Common technologies:
– CCDs (charge-coupled devices)
– CMOS (complementary metal oxide semiconductor)
4/1/2021 24
Digital Camera Technologies
4/1/2021 25
Digital Camera Technologies
4/1/2021 26
Digital camera technologies comparison
CCD (Charge Coupled Device) CMOS (Complementary Metal
–Specialized fabrication Oxide Semiconductor)
techniques are used so –Cheaper technology
expensive technology –Smaller size
–Larger size –Low power consumption
–Higher power consumption –Readout for selective area of an
because of the capacitive image is possible
architecture –Amplifier and additional
–Always have to read out the circuitry can be fabricated
whole image inside each pixel.
–Resolution is limited by –Higher resolution possible
sensor elements size –Stronger noise due to higher
–Less on-chip circuitry so dark currents because of more
lesser dark currents and on-chip circuitry
noise
4/1/2021 27
Acquisition of color images
Single sensor assembly
For still scenes
Three sensors
with prisms
Sensor arrays
a. Stripe filter pattern
b. Bayers filter pattern
4/1/2021 28
Foveon X3 imager
This sensor uses 3 layers of
CMOS imagers.
Each layer absorbs different
colours of light at different
depths.
Single shot camera for three
colors
4/1/2021 29
Various commercial sensor sizes
4/1/2021 30
Basic relationships and distance
measures between pixels
Pixel Neighborhood
4/1/2021 32
Neighbors of a Pixel
Adjacency: Two pixels are adjacent if they are neighbors and their
intensity level ‘V’ satisfy some specific criteria of similarity.
e.g. V = {1}
V = { 0, 2}
Binary image = { 0, 1}
Gray scale image = { 0, 1, 2, ------, 255}
In binary images, 2 pixels are adjacent if they are neighbors & have
some intensity values either 0 or 1.
4-adjacency: Two pixels p and q with the values from set ‘V’ are
4-adjacent if q is in the set of N4(p).
e.g. V = { 0, 1}
1 1 0
1 1 0
1 0 1
p in RED color
q can be any value in GREEN color.
Adjacency, Connectivity
8-adjacency: Two pixels p and q with the values from set ‘V’ are
8-adjacent if q is in the set of N8(p).
e.g. V = { 1, 2}
0 1 1
0 2 0
0 0 1
p in RED color
q can be any value in GREEN color
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p) OR
(ii) q is in ND(p) & the set N4(p) n N4(q) have no pixels whose
values are from ‘V’.
e.g. V = { 1 }
0a 1b 1c
0d 1e 0f
0g 0h 1i
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p)
e.g. V = { 1 }
(i) b & c
0a 1b 1c
0d 1e 0f
0g 0h 1I
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p)
e.g. V = { 1 }
(i) b & c
0a 1b 1c
0d 1e 0f
0g 0h 1I
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p)
e.g. V = { 1 }
(ii) b & e
0a 1b 1c
0d 1e 0f
0g 0h 1I
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p)
e.g. V = { 1 }
(ii) b & e
0a 1b 1c
0d 1e 0f
0g 0h 1I
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p) OR
e.g. V = { 1 }
(iii) e & i
0a 1b 1c
0d 1e 0f
0g 0h 1i
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in ND(p) & the set N4(p) n N4(q) have no pixels whose
values are from ‘V’.
e.g. V = { 1 }
(iii) e & i
0a 1b 1c
0d 1e 0f
0g 0h 1I
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in ND(p) & the set N4(p) n N4(q) have no pixels whose
values are from ‘V’.
e.g. V = { 1 }
(iii) e & i
0a 1b 1c
0d 1e 0f
0g 0h 1I
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p) OR
(ii) q is in ND(p) & the set N4(p) n N4(q) have no pixels whose
values are from ‘V’.
e.g. V = { 1 }
(iv) e & c
0a 1b 1c
0d 1e 0f
0g 0h 1I
Adjacency, Connectivity
m-adjacency: Two pixels p and q with the values from set ‘V’ are
m-adjacent if
(i) q is in N4(p) OR
(ii) q is in ND(p) & the set N4(p) n N4(q) have no pixels whose
values are from ‘V’.
e.g. V = { 1 }
(iv) e & c
0a 1b 1c
0d 1e 0f
0g 0h 1I
Soln: e & c are NOT m-adjacent.
Adjacency, Connectivity
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-4 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-8 path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
Paths
Example # 1:
Shortest-m path:
V = {1, 2}.
4 2 3 2q
3 3 1 3
2 3 2 2
p2 1 2 3
1 1 1
1 0 1 Ri
0 1 0
0 0 1
1 1 1 Rj
1 1 1
Regions & Boundaries
D4( p, q) = |x - s| + |y - t|
D8( p, q) = max( |x - s| , |y - t| )
78
Two major areas of color image processing
79
Color Spectrum
In the mid seventeen century, Sir Issac Newton
discovered that light passed through the glass prism
comes out as spectrum of colors ranging from violet
to red.
So, the color spectrum can be divided into SIX
regions.
80
Characterization of Light
Achromatic Light
– Intensity measured by gray levels
Chromatic Light
There are three basic quantities to describe the quality of a chromatic light.
– Radiance : total amount of energy that flows from the light source (Watts)
81
Primary Colors of Light
Additive model
Secondary Colors
Secondary Colors (CMY)
Cyan (green + blue)
Magenta (red + blue)
Yellow (red + green)
C 1 R
M 1 G
Y 1 B
Pigment Primaries
Color Distinguishing Characteristics
The following THREE attributes are used to distinguish one color from
another:
1. Intensity(Brightness): Chromatic notion of intensity, used to
describe color sensation.
2. Hue: associated with the dominant wavelength in a mixture of
light waves. It represents dominant color as perceived by an
observer.
3. Saturation: refers to the relative purity or the amount of white
light mixed with a hue ( Spectrum colors are fully saturated).
Hue & saturation taken together are called chromaticity
Trichromatic coefficients
Tristimulus value: amount of R, G, and B required to
form a particular color ( X, Y , Z)
Trichromatic
X
coefficients:
Y
portions
Z
of R, G, and B (x,
x y, z) , y , and z .
X Y Z X Y Z X Y Z
x y z 1
Chromaticity Diagram
88
Analysis of Color Spaces
RGB
Properties
• Default color space and primary colors of light
• Based on Cartesian coordinate system R
• It is an elementary & simplest representation based in terms of
wavelengths of R, G & B in white light
• Device-Independent
Applications G
• Used in various colored image processing applications including
boundary extraction on color bases, segmentation and
recognition of objects in color images
• The hardware oriented models most commonly use RGB space
• RGB is supported by various devices like CRT Monitors, B
Cameras, etc.
Analysis of Color Spaces
RGB
Pros
• More near to real colors
• Simple and easy to implement
• Matches nicely with the human visual system (eye), therefore strongly perceptive to R, G & B primaries.
• Ideal for image color generation
• R, G & B components with equal contributions produce a gray level shades
• If components are contributed with their maximum equal strengths then white color is produced (Black if no
contribution)
Cons
• Not intuitive to human perception
• High correlation between the channels
• Mixing of chrominance and luminance data
• Significant perceptual non uniformity
Analysis of Color Spaces
CMY
Properties
• Secondary colors of light derived from RGB by
subtracting one primary component
Applications
• Used in most of devices that deposit colors on
paper, such as printers, copiers, require CMY data.
Pros
• The maximum equal contribution of all components
of CMY produces black color
Cons
• Not suitable for describing colors in terms that are
practical for human interpretation
Analysis of Color Spaces
CMYK
• In CMYK, K is the fourth color, black; because equal amount of CMY
produces a muddy-looking black, since black is the predominant color in
printing, we need to produce true, pure black
K = min(C,M,Y)
– Hue: color attribute that describes a pure color (e.g., pure yellow or red)
HSI is ideal for processing color image based on the color sensing properties of
the human visual system
4/1/2021 93
Analysis of Color Spaces
HSV/HSI
Characteristics
• Used in various Computer Graphics applications
H
• Also used in image processing applications such as
Image enhancement by manipulating only its intensity
channel
• As intensity channel alone can be handled separately S
so brightness can be controlled by just dealing with
this channel alone without affecting chromaticity
information
• Medical image segmentation using HSI model
V
Analysis of Color Spaces
YUV (YCbCr) color
YUV is used worldwide for TV and video encoding standards.
• YUV allows separating the color information from the
luminance component
Y
• A conversion of an RGB signal to YUV requires just a
linear transform-easy to implement
Y: Luminance
U, Cb: Chroma channel, U axis, blue component
V, Cr: Chroma channel, V axis, red component Cb
Y 0.299 0.587 0.114 R
U 0.147 0.289 0.436 G
V 0.615 0.515 0.1 B
Cr
In MATLAB: rgb2ycbcr and ycbcr2rgb
Analysis of Color Spaces
YCbCr
Pros
• Useful if we need to change Brightness/Contrast component of a
color image without altering the color component of that image
Cons
• Not an absolute color space
• Can not be represented in the corresponding RGB domain. This
causes some difficulty in determining how to correctly interpret and
display some of its signals.
Analysis of Color Spaces
L*a*b
Properties
• L*a*b* space has CIE specifications that attempts to make the
luminance scale more perceptually uniform. It is derived from
L
CIE- XYZ space. L* is a non linear scaling of L
• Device-Independent
Applications
a
• Used in creative art, advertising, graphic art, digitized or
animated paintings etc.
• Applied to image coding for printing
• Color calibration
• Medical imaging especially dentistry b
• Segmentation of natural scenes
Analysis of Color Spaces
L*a*b
Pros
• L* values relates linearly to human perception of brightness.
• It is useful in imaging systems where exact perceptual reproduction of color image across the entire
system is of primary concern
• It is useful if we need to change Brightness/Contrast component of a color image without altering
the color component of that image
Cons
• Disadvantage in real time processing where computational resources are important
Most information in intensity
Only color shown – constant intensity Only intensity shown – constant color
Original image
Exercise
Hint: May use the combination of HSV and YCbCr color spaces
1. http://www.hamradio.si/~s51kq/V-CIV.HTM
2. http://research.ijais.org/volume2/number2/ijais12-450264.pdf
Image Enhancement
• Pixel/Intensity Transformation
• Histogram Processing
• Spatial Filtering
Image Enhancement
Image enhancement is a process to make the result more suitable than the
original image for a specific application
Image enhancement is a subjective (problem/application oriented) image
enhancement approch.
Spatial domain: Direct manipulation of pixel in an image (on the image
plane)
Frequency domain: Processing the image based on modifying the Fourier
transform of an image
Many techniques are based on various combinations of methods from
these two categories
102
Image Enhancement
Goals:
– To improve the subjective quality of an image for human
viewing; (an image needs improvement)
103
Image Enhancement Process
104
Image Enhancement
4/1/2021 105
Basic concepts
Spatial domain enhancement methods can be generalized as
g(x,y)=T[f(x,y)]
f(x,y) : input image
g(x,y): processed (output) image
T[*] : an operator on f (or a set of input images), defined
over intensity (x,y) or neighborhood of (x,y)
Neighborhood about (x,y): a square or rectangular sub-
image area centered at (x,y)
106
Basic Concepts
107
Basic concepts
Pixel/Point operation:
Neighborhood of size 1x1: g depends only on f at (x,y)
T: a gray-level/intensity transformation/mapping function
Let r = f(x,y) s = g(x,y)
r and s represent gray levels of f and g at (x,y)
Then s = T(r)
Local operations: g depends on the predefined number of neighbors of f at
(x,y) implemented by using mask processing or filtering
Masks (filters, windows, kernels, templates) : a small (e.g. 3×3) 2-D array,
in which the values of the coefficients determine the nature of the process
108
Common pixel operations
• Image negatives
• Log transformations
• Power-law
transformations
• Contrast stretching
109
Image Negative
>> f = imread('xray.tif');
>> g = imcomplement(f);
>> imshow(f), figure, imshow(g)
Image negatives
s =T(r) = (L-1)-r
111
Log transformations
Function of s = c Log(1+r)
112
Log transformations
Properties of log transformations
– For lower amplitudes of input image the range of gray levels is
expanded
– For higher amplitudes of input image the range of gray levels is
compressed
Application:
– Dynamic range of a processed image far exceeds the capability
of the display device
• (e.g. display of the Fourier spectrum of an image)
– Also called “dynamic-range compression / expansion”
Log transformations
114
Log transformation
s = crg ,
where c & g
are positive
Plots of equation
s = crg,
For various values of
g (c 1)
116
Power-law Transformation
For γ < 1: Expands values of dark pixels, compress values of brighter pixels
For γ > 1: Compresses values of dark pixels, expand values of brighter
pixels
If γ=1 & c=1: Identity transformation (s = r)
A variety of devices (image capture, printing, display) respond according to
a power law and need to be corrected;
Gamma (γ) correction
The process used to correct the power-law response phenomena
117
Power-law Transformation: Example
Original Result of
satellite applying power-
image law
transformation
c = 1, g 3.0
Result of Result of
applying applying power-
power-law law
transformation transformation
c = 1, g 4.0 c = 1, g 5.0
118
Power-Law Transformation Example
>> f = imread('xray.tif');
>> g = imadjust(f, [ ], [ ], 0.5);
>> imshow(f), figure, imshow(g)
Contrast stretching
Goal:
• Increase the dynamic range of the gray levels for low contrast images
• Low-contrast images can result from
– poor illumination
– lack of dynamic range in the imaging sensor
– wrong setting of a lens aperture during image acquisition
120
Contrast-Stretching
121
Contrast-Stretching
Auto Contrast
4/1/2021 122
Contrast-Stretching
I =Auto Contrast Example
imread('salzburg_before.png');
f = double(I);
fmin = min(min(f))
fmax = max(max(f))
g = uint8(255*(f-fmin)/(fmax-fmin));
imshow(g)
123
Contrast-Stretching
s = T(r) = 1 / (1 + (m/r)E), where 0 < m < r
>> f = imread('pollen.tif');
>> fd = im2double(f);
>> m = mean2(fd)
>> g = 1./(1+(m./(fd+eps)).^4);
>> imshow(f), figure, imshow(g)
Exercise
Identify which intensity transformation was used on liftingbody.png to
create each of the four results below. Write a script to reproduce the
results using the intensity transformation functions.
Original
h(rk) = nk
Normalized Histogram:
p(rk) = h(rk)/n
= nk/n, (Relative frequency)
>> imhist(f);
Histogram Equalization
>> f = imread('pollen.tif');
>> h = histeq(f);
>> subplot(2,2,1), imshow(f),
subplot(2,2,2), histogram(f),
subplot(2,2,3), imshow(h),
subplot(2,2,4), histogram(h)
Exercise
Write a program which can read an image as an input and do the
following automatically. Show the results of all steps.
a) Find the type of image: binary, gray or RGB.
b) Find the issue in image, over dark, over bright, low contrast, or
normal. (Hint: can use histogram).
c) Resolve the issue if any and show the final image after
enhancement.
d) Test your program on following images:
Neighborhood Processing
Spatial Filtering
w1 w2 w3 f1 f2 f3 g1 g2 g3
w4 w5 w6 f4 f5 f6 g4 g5 g6
w7 w8 w9 f7 f8 f9 g7 g8 g9
0 -1 0 2 3 5 7 2
-1 4 -1 3 4 7 5 2 -3 ? ?
0 -1 0 5 6 5 3 1 ? ? ?
2 6 2 2 1 ? ? ?
7 3 3 2 0
2 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 16 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 16 -1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
0 -12 03 5 7 2
-1 43 -14 7 5 2
0 -1 0
2 1 3 16 -1 5 6 5 3 1
1 2 6 2 2 1
7 3 3 2 0
Spatial Filtering - With Zero Padding
02 -13 0 5 7 2
-13 44 -1 7 5 2
0 -1 0
2 1 3 16 -1 5 6 5 3 1
1 -3 2 6 2 2 1
7 3 3 2 0
Spatial Filtering - Padding Options
>> f=[1 2; 3 4]
f=
1 2
3 4
(a) (b) (c (d
) )
Segmentation of Water in Bottles
b = imread('bottles.jpg');
bL = im2bw(b,0.2);
bH = im2bw(b,0.7);
L = (bL-bH);
L1 = medfilt2(L,[9 9],'symmetric');
subplot(2,3,1),imshow(b),title('Bottles'
),
subplot(2,3,2), imshow(bL),
title('Liquid White'),
subplot(2,3,3), imshow(bH),
title('Liquid Black'),
Morphological Image Processing
• Erosion
• Dilation
• Opening
• Closing
Morphology
Morphological image processing (or morphology)
describes a range of image processing techniques that
deal with the shape (or morphology) of features in an
image
Morphological operations are typically applied to
remove imperfections introduced during segmentation,
boundary extraction, pruning, thinning,
Skeletonization, etc.
What is mathematical morphology?
148
Fundamental concepts and operations
Basic set operations:
– Complement
– Difference
– Translation
– Reflection
149
Fundamental concepts and operations
150
(c) (d)
Figur e 13.1 Basic set operations: (a) set A; (b) translation of A by x = (x 1 , x 2 ); (c) set B ; (d)
– Intersection ~ logical AND
reflection of B ; (e) set A and its complement A c ; (f) set difference (A − B ).
The equivalent expression using conventional image processing notation would be:
⇢
1 if A (x, y) and B (x, y) are both 1
C (x, y) = (13.6)
0 otherwise
This expression leads quite easily to a single MATLAB statement that perform the
– Similarly: intersection operation using the logical operator AND (&). Similarly, complement can be
obtianed using the unary NOT (~) operator, set union can beimplemented using the logical
operator OR (| ) and set difference (A − B ) can be expressed as ( A & ~B) . Figure 13.2
• Complement ~ logical NOT for two binary input images. Please notethat wehavefollowed
showsrepresentativeresults
• theIPT convention, representing foreground (1-valued) pixelsaswhitepixelsagainst ablack
Union ~ logical OR
background.
• Difference ~ A AND (NOT B)
13.2.1 The structuring element
The structuring element (SE) is the basic neighborhood structure associated with morpho-
logical image operations. It is usually represented as a small matrix, whose shape and size
151
Fundamental concepts and operations
Logical equivalents of set theory operations
152
The structuring element
Examples:
square cross
MATLAB functions
– strel
– getsequence
154
The structuring element
Examples:
155
Structuring Elements, Hits & Fits
Structuring Element
B
Fit: All on pixels in the structuring element
cover on pixels in the image
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1
0 0 0 1 1 0 0 0 0 0 0 0
1 1 1
0 0 1 B1 1 1 1 0 C
0 0 0 0
1 1 1
0 1 1 1 1 1 1 1 0 0 0 0
Structurin
0 1 1 1 1 1 1 1 0 0 0 0 g
0 0 1 1 1 1 1 1 0 0 0 0 Element
0 1 0
1
0 0 1 1 1 1 1 1 1 0 0 0 1 1 1
0 0 1 1 1 1 1 A
1 1 1 1 0 0 1 0
0 0 0 0 0 1 1 1 1 1 1 0 Structurin
0 0 0 0 0 0 0 0 0 0 0 0 g
Element
2
Structuring Elements
Structuring elements can be any size and make any shape
However, for simplicity we will use rectangular
structuring elements with their origin at the middle pixel
0 0 1 0 0
1 1 1 0 1 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 0 0 1 1 1 0
0 0 1 0 0
Erosion
Structuring Element
Erosion
161
Dilation and erosion
Erosion – geometrical interpretation
162
Dilation
1 if s hits f
g ( x, y )
0 otherwise
Dilation enlarges objects
Erosion
a = [ 0 0 0 0 0 ; 0 1 1 1 0; 1 1 1 0 0; 0 1 1 1 1; 0 0 0 0 0]
se1 = strel('square',3)
b = imerode (a,se1)
164
Dilation Example
Structuring Element
Dilation
166
Dilation
The value of the output pixel is the maximum value of
all the pixels in the input pixel's neighborhood.
In a binary image, if any of the pixels is set to the value
1, the output pixel is set to 1.
167
Dilation
Dilation – geometrical interpretation
168
Dilation
Dilation – Example
169
Dilation
Dilation – MATLAB example
a = [0 0 0 0 0;
0 1 1 0 0;
0 1 1 0 0;
0 0 1 0 0;
0 0 0 0 0]
se1 = strel('square',3)
b = imdilate (a,se1)
se2 = strel('rectangle', [1
3])
c = imdilate (a,se2)
170
Dilation and Erosion – geometrical interpretation
171
Compound Morphological Operations
More interesting morphological operations can be
performed by performing combinations of erosions and
dilations
The most widely used of these compound operations are:
Opening: f ○ s = (f s) s
Closing: f s = (f s) s
Compound Operations (Opening & Closing)
or:
In MATLAB: imopen
173
Compound operations (Opening & Closing)
In MATLAB: imclose
174
Compound Operations (Example)
175
Compound Operations (Example)
176
Compound Operations (Example)
Operations
supported by
bwmorph
177
Basic Morphological Algorithms
Boundary Extraction
Hole Filling
Extraction of Connected Components
Thinning
Thickening
Skeletons
Pruning
Reconstruction
178
Segmentation of Water in Bottles
b = imread('bottles.jpg');
t=double(multithresh(b,2))/256
bL = im2bw(b,t(1));
bH = im2bw(b,t(2));
L = bL - bH;
se5 = strel('disk',5);
se10 = strel('disk',10);
L1 = imopen(L,se5);
L2 = imclose(L1,se10);
subplot(2,3,1),imshow(b),title('Bottles'),
subplot(2,3,2), imshow(bL), title('Liquid
White'),
subplot(2,3,3), imshow(bH), title('Liquid
Black'),
subplot(2,3,4), imshow(L), title('Liquid
with noise'),
subplot(2,3,5), imshow(L1), title('Liquid
with less noise'),
subplot(2,3,6), imshow(L2), title('Pure
Liquid');
Exercise
[SR] Szeliski R., Computer Vision - Algorithms and Applications, Springer, 2011. (Ch.
4, 5)
[GR1] Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image Processing Using
Matlab, Pearson Education, 2nd edition, 2009. (Ch. 10)
[GR2] Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image Processing, Pearson
Education, 3rd edition, 2007. (Ch. 10)
Image Segmentation
Problem Knowledge
Result
domain base
Image Segmentation Approaches
There are two different approaches for Image segmentation:
1. Similarity-based approach
• Thresholding
• Region growing
• Region splitting and merging
2. Discontinuity-based approach
• Identification of Lines
• Identification of Edges
• Identification isolated points
Thresholding
185
Thresholding
1 if f ( x, y ) T
g ( x, y )
0 if f ( x, y ) T
GLOBAL
ADAPTIVE/LOCAL
Global Thresholding
Global Thresholding
Global Thresholding Algorithm
The global threshold, T, is calculated as follows:
1. Select an initial estimate for T (typically the average grey
level in the image)
2. Segment the image using T to produce two groups of pixels:
o G1 consisting of pixels with grey levels >T and
o G2 consisting pixels with grey levels ≤ T
3. Compute the average grey levels of pixels in G1 to give μ1
and G2 to give μ2
Global Thresholding Algorithm
k 1 L 1
where 0 pq (rq ) 1 pq ( rq )
q 0 q k
k 1 L 1
0 qpq ( rq ) / 0 1 qpq ( rq ) / 1
q 0 q k
k 1
T qpq ( rq )
q 0
Thresholding using Otsu Method
T = graythresh(f)
takes an image, computes its histogram, and then finds the threshold
value that maximizes the between-class variance.
The threshold is returned as a normalized value between 0.0 and 1.0,
where f is the input image and T is the resulting threshold.
Thresholding using Otsu Method: Example
>> I = imread('coins.png');
>> imhist(I)
>> level = graythresh(I)
level =
1600
1400
1200
1000
0.4941 800
600
400
>> imshow(BW)
Problems With Single Value Thresholding
bimodal histograms
Images with other kinds of histograms need
more than a single threshold
Uneven illumination can really upset a single
valued thresholding scheme
Problems With Single Value Thresholding (cont…)
of the bottles.
Think about what the histogram for this
image would look like.
What would happen if we use a single
threshold value?
>> b = imread('bottles.jpg');
>> imhist(b);
Segmentation of Water in Bottles
b = imread('bottles.jpg');
t=double(multithresh(b,2))/256
bL = im2bw(b,t(1));
bH = im2bw(b,t(2));
L = (bL-bH);
L1 = medfilt2(L,[9 9],'symmetric');
subplot(2,3,1),imshow(b),title('Bottles'),
subplot(2,3,2), imshow(bL), title('Liquid
White'),
subplot(2,3,3), imshow(bH), title('Liquid
Black'),
t =
subplot(2,3,4), imshow(L), title('Liquid
with noise'),
0.2070 0.6758
subplot(2,3,5), imshow(L1), title('Pure
Liquid')
The Role of Uneven Illumination
Therefore, either
balance the
illumination or
apply adaptive
thresholding
Region Growing
Region growing: groups pixels or subregions into larger regions.
Pixel aggregation: starts with a set of “seed” points and from these grows
regions by appending to each seed points those neighboring pixels that
have similar properties (such as gray level).
1. Choose the seed pixel(s).
2. Check the neighboring pixels and add them to the region if they are similar to
the seed
3. Repeat step 2 for each of the newly added pixels; stop if no more pixels can be
added
Example
Region-Based Segmentation
Region Splitting
Region Growing: Starts from a set of seed points.
Region Splitting: Starts with the whole image as a single region and
subdivide the regions that do not satisfy a condition.
Image = One Region R
Select a predicate P (gray values etc.)
Successively divide each region into smaller and smaller quadrant regions
so that:
P( Ri ) true
Region-Based Segmentation
Region Splitting
https://www.youtube.com/watch?v=ZAXjI9CFvDU
Region-Based Segmentation
Region Merging
Region merging is the opposite of region splitting.
P( Ri R j ) True
Region Splitting/Merging
Stop when no further split or merge is possible
Region-Based Segmentation
Example
Discontinuity-based approach
• Identification of Lines
• Identification of Edges
Segmentation Examples
Detection of Discontinuities
Detection of Discontinuities
>> T = max(g(:))
T=
391
>> g = g >= T;
>> imshow(g);
Point Detection (cont…)
>> g = abs(imfilter(double(f), w)) >= 10;
>> imshow(g);
Line Detection
mask
After
Result of
processing with
thresholding
-45° line
filtering result
detector
Line Detection (cont…)
>> f = imread('wirebond.tif');
>> imshow(f); % Fig 10.4(a)
>> w = [2 -1 -1; -1 2 -1; -1 -1 2];
w=
2 -1 -1
-1 2 -1
-1 -1 2
>> g = imfilter(double(f), w);
>> imshow(g, [ ]); % Fig 10.4(b)
>> gtop = g(1:120, 1:120);
>> figure, imshow(gtop, [ ]) % Fig 10.4(c)
>> gbot = g(end-119:end, end-119:end);
>> figure, imshow(gbot, [ ]) % Fig 10.4(d)
Line Detection (cont…)
>> g = abs(g);
>> figure, imshow(g, [ ]); % Fig 10.4(e)
>> T = max(g(:))
T=
1530
>> g = g >= T;
>> figure, imshow(g); % Fig 10.4(f)
Edge Detection
Derivative based edge detectors are extremely Original 1st derivative 2nd
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
derivative
sensitive to noise. We need to keep this in mind.
# Detection of Discontinuities
Gradient Operators
First-order derivatives:
– The gradient of an image is a directional change in the
intensity or color in an image. The gradient of an image f(x,y) at
x fx
location (x,y) is defined as the vector:
G
f f
G y y
– The magnitude of this vector:
f mag (f ) G G 2
x
2
y
1
2
Gy
( x, y ) tan 1
– The direction of this vector:
Gx
# Detection of Discontinuities
Gradient Operators
Common Edge Detectors
Given a 3*3 region of an image the following edge detection filters can
be used
Common Edge Detectors
Syntax:
>> f = imread('building.tif');
>> imshow(f); % Fig. 10.6(a)
>> [gv, t] = edge(f, 'sobel', 'vertical');
>> imshow(gv); % Fig. 10.6(b)
>> t
t=
0.0516
>> gv = edge(f, 'sobel', 0.15, 'vertical');
>> imshow(gv); % Fig. 10.6(c)
>> gboth = edge(f, 'sobel', 0.15);
>> imshow(gboth); % Fig. 10.6(d)
Detection of Discontinuities
Edge Detection Using Matlab
>> wm45 = [0 1 2; -1 0 1; -2 -1 0]
wm45 =
0 1 2
-1 0 1
-2 -1 0
>> gm45 = imfilter(double(f), wm45,
'replicate');
>> T = 0.3*max(abs(gm45(:)))
T=
206.7000
>> gm45 = gm45 >= T;
>> figure, imshow(gm45); % Fig. 10.6(f)
Detection of Discontinuities
Gradient Operators
r2
r 2 2
2 2 The Laplacian of a
h( r )
2
e Gaussian (LoG)
4
The Laplacian of a Gaussian sometimes is called the Mexican hat function. It
also can be computed by smoothing the image with the Gaussian smoothing mask,
followed by application of the Laplacian mask.
Detection of Discontinuities
Laplacian Of Gaussian
The Laplacian of Gaussian (or Mexican hat) filter uses the Gaussian
for noise removal and the Laplacian for edge detection
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
>> I = imread('building.tif');
>> imshow(I);
>> ILoG = edge(I, 'log');
>> imshow(ILoG);
Canny Edge Detection
The Canny edge detector is an edge detection
operator that uses a multi-stage algorithm to
detect a wide range of edges in images. It was
developed by John F. Canny in 1986.
Canny Edge Detection: Process of
Algorithms
o Apply Gaussian filter to smooth the image in order to remove the
noise
o Find the intensity gradients of the image
o Apply non-maximum suppression to get rid of spurious response to
edge detection (take the point that is local maximum of the gradient
magnitude)
o Apply double threshold to determine potential edges
o Track edge by hysteresis: Finalize the detection of edges by
suppressing all the other edges that are weak and not connected to
strong edges.
Canny Edge Detection
>> I = imread('building.tif');
>> imshow(I);
>> IC = edge(I,'canny');
>> imshow(IC);
Summary