Image Transforms
not to be confused with Transformers…
Maya / Lucas
Lecture goal:
• What does Transform refer to in Computer Vision?
• What is it used for?
• How does it work?
2
Distance – basic idea
3
Distance – basic idea
4
Distance - function
Input: A Binary image
Output: A grayscale image like the input image, except that the pixel
intensities of foreground region are changed to show the distance to
the closest boundary from each point.
5
Distance – Visual Example
Considering Black pixels as feature pixels and White pixels as non-feature pixels:
?
6
Distance – Visual Example
Considering Black pixels as feature pixels and White pixels as non-feature pixels:
7
Exercise
Find the number of coins in the binary segmented (:-/) image, using OpenCV distance transform
Tip: You may need help of SciPy and Skimage to achieve the final requested results
You need to return an integer with the total number of coins in the image
8
Hough Transform
• The Hough transform is a common approach to finding parameterized line,
circle, or almost any geometric shape segments in an image.
9
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
10
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one …
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
11
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
12
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
• In the Hough transform, each point votes for
every line it could be on
• The lines with the most votes win
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
13
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
• In the Hough transform, each point votes for
every line it could be on
• The lines with the most votes win
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
14
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
• In the Hough transform, each point votes for
every line it could be on
• The lines with the most votes win
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
15
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
• In the Hough transform, each point votes for
every line it could be on
• The lines with the most votes win
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
16
Hough – Introduction
• The Hough transform is a common approach to
finding parameterized line segments
• Let us assume we want to detect this diagonal line
• This line is composed of infinite points, and from
each point like this one
• Infinite number of lines can pass
• In the Hough transform, each point votes for
every line it could be on
• The lines with the most votes win
Now we see how the point voting procedure happens
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
17
Hough – Line Representation
Y = a*x + b
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms, [Link] and [Link]
18
Hough – Polar Coordinates
Agreed origin
• Any line can be represented by two numbers
• Here we will represent the yellow line by (w) w
• In other words we define it using a perpendicular
line from an agreed origin
• of length w
• at angle to the horizon
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
19
Hough – Hough Space w
• Since we can use (w,) to represent any line in
the image space
• We can represent any line in the image space as
a point in the plane defined by (w,)
=
• This is called Hough space
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
20
w=0 w=100
Hough – Hough Space
w = x cos( ) + y sin( )
=
=
w=0 w=100
why using the polar representation of lines?
21 [Link]
Hough – Hough Space – 1 point
Image space Hough space
=
• One point in image space corresponds to
a sinusoidal curve in Hough space
• Two points correspond to two curves in
Hough space
• The intersection of those two curves has =
“two votes”. =
• This intersection represents the straight
line in image space that passes through
both points
Figures borrowed from Jeremy Wyatt public presentation on Hough Transforms
=
Hough – Simple Examples
11x11 image and its Hough transform:
23
Hough –Examples
24
Hough – Circle transform
• A line in xy-plane is given by 2 parameter space
y = ax + b
• A circle in the xy-plane is given by 3 parameter space
(x - a)² + (y - b)² = c²
• Circle Hough transform is similar to line Hough transform, only in 3D space.
25
Hough – Circle transform
• A line in xy-plane is given by 2 parameter space
y = ax + b
• A circle in the xy-plane is given by 3 parameter space
(x - a)² + (y - b)² = c²
• Circle Hough transform is similar to line Hough transform, only in 3D space.
26
Exercise
Second exercise: Find the number of detected coins, using OpenCV hough transform
27
Exercise
Third exercise: Compute Hough Space – hough_space = (distance,angles)
28
Exercise – Implementation Details
• Pseudo-code
• Define the hough space accumulator
• Loop through the image coords:
• Is it edge point?
• Loop through the theta/angle values:
• Convert to radians
• Compute the distance from origin using the equation (watch out for x and y)
• Add value to prevent negative values
• Increment the hough space position
• Return hough space accumulator
29
Exercise – Implementation Details
= How do I define the maximum size of the Hough Space?
Im_h=100
Hyp = sqrt(100²+100²) = 140
-100 Im_w=100 Hyp = sqrt(400²+400²) = 565
=
w=0 w=100+100
Value = Hyp + value
30
Exercise – Implementation Details
• Testing your implementation:
• Load image
• Compute the Hough Space
• Find the maximum points
• Plot lines on top of the images
• You got 5 lines? You can move on
31
Fourier Transform
“All waveforms, no matter what you scribble or
observe in the universe, are actually just the sum
of simple sinusoids of different frequencies.”
32
Fourier Transform
“All waveforms, no matter what you scribble or
observe in the universe, are actually just the sum
of simple sinusoids of different frequencies.”
33 [Link]
Fourier Transform
“All waveforms, no matter what you scribble or
observe in the universe, are actually just the sum
of simple sinusoids of different frequencies.”
34
Fourier Transform
35
Fourier Transform
2 frequencies:
• of signal -> 3 times per second
• Of the graph around the circle -> 0.5 per second This we can change
36
Fourier Transform
Matching frequency
Low frequencies on the left
37
Fourier Transform
38
Fourier Transform
Peak at 3, what else is also 3 in the current signal?
39
Fourier Transform - Images
• Cosine function as an image
Y
• FT tries to represent all images
as a summation of cosine-like
images X
40
Fourier Transform - Images
• FT always treats an image as if it
were part of a periodically replicated
array of identical images extending
horizontally and vertically to infinity
41
Fourier Transform - Images
• FT always treats an image as if it
were part of a periodically replicated
array of identical images extending
horizontally and vertically to infinity
42
Fourier Transform
• Bricks: vertical lines = high horizontal frequency
• Blocks: edges = perpendicular frequency lines
• Center of Fourier: Low frequency (brightness)
• Edges of Fourier: High frequency (edges, texture)
43
Fourier Transform - Applications
• Low Pass Filter (LPF) = Filter high
frequency “noise” components
44 This is quite extreme, can we make it less?
Fourier Transform - Applications
• High Pass Filter (HPF) = Filter Low
frequency components
45
Fourier Transform - Applications
• High Pass Filter (HPF) = Filter Low
frequency components
46
Exercise
Third exercise: Remove the grid background noise using the Fast Fourier Transform
47
Exercise
Third exercise: Remove the grid background noise using the Fast Fourier Transform
48
Exercise – Implementation Details
• The code given already contains a start, you need to:
• Apply the Fourier transform to the input image;
• Shift the non-zero frequency components of the kernel to the center;
• Apply the Fourier transform to the kernel so it is also in the frequency
domain;
• Multiple the image with the kernel.
• Apply the inverse Fourier transform to bring the result back to the spatial
domain.
49
Wavelets
• Fourier = Frequency
• Wavelets = Frequency/Temporal
▪ Compression
▪ Denoising
▪ Convolutions
50
Wavelets – 1D Example
51 [Link]
Wavelets – 1D Example
52
The Haar Wavelet
1910 - Alfréd Haar
Fast to compute (sparse = zero elements)
Input and output of same size
Lossy compression
Can be used to analyze features of signals
53
Haar Cascade – Viola Jones
Popular algorithm for detecting faces on images:
1. Feature Extractor
2. Weak cascade of classifiers to select the most relevant
54
Haar Cascade – Viola Jones
Popular algorithm for detecting faces on images:
1. Feature Extractor
2. Weak cascade of classifiers to select the most relevant
55
Haar Cascade – Feature Extractor
• Define feature extractors:
• Subtract regions from each other
• Compute image integral:
• Saves computacional time
56
Haar Cascade – Feature Extractor
• Define feature extractors:
• Subtract regions from each other
• Compute image integral:
• Saves computacional time
57
Haar Cascade – Cascade of classifiers
• Compute the features for all regions
• Select the promissing “face” ones:
• Using a cascade of classifiers
• Each classifier is responsible for a certain complexity
58
Exercise
Fourth exercise: Detect Faces using the Haar Transform
59
Exercise – Implementation Details
• The code given already contains a start, you need to:
• Detect the faces using multiscale function;
• Loop through the faces to find the eyes.
60
Updated Resources
• Solving image problems with Python libraries:
• Face Detection using Haar Cascades
• Hough Circle Transform
• Image Segmentation with Distance Transform and Watershed Algorithm
• Fourier Transform
61
Where can I find the images?
• Check the tests from the notebooks.
62
We meet at 16h, to discuss your solutions.
You are expected to solve “all” exercises.
63