6. 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.
7. Template matching
Here, as an example, we will search for Messi’s face in his photo.
We will try all the comparison methods
10. Template matching with multiple objects Example
Suppose you are searching for an object which has multiple occurrences, so,we
will use thresholding.
12. Hough Line Transform
Hough Transform is a popular technique to detect any shape, if you can
represent that shape in mathematical form. It can detect the shape even if it is
broken or distorted a little bit.
13. Hough Line Transform
Any line can be represented in these two terms, (⍴, ). to detect a line we use the
OpenCV function, cv2.HoughLines(). It simply returns an array of (⍴, ).
Inputs
1. Input image should be a binary image, so apply threshold or use canny edge
detection before finding applying hough transform.
2. ⍴ and accuracies respectively.
3. threshold, which means minimum vote it should get for it to be considered as a line.
14. number of votes depend upon number of points on the line. So it
represents the minimum length of line that should be detected.
16. Probabilistic Hough Transform
Probabilistic Hough Transform is an optimization of Hough Transform, It
doesn’t take all the points into consideration, instead take only a random
subset of points and that is sufficient for line detection. Just we have to
decrease the threshold.
OpenCV implementation is based on Robust Detection of Lines Using the
Progressive Probabilistic Hough Transform. The function used is
cv2.HoughLinesP(). It has two new arguments:
● minLineLength - Minimum length of line. Line segments shorter than
this are rejected.
● maxLineGap - Maximum allowed gap between line segments to treat
them as single line.
18. Hough Circle Transform
So OpenCV uses Hough Gradient Method which uses the gradient information
of edges. The function we use here is cv2.HoughCircles().
20. Image Segmentation
Any grayscale image can be viewed as a topographic surface where high intensity
denotes peaks and hills while low intensity denotes valleys.
21. Watershed Algorithm
OpenCV implemented a marker-based watershed
algorithm where you specify which are all valley
points are to be merged and which are not. It is an
interactive image segmentation.
Consider the coins image below, the coins are
touching each other. Even if you threshold it, it will
be touching each other.
22. Watershed Algorithm
1. We start with finding an approximate estimate of the coins. For that, we can
use the Otsu’s binarization.
Result
23. Watershed Algorithm
2. Now we need to remove any small white noises in the image. For that we can
use morphological opening. To remove any small holes in the object, we can
use morphological closing.
3. We need to extract the area which we are sure they are coins. Erosion
removes the boundary pixels. So whatever remaining, we can be sure it is
coin.
4. Next we need to find the area which we are sure they are not coins. For that,
we dilate the result. Dilation increases object boundary to background.
25. Watershed Algorithm
The remaining regions are those which we don’t
have any idea, whether it is coins or background.
Watershed algorithm should find it. We call it
border. It can be obtained from subtracting sure_fg
area from sure_bg area.
27. Watershed Algorithm
Now our marker is ready. It is time for final step, apply watershed. Then marker image will be modified.
The boundary region will be marked with -1.
30. Understanding Features
We are looking for specific patterns or specific
features which are unique, which can be easily
tracked, which can be easily compared.
A and B are flat surfaces, and they are spread in a lot
of area. It is difficult to find the exact location of
these patches.
C and D are much more simpler. They are edges of
the building. You can find an approximate location,
but exact location is still difficult. It is because,
along the edge, it is same everywhere. Normal to the
edge, it is different. So edge is a much better feature
compared to flat area, but not good enough.
31. Understanding Features
Finally, E and F are some corners of the building.
And they can be easily found out. Because at
corners, wherever you move this patch, it will look
different. So they can be considered as a good
feature.
33. Harris Corner Detector
Theory
It basically finds the difference in intensity for a displacement of (u,v) in all directions.
We have to maximize this function E(u,v) for corner detection. That means, we have to maximize the
second term. So, applying Taylor Expansion to above equation and using some mathematical steps
where
34. Harris Corner Detector
I͓ and are image derivatives in x and y directions respectively. (Can be easily found out using
cv2.Sobel()).
This equation will determine if a window can contain a corner or not.
36. Harris Corner Detector in OpenCV
OpenCV has the function cv2.cornerHarris() for this purpose. Its arguments are :
● img - Input image, it should be grayscale and float32 type.
● blockSize - It is the size of neighbourhood considered for corner detection
● ksize - Aperture parameter of Sobel derivative used.
● k - Harris detector free parameter in the equation.
dst = cv2.cornerHarris(gray,2,3,0.04)
41. Assignment 3
● Description: Implement and Compare with OpenCV functions on the same
image
a. Hough Line and Circle Transform
b. Harris Corner Detector
c. JPEG Image Compression
● Notes:
a. Don't use any opencv function for processing just use it for loading and viewing results
■ Ask Ahmed Nasser Qassem
● Points: 5 points
● Team: 2 members
● Deliverables:
a. Jupyter (html) notebook to https://goo.gl/forms/lroYhjLHcID6lHB83
● Deadline: 31th March 2018
44. Project
● Description: Solve any complex problem from your real life using computer
vision.
○ Choose your own idea ( DON’T use a project from the internet- > Zero MARKS)
○ Capture or download images to work on them
○ Implement different algorithms just to proof your concept
○ Build the best algorithm on a mobile application (Java or C++ or Python).
● Team: 4 members
● Deliverables Milestones:
○ Milestone 1: Project proposal (Idea description, your plan , image samples, references)
■ Deadline: 27th March 2018
○ Milestone 2: Desktop Script
■ Deadline: 15th April 2018
○ Milestone 3: Mobile application
■ Deadline: 27th April 2018
● Points: 15 points
46. If you have any question
● Facebook
○ You can ask me through Facebook but don’t wait for any response before 1 month.
“Simply don’t use facebook”
■ Because I hate facebook
■ I hate talking to people for (work-study) on facebook
■ I use facebook to escape from real world so I hate facebook
● Office Hours
○ Monday 3:30 to 5:00
○ Wednesday 11:30 to 12:30
● Email (Prefered)
○ aly.osama@eng.asu.edu.eg