Chapter - 4 Histogram, Frequency Domain
Chapter - 4 Histogram, Frequency Domain
Chapter 4
Woldia University
IOT
March 2021
Outline
• Histograms: Find, Plot, Analyse
• Spatial domain
• Frequency domain: DFT, DCT, and HT
• Template Matching
Histograms
Goal:
Learn to:
Find histograms, using both OpenCV and Numpy functions
Plot histograms, using OpenCV and Matplotlib functions
You will see these functions : cv2.calcHist(), np.histogram() etc.
Histograms
So what is histogram ?
You can consider histogram as a graph or plot, which gives you an overall idea about the
intensity distribution of an image.
It is a plot with pixel values (ranging from 0 to 255, not always) in X-axis and
corresponding number of pixels in the image on Y-axis.
It is just another way of understanding the image.
By looking at the histogram of an image, you get intuition about contrast, brightness,
intensity distribution etc of that image.
Histograms
2. Using OpenCV:
We used cv2.calcHist() to find the histogram of the
full image.
What if you want to find histograms of some
regions of an image?
Just create a mask image with white color on the
region you want to find histogram and black
otherwise.
Then pass this as the mask.
Histograms
In frequency domain we don’t analyze signal with respect to time , instead with respect of
frequency.
Difference b/n Spatial domain and Frequency domain:
In spatial domain , we deal with images as it is. The value of the pixels of the image
change with respect to scene.
Whereas in frequency domain , we deal with the rate at which the pixel values are
changing in spatial domain.
Frequency domain
Spatial domain:
Frequency domain:
We first transform the image to its frequency distribution.
Then our black box system perform what ever processing it has to performed , and the
output of the black box in this case is not an image , but a transformation.
After performing inverse transformation , it is converted into an image which is then
viewed in spatial domain.
Frequency domain
Transformation:
A signal can be converted from time domain into frequency
domain using mathematical operators called transforms.
Types of Transformation are:
1. Fourier Series
2. Fourier transformation
3. Laplace transform
4. Z transform
5. etc..
Frequency domain
Frequency Components:
Any image in spatial domain can be represented in a frequency domain. But what do this
frequencies actually mean.
Frequency components divide into two major components.
High Frequency Components: correspond to edges in an image.
Low Frequency Components: in an image correspond to smooth regions.
Frequency domain
FOURIER:
Fourier was a mathematician in 1822.
He give Fourier series and Fourier transform to convert
a signal into frequency domain.
Frequency domain
Which one is applied on images, the Fourier series or the Fourier transform?
Since the images are non periodic , so Fourier transform is used to convert them into
frequency domain.
Fourier transform:
DISCRETE FOURIER TRANSFORM.
Since we are dealing with images, and in fact digital images , so for digital images we will
be working on discrete Fourier transform.
Frequency domain
f(x,y) denotes the image , and F(u,v) denotes the discrete Fourier transform.
The formula for 2 dimensional inverse discrete Fourier transform is:
Frequency domain
Fourier Transform:
We as a human being can easily
identify the image as airplane, but
computers can’t.
Computers can pick a lot of details
and come up with new answers that
human might be miss.
Frequency domain
Humans are good in reading Handwritten and Printed text. But Computers really struggle with it.
Frequency domain
One of the method the we use for recognizing characters is to use the Fourier
transform.
When we apply transform to something we are just taking some information that is
captured in the original data, but we displayed in a different way.
Frequency domain
If you just take the Fourier transform of the signal, we move from the time
domain to the frequency domain.
1. DCT
2. DFT
3. HT
Frequency domain
1. DCT [Discrete Cosine Transform] i.e. Cosine indicates there are only real terms.
DCT uses real cosine values.
DCT
f --------------> F
[Spatial domain] ? [DFT] [Frequency domain]
• F(u)
for 1 Dimensional, F(u,v) for 2Dimensional.
F = C.f for 1D
F = C.f.CT for 2D
Where, C denote Cosine Transform Matrix
C(u,v) = , for the 1st raw, u=0, 0v N-1
= cos[] , for the other rows, 1 u N-1
0 v N-1
Frequency domain
Let N = 4
C(u,v) = u & v -> 0 to N-1
For the first raw, = = 0.5
(CT)T = C
Frequency domain
DCT:
C = CT -----> Not symmetric matrix
DCT is real term, and orthogonal.
DFT has the complex terms, and it is also a Sinusoidal function.
DCT also used in JPEG Compression.
DCT also known as Sinusoidal function., which is nothing but you can manipulate in terms
of sine. That is it changes with time.
Frequency domain
T f
4 8 12 8
-2 -2 -2 -2
x =
0 0 0 0
-2 -2 -2 -2
Frequency domain
TT
32 -8 0 -8
-8 0 0 0
x =
0 0 0 0
-8 0 0 0
Frequency domain
[ ]
4x4 matrix of Hadamard H = 1 1 1 1
1 -1 1 -1
2 1 -1 -1
1 -1 -1 1
4x4
Frequency domain
[ ]
1 1 1 1 1 1 1 1
8x8 matrix of Hadamard
1 -1 1 -1 1 -1 1 -1
H = 2 1 -1 -1 2 1 -1 -1
1 -1 -1 1 1 -1 -1 1
3 1 1 1 -1 -1 -1 -1
1 -1 1 -1 -1 1 -1 1
4 1 -1 -1 -1 -1 1 1
8x8
1 -1 -1 1 -1 1 1 -1
Frequency domain
[ ] [ ]
1 6
2 = -4
0 0
3 2
Matrix Multiplication
We just convert an image from spatial domain f(x) to frequency domain F.
Frequency domain
[ ]
Q2: f = 2 1 2 1
3 2 3 2 => Apply HT ?
2 3 4 3
1 2 3 2
4x4
Soln.
2D: F = H.f. HT = H.f.H
[ ]
6 8 12 8
= 2 0 0 0
0 -2 -2 -2
0 -2 -2 -2
Frequency domain
[ ]
34 2 6 6
= 2 2 2 2
-6 2 2 2
-6 2 2 2
=
Fourier Transform in Numpy
Now once you got the result, zero frequency component (DC component) will be at top left
corner.
If you want to bring it to center, you need to shift the result by N/2 in both the directions.
This is simply done by the function, np.fft.fftshift(). (It is more easier to analyze). Once
you found the frequency transform, you can find the magnitude spectrum.
Fourier Transform in Numpy
Implementation Assignment!!
OCR (Optical Character Recognition)
//End//
Template Matching
Goals:
To find objects in an image using Template Matching
You will see these functions : cv2.matchTemplate(), cv2.minMaxLoc()
Template Matching
Template Matching is a method for searching and finding the location of a template
image in a larger image.
OpenCV comes with a function cv2.matchTemplate() for this purpose.
It simply slides the template image over the input image (as in 2D convolution) and
compares the template and patch of input image under the template image.
Several comparison methods are implemented in OpenCV.
Template Matching
If input image is of size (WxH) and template image is of size (wxh), output image will
have a size of (W-w+1, H-h+1).
Once you got the result, you can use cv2.minMaxLoc() function to find where is the
maximum / minimum value.
Take it as the top-left corner of rectangle and take (w,h) as width and height of the
rectangle. That rectangle is your region of template.
Template Matching
Template Image
Input Image
Template Matching
Template Matching
cv2.TM_CCOEFF cv2.TM_CCOEFF_NORMED
Template Matching
cv2.TM_CCORR cv2.TM_CCORR_NORMED
Template Matching
cv2.TM_SQDIFF cv2.TM_SQDIFF_NORMED
Template Matching
Template Image