You are on page 1of 70

Department of Mechatronics

Image Acquisition and Pre-processing


MACHINE VISION AND IMAGE PROCESSING
(MTE 4075)

Year/ Semester : IVth / VII th

Name of Faculty:
Dr. Umesh Sahu
(Assistant Professor)
Manipal Institute of Technology, Manipal, India
−1−
Syllabus
Image acquisition and pre-processing: [15]
Vision system components, Image acquisition and analysis, Image
formation - Sampling & Quantization, simple operations on image,
Image enhancement: Spatial Domain and frequency domain
methods, Image noise, image restoration , morphological operations,
Segmentation, image analysis, representation of regions, Feature
extraction, fundamentals of color image processing, conversion of
color spaces.

Manipal Institute of Technology, Manipal, India


−2−
Content:
▪Image Enhancement
–Spatial Domain Techniques
–Frequency Domain Techniques

Manipal Institute of Technology, Manipal, India


−3−
What Is Image Enhancement?
Image enhancement is the process of making images more
useful
The reasons for doing this include:
– Highlighting interesting detail in images
– Removing noise from images
– Making images more visually appealing

Manipal Institute of Technology, Manipal, India


−4−
Image Enhancement Examples
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Manipal Institute of Technology, Manipal, India


−5−
Image Enhancement Examples (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Manipal Institute of Technology, Manipal, India


−6−
Image Enhancement Examples (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Manipal Institute of Technology, Manipal, India


−7−
Image Enhancement Examples (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Enhance

Manipal Institute of Technology, Manipal, India


−8−
Image Enhancement Examples (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Enhance

Manipal Institute of Technology, Manipal, India


−9−
Spatial & Frequency Domains

There are two broad categories of image enhancement


techniques
– Spatial domain techniques
• Direct manipulation of image pixels
– Frequency domain techniques
• Manipulation of Fourier transform or wavelet transform of an image
For the moment we will concentrate on techniques that
operate in the spatial domain

Manipal Institute of Technology, Manipal, India


− 10 −
Spatial Domain Techniques

• Histogram Processing
• Basic Intensity (Gray level) transformations
• Point Processing
• Neighborhood processing (Spatial Filtering)

Manipal Institute of Technology, Manipal, India


− 11 −
Histogram Processing: Image Histogram
Image histogram used to plot the frequency of the intensity values of an
image.

Frequency of occurrence
0 4 8 10 12
12 16 5 0 16
4 16 8 5 10 5 5
10 0 4 12 16 4
5
12 0 16 0 8 3 3 3
4
2
3
2
1
0 4 5 8 10 12 16
Gray Level

Manipal Institute of Technology, Manipal, India


− 12 −
Histogram Processing: Image Histogram

Manipal Institute of Technology, Manipal, India


− 13 −
Histogram Processing: Image Histogram
▪ Image histogram used to plot the frequency of the intensity values of an image.
pixels

130

36 36

22

0 1 2 3 level
Image 16x14 = 224 pixels

Manipal Institute of Technology, Manipal, India


− 14 −
Histogram Processing: Image Histogram
▪ Image histogram used to plot the frequency of the intensity values of an image.

Manipal Institute of Technology, Manipal, India


− 15 −
Histogram Processing:
Histogram of Overexposed Image
7000

6000

5000

4000

3000

2000

1000

0 50 100 150 200 250

Manipal Institute of Technology, Manipal, India


− 16 −
Histogram Processing:
Histogram of Underexposed Image

4
x 10
4

3.5

2.5

1.5

0.5

0 50 100 150 200 250

Manipal Institute of Technology, Manipal, India


− 17 −
Histogram Processing: Image Histogram
(List-I) (List-II)

(a) (1) Quiz?

Match List-I (Image) with List-II


(b) (2) (histogram) and select the correct
answer using the codes given below
the list
Codes a b c d
(c) (A) 1 3 2 4
(3)
(B) 2 1 4 3
(C) 3 2 4 1
(D) 1 2 3 4
(d) (4)

Manipal Institute of Technology, Manipal, India


− 18 −
Histogram Processing:
Analyze an image using Histogram (OpenCV/ MATLAB)
MATLAB/
OpenCV
demo
• OpenCV Python
• To create a histogram of our image data, we use the hist() function.
• use cv2.calcHist() (in-built function in OpenCV) to find the histogram.

• MATLAB
• To create a histogram of our image data, we use the imhist() function.

https://www.geeksforgeeks.org/opencv-python-program-analyze-image-using-histogram/
Manipal Institute of Technology, Manipal, India
− 19 −
Histogram Processing: Histogram Equalization

Manipal Institute of Technology, Manipal, India


− 20 −
Histogram Processing: Histogram Equalization

Histogram h(rk ) = nk
rk is the k th intensity value
nk is the number of pixels in the image with intensity rk
nk
Normalized histogram p(rk ) =
MN
nk : the number of pixels in the image of size M  N with intensity rk

Manipal Institute of Technology, Manipal, India


− 21 −
Histogram Processing: Histogram Equalization
▪Histogram equalization is a method in image processing to adjust
the image contrast using the image's histogram.
▪This method usually increases the global contrast of images.
▪Through this adjustment, the intensities can be equally distributed
on the histogram.
▪This allows for areas of lower local contrast to gain a higher
contrast.
▪Histogram equalization accomplishes this by effectively spreading
out the most frequent intensity values.

Manipal Institute of Technology, Manipal, India


− 22 −
Histogram Processing: Histogram Equalization

Manipal Institute of Technology, Manipal, India


− 23 −
Histogram Processing: Histogram Equalization
The intensity levels in an image may be viewed as random variables in the interval [0, L-1].
Let p r (r) and ps (s) denote the probability density function (PDF) of random variables r and s.

Mathematical Derivation:
Gonzalez, Rafael C., and Richard E. Woods. "Digital Image Processing, New Jersey.", 2007. 3rd edition. (Chapter-3, Page-144)

Manipal Institute of Technology, Manipal, India


− 24 −
Histogram Processing: Histogram Equalization (Example)
Let us suppose we have a 3-bit, 8 x 8 grayscale image. The grayscale range is 2^3 = 8
intensity values (i.e. gray levels) because the image is 3 bits. We label these intensity values 0
through 7. Below is the histogram of this image.

Manipal Institute of Technology, Manipal, India


− 25 −
Histogram Processing: Histogram Equalization (Example)

Manipal Institute of Technology, Manipal, India


− 26 −
Histogram Processing:
Histogram Equalization (Example)

Manipal Institute of Technology, Manipal, India


− 27 −
Histogram Processing:
Histograms Equalization in OpenCV/ MATLAB

• OpenCV Python
• use cv2.equalizeHist() to find the histograms Equalization of a image.

• MATLAB
• Histogram equalization: histeq()

https://www.geeksforgeeks.org/histograms-equalization-opencv/
Manipal Institute of Technology, Manipal, India
− 33 −
Histogram Processing:

▪ Histogram Equalization

▪ Histogram Matching (self Learning)

▪ Local Histogram Processing (self Learning)

▪ Using Histogram Statistics for Image Enhancement (self Learning)

Reference of Self Learning Topic:


Gonzalez, Rafael C., and Richard E. Woods. "Digital Image Processing, New Jersey.", 2007. 3rd edition. (Chapter-3)

Manipal Institute of Technology, Manipal, India


− 34 −
Spatial Domain Techniques

• Histogram Processing
• Basic Intensity (Gray level) transformations
• Point Processing
• Neighborhood processing (Spatial Filtering)

Manipal Institute of Technology, Manipal, India


− 35 −
Intensity Transformation
▪ Why do we require intensity transformation?
– Image Enhancement
g ( x, y ) = T [ f ( x, y )]
▪ Different approaches to transform the intensity of an image:
– Spatial domain (direct mapping)
– Frequency domain (indirect mapping)

Original Processed
Image image
T

(x,y) (x,y)

Image f(x,y) Image g(x,y)

Manipal Institute of Technology, Manipal, India


− 36 −
Spatial Filtering
▪ Filtering refers to accepting(passing) or rejecting certain frequency
components. This effectively smoothens or sharpens the image.

–E.g. Low pass filter/ smoothing filter, high pass filter/ sharpening filters.

▪ Such operations can be directly carried out on an image in spatial domain by


using spatial filters (kernels, spatial masks, templates, & windows).

▪ Spatial filters are versatile as they are used in linear as well as non-linear.

Manipal Institute of Technology, Manipal, India


− 37 −
Spatial Filtering
▪Types of Spatial Filtering:
–Point to point (pixel to pixel) operation
–Mask based (Neighborhood) operations
•Operation with 3x3 filter (E.g. Mean, max, min, etc)
•Correlation or Convolution

Manipal Institute of Technology, Manipal, India


− 39 −
Point Processing
▪ Point operations are zero-memory operations where a given gray level u[0,L]
is mapped to another gray level v[0,L] according to a transformation

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

Original Processed
Image image
T

(x,y) (x,y)

Image f(x,y) Image g(x,y)

Manipal Institute of Technology, Manipal, India


− 41 −
Point Processing

▪Point processing is based on the transformation of intensity


of single pixels of the image under consideration.

▪Three basic functions:


1. Linear (Image negatives, identity transformation)
2. Logarithmic (log and Inverse log transformation)
3. Power-law (nth power and nth root transformation)

Manipal Institute of Technology, Manipal, India


− 42 −
Point Processing: Image Negative

▪s, r: intensity of the pixels after (output image) and before


processing (Input image)
▪T: transformation operator
▪L-1: Maximum intensity level of input image

▪Usually performed to enhance the white or gray details embedded in


dark region of an image.
Manipal Institute of Technology, Manipal, India
− 43 −
Point Processing: Image Negative (Example)

u
0 L

Input Image Transformation Output Image

Manipal Institute of Technology, Manipal, India


− 44 −
Point Processing: Log Transformations

This transformation
maps a narrow range
of low intensity
values in the input
image to a wider
range of output level
and vice versa.

Manipal Institute of Technology, Manipal, India


− 45 −
Point Processing: Log Transformations (example)

Manipal Institute of Technology, Manipal, India


− 46 −
Point Processing: Power-law (Gamma) transformation

• Each input value is raised to the power of


gamma
• Gamma transforms were often used to
correct the intensities in CRT displays

Manipal Institute of Technology, Manipal, India


− 47 −
Point Processing: Power-law (Gamma) transformation

Manipal Institute of Technology, Manipal, India


− 48 −
Point Processing: Power-law (Gamma) transformation

Manipal Institute of Technology, Manipal, India


− 49 −
Neighbourhood Processing
Original Processed
Image image

(x,y) (x,y)

Image f(x,y) Image g(x,y)

Sliding Neighbourhood Operations

Manipal Institute of Technology, Manipal, India


− 57 −
Neighbourhood Processing
Neighbourhood Processing consists of

1. defining a center point


2. performing an operation that involves only the pixels in a predefined neighborhood about that
center point
3. letting the result of that operation be the “response” of the process at that point
4. repeating the process for every point in the image

▪ Known as neighborhood processing or spatial filtering, with the second term being more
prevalent.
▪ If the computations performed on the pixels of the neighborhoods are linear, the
operation is called linear spatial filtering (the term spatial convolution also used);
▪ Otherwise it is called nonlinear spatial filtering.

Manipal Institute of Technology, Manipal, India


− 58 −
Illustration of Spatial filtering

7 9 11
0 0 0 0 0
10 50 8
0 7 9 11 0
9 5 6
Original Image 0 10 50 8 0

0 9 5 6 0
1 1 1

1/9 1 1 1 0 0 0 0 0

1 1 1 Input Image after zero padding


3 x 3 Averaging
Mask
(filter, mask, filter mask,, kernel, template, or window)
Manipal Institute of Technology, Manipal, India
− 59 −
Movement of Spatial Mask

1/9 1/9 1/9 1/9 1/9 1/9


0 0 0 0 0

1/9 1/9 1/9 1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9 1/9 1/9 1/9


0 10 50 8 0

0 9 5 6 0

0 0 0 0 0

0 x 1/9 + 0 x 1/9 + 0 x 1/9 + 0x 1/9 + 7 x 1/9 + 9 x 1/9 + 0 x 1/9 + 10 x 1/9 + 50 x 1/9 = 8.4
Manipal Institute of Technology, Manipal, India
− 60 −
Movement of Spatial Mask (Cont..)

1/9 1/9 1/9


0 0 0 0 0

1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

0 9 5 6 0

0 0 0 0 0

0 x 1/9 + 0 x 1/9 + 0 x 1/9+ 7 x 1/9 + 9 x 1/9 + 11 x 1/9 +10 x 1/9 +50 x 1/9 + 8 x 1/9 = 10.5

Manipal Institute of Technology, Manipal, India


− 61 −
Movement of Spatial Mask (Cont..)

1/9 1/9 1/9


0 0 0 0 0

1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

0 9 5 6 0

0 0 0 0 0

0 x 1/9 + 0 x 1/9 + 0 x 1/9+9 x 1/9 +11 x 1/9 + 0 x 1/9 + 50 x 1/9 + 8 x 1/9 + 0 x 1/9 = 8.6

Manipal Institute of Technology, Manipal, India


− 62 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

0 0 0 0 0

0 x 1/9 + 7 x 1/9 +9 x 1/9 + 0 x 1/9 +10 x 1/9 + 50 x 1/9 + 0 x 1/9 + 9 x 1/9 + 5 x 1/9 = 10
Manipal Institute of Technology, Manipal, India
− 63 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

0 0 0 0 0

=
7 x 1/9 + 9 x 1/9 + 11 x 1/9 +10 x 1/9 + 50 x 1/9+8 x 1/9 + 9 x 1/9 +5 x 1/9 + 6 x 1/9 12.7
Manipal Institute of Technology, Manipal, India
− 64 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

1/9 1/9 1/9


0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

0 0 0 0 0

9 x 1/9 + 11 x 1/9 + 0 x 1/9 + 50 x 1/9 + 8 x 1/9 + 0 x 1/9 + 5 x 1/9+ 6 x 1/9 + 0 x 1/9 = 9.8

Manipal Institute of Technology, Manipal, India


− 65 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

1/9 1/9 1/9


0 0 0 0 0

0 x 1/9 + 10 x 1/9 +12 x 1/9 + 0 x 1/9 + 9 x 1/9 + 5 x 1/9 + 0 x 1/9 + 0x 1/9 + 0 x 1/9 = 8.2
Manipal Institute of Technology, Manipal, India
− 66 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

1/9 1/9 1/9


0 0 0 0 0

10 x 1/9 +50 x 1/9 + 8x 1/9 + 9 x 1/9 + 5 x 1/9 + 6 x 1/9 + 0 x 1/9 + 0 x 1/9 + 0 x 1/9 = 9.7

Manipal Institute of Technology, Manipal, India


− 67 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

0 7 9 11 0

1/9 1/9 1/9


0 10 50 8 0

1/9 1/9 1/9


0 9 5 6 0

1/9 1/9 1/9


0 0 0 0 0

12 x 1/9 + 5 x 1/9 + 0 x 1/9 + 4 x 1/9 + 6 x 1/9+ 0 x 1/9 + 0 x 1/9 + 0 x 1/9 + 0 x 1/9 = 7.6

Manipal Institute of Technology, Manipal, India


− 68 −
Movement of Spatial Mask (Cont..)

0 0 0 0 0

0 8.4 10.5 8.6 0

[[ 8.444445 10.555555 8.666667 ]


[10. 12.777778 9.888889 ] 0 10.0 12.7 9.8 0
[ 8.222222 9.777778 7.6666665]]

0 8.2 9.7 7.6 0

0 0 0 0 0

Manipal Institute of Technology, Manipal, India


− 69 −
Result of Averaging filter

7 9 11 8 11 9

10 50 8 10 13 10

9 5 6 8 10 8

Original Image Image after Spatial Averaging

[[ 8.444445 10.555555 8.666667 ]


[10. 12.777778 9.888889 ]
[ 8.222222 9.777778 7.6666665]]

Manipal Institute of Technology, Manipal, India


− 70 −
Spatial Averaging

3x3
Smoothing
filter

Original Image Smoothened Image

Manipal Institute of Technology, Manipal, India


− 71 −
Spatial Averaging

5x5
Smoothing
filter

Original Image Smoothened Image

Manipal Institute of Technology, Manipal, India


− 72 −
Spatial Filtering: OpenCV/ MATLAB

• MATLAB
• fspecial() useful to create filters
• imfilter() does cross correlation

• OpenCV Python
• Python | Intensity Transformation Operations on Images
https://www.geeksforgeeks.org/python-intensity-transformation-operations-on-images/

• Python | Image blurring using OpenCV


https://www.geeksforgeeks.org/python-image-blurring-using-opencv/

Manipal Institute of Technology, Manipal, India


− 73 −
Average Vs Weighted Average

1 1 1 1
1 1 1
9
1 1 1

Original Image 1 1 2 1
2 4 2
16
1 2 1

Manipal Institute of Technology, Manipal, India


− 80 −
Non-linear Filters

Non-linear Filters

Homomorphic Polynomial Order Statistics Morphological


filters filters filters filters

Median filter Min filter Max filter


Manipal Institute of Technology, Manipal, India
− 81 −
Median filter

Original Image Corrupted Image

Manipal Institute of Technology, Manipal, India


− 82 −
What should be the mask that
Mean and Median Filters convolves with the image and
can produce the mean filter?

▪ The mean filter is a simple sliding-window ▪ The median filter is also a sliding-window
spatial filter that replaces the center value in spatial filter, but it replaces the center value
the window with the average (mean) of all in the window with the median of all the pixel
the pixel values in the window. The window, values in the window. As for the mean filter,
or kernel, is usually square but can be any the kernel is usually square but can be any
shape. An example of mean filtering of a shape. An example of median filtering of a
single 3x3 window of values is shown single 3x3 window of values is shown
below. below.

5 + 3 + 6 + 2 + 1 + 9 + 8 + 4 + 7 = 45
45 / 9 = 5 0, 2, 3, 3, 4, 6, 10, 15, 97

Manipal Institute of Technology, Manipal, India


− 83 −
Median filter

Original Image Corrupted Image Median filtered Image

Manipal Institute of Technology, Manipal, India


− 84 −
Drawbacks of Median filter
• Removes image details
• Signal dependent noise

Variations of Median filter


• Weighted Median filter

• Center Weighted Median filter

• Adaptive Center Weighted Median filter

Manipal Institute of Technology, Manipal, India


− 85 −
Try it by yourself

MATLAB
DEMO

Manipal Institute of Technology, Manipal, India


− 86 −
▪Mean filter has ability to perform de-noising of the images, however,
it produces the blurring effect.
▪Mean filter may perform satisfactory in case of random noise,
Gaussian noise.
▪Median filter do not produces as much as blurring as mean filter
does.
▪Median filter may perform satisfactory in case of salt and pepper
noise/ impulsive noise.

Manipal Institute of Technology, Manipal, India


− 87 −
Practice Problem
Gonzalez, Rafael C., and Richard E. Woods. "Digital Image Processing,
New Jersey.", 2007. 3rd edition.

Example 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.9


3.7, 3.8, 3.10, 3.11, 3.12 (optional)

Manipal Institute of Technology, Manipal, India


− 98 −
Summary
▪We have looked at:
❑Image Enhancement
• Spatial Domain Technique
❖Histogram processing
❖Intensity Transformation Function
•Point Processing
✓ Linear/Logarithmic/ power-law transform
•Neighbourhood Processing
✓ Smoothing spatial filter-LPF End of
• Average filter Lecture-2
• Median and Mean filter
✓ Sharpning spatial filter-HPF

Manipal Institute of Technology, Manipal, India


− 99 −
Thank you
for your attention

Manipal Institute of Technology, Manipal, India


− 100 −

You might also like