Chapter - 3 Image Filtering
Chapter - 3 Image Filtering
Chapter 3
Woldia University
IOT
March 2021
Outline
• Image Filtering
• Image Thresholding
Global thresholding,
Adaptive thresholding, and
Otsu’s binarization
• Transformation
• Concept of Blurring and Edge detection
Question: Noise reduction
Give a camera and a still scene, how can you reduce noise?
Take lots of images and average them!
What is Convolution?
Linear filtering of an image is accomplished through an operation called
convolution.
Convolution is a neighborhood operation in which each output pixel is the
weighted sum of neighboring input pixels.
The matrix of weights is called the convolution kernel, also known as the
filter. A convolution kernel is a correlation kernel that has been rotated
180 degrees.
Linear and Nonlinear filters
Convolution
For example, suppose the image is
A = [ 17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
The following figure shows how to compute the (2,4) output pixel using
these steps:
1. Rotate the convolution kernel 180 degrees about its center element.
2. Slide the center element of the convolution kernel so that it lies on top of
the (2,4) element of A.
3. Multiply each weight in the rotated convolution kernel by the pixel of A
underneath.
4. Sum the individual products from step 3.
Linear and Nonlinear filters
Nonlinear filters are those for which the linearity relationship breaks
down.
Consider two signals A and B, for linear filter such as mean filter Fm,
you have:
Fm (A + λB) = Fm (A) + λFm (B),
but the equation is not satisfied for a nonlinear filter such as the median
filter.
Linear and Nonlinear filters
Example:
Linear and Nonlinear filters
To understand the linearity property more easily. Let us consider the above
diagram, here we have 2 sequences namely Xn and Yn. when we add both
the sequence we get Xn+Yn whose amplitude value are represented with
blue color. when any system which satisfy this condition then it is called
linear.
In case of mean filter, mean value for sequence Xn is 1+1+3/3=5/3. Mean
value for sequence Yn is 1+2+0/3=1. Mean value for Xn+Yn is
2+3+3/3=8/3.
Linear and Nonlinear filters
mean(Xn)+mean(Yn)=mean(Xn+Yn)
5/3+1=8/3
hence we called mean filter as linear filter.
Linear and Nonlinear filters
In case of median filter, if we calculate median value for sequence Xn, we get
1 (arrange the sequence in ascending order and then find the middle value).
similarly median value of sequence Yn is 1 .
median value of sequence Xn+Yn is 3.
median(Xn) + median(Yn) ≠ median(Xn+Yn).
1+1 ≠ 3
hence we call median filter as non-linear filter
Median filter
18 22 33
34 24 24
22 19 32
Median filter
19 22 24 24 25 31 32 33 172
Median filter
22 33 25
24 25 172
19 32 31
Finally we will have an output image as bellow,
18 22 33 25
34 24 25 172
22 19 32 31
Image Thresholding
If pixel value is greater than a threshold value, it is assigned one value (may
be white), else it is assigned another value (may be black).
The function used is “cv2.threshold’’.
First argument is the source image, which should be a grayscale image.
Second argument is the threshold value which is used to classify the pixel
values.
Third argument is the maxVal which represents the value to be given if pixel
value is more than (sometimes less than) the threshold value.
Simple Thresholding
In this, the algorithm calculate the threshold for a small regions of the image.
So we get different thresholds for different regions of the same image and it
gives us better results for images with varying illumination.
It has three ‘special’ input parameters and only one output argument.
Adaptive Thresholding
Result
Otsu’s Binarization
For that image, we can approximately take a value in the middle of those
peaks as threshold value.
That is what Otsu binarization does.
So in simple words, it automatically calculates a threshold value from image
histogram for a bimodal image. (For images which are not bimodal,
binarization won’t be accurate.)
Otsu’s Binarization
For this, our cv2.threshold() function is used, but pass an extra flag,
cv2.THRESH_OTSU.
For threshold value, simply pass zero.
Then the algorithm finds the optimal threshold value and returns you as the
second output, retVal.
If Otsu thresholding is not used, retVal is same as the threshold value you
used.
Otsu’s Binarization
Otsu’s Binarization
Geometric Transformations of Images
Goals:
Learn to apply different geometric transformation to images like translation, rotation,
scaling, reflection, affine transformation etc.
Using a function: cv2.getPerspectiveTransform
Transformations
You can take make it into a Numpy array of type “np.float32” and pass it into
“cv2.warpAffine()” function.
Let you see the bellow example for shifting the image by (35,50)
Translation
Translation
Rotation
Suppose a 2D point is reflected about the y-axis then the matrix used to represent this is:
Applying transformations to 2D points
Q1. Using the transformation matrix, translate the point (2,6) by 4 in the x-direction and
by -3 in the y-direction.
Applying transformations to 2D points
Q1. Using the transformation matrix, translate the point (2,6) by 4 in the x-direction and
by -3 in the y-direction.
Applying transformations to 2D points
Q2. Using a 3D transformation matrix, rotate the point (2,6) by 90 degrees anti-
clockwise about (0,0).
Applying transformations to 2D points
Q2. Using a 3D transformation matrix, rotate the point (2,6) by 90 degrees anti-
clockwise about (0,0).
Applying transformations to 2D points
Q3. Using a 3D transformation matrix, firstly, translate the point (2,6) by 4 in the x-
direction and -3 in the y-direction and then rotate it anti-clockwise by 90 degrees about
(0,0).
Applying transformations to 2D points
Q3. Using a 3D transformation matrix, firstly, translate the point (2,6) by 4 in the x-
direction and -3 in the y-direction and then rotate it anti-clockwise by 90 degrees about
(0,0).
Applying transformations to 2D points
Q4. The triangle Q is defined by the points a(2,6), b(2,10), and c(6,8).
Using a 3D combo matrix, apply the transformation to Q which:
firstly, translates Q by 4 in the x direction and -3 in the y direction and then rotates it anti-
clockwise by 90 degrees about (0,0).
Applying transformations to 2D points
Q4. The triangle Q is defined by the points a(2,6), b(2,10), and c(6,8).
Using a 3D combo matrix, apply the transformation to Q which:
firstly, translates Q by 4 in the x direction and -3 in the y direction and then rotates it anti-
clockwise by 90 degrees about (0,0).
Applying transformations to 2D points
Q4. The triangle Q is defined by the points a(2,6), b(2,10), and c(6,8).
Using a 3D combo matrix, apply the transformation to Q which:
firstly, translates Q by 4 in the x direction and -3 in the y direction and then rotates it anti-
clockwise by 90 degrees about (0,0).
Scaling and reflection transformations
Q5. Scale the line joining the points (2,6) and (6,8) by 2 in the x-direction and 0.5 in the
y-direction.
Scaling and reflection transformations
Q5. Scale the line joining the points (2,6) and (6,8) by 2 in the x-direction and 0.5 in the
y-direction.
Scaling and reflection transformations
Q6. Using a 3D transformation matrix reflect the point (2,6) in the x-axis.
Affine Transformation
E.g. satellite imagery uses affine transformations to correct for wide angle lens distortion,
panorama stitching, and image registration.
Image registration is an image processing technique used to align multiple scenes into a
single integrated image. It helps overcome issues such as image rotation, scale, and skew
that are common when overlaying images.
Image registration is often used in medical and satellite imagery to align images from
different camera sources. Digital cameras use image registration to align and connect
adjacent images into a single panoramic image.
Affine Transformation
To find the transformation matrix, we need three points from input image
and their corresponding locations in output image.
“cv2.getAffineTransform” will create a 2x3 matrix which is to be passed
to “cv2.warpAffine”
Affine Transformation
Affine Transformation
//End…//
References..
“Digital Image Processing, Rafael C. Gonzalez”
“Computer Vision: Algorithms and Applications”, Richard Szeliski