Skip to content

Commit e3450e7

Browse files
committed
template matching
1 parent c0437e8 commit e3450e7

15 files changed

+630
-1
lines changed

indexplan.ods

51 Bytes
Binary file not shown.
Loading

source/py_tutorials/py_imgproc/py_canny/images/hysteresis.svg

+171
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. _Canny:
2+
3+
Canny Edge Detection
4+
***********************
5+
6+
Goal
7+
======
8+
9+
In this chapter, we will learn about
10+
11+
* Concept of Canny edge detection
12+
* OpenCV functions for that : **cv2.Canny()**
13+
14+
Theory
15+
=========
16+
17+
Canny Edge Detection is a popular edge detection algorithm. It was developed by John F. Canny in 1986. It is a multi-stage algorithm and we will go through each stages.
18+
19+
1. **Noise Reduction**
20+
21+
Since edge detection is susceptible to noise in the image, first step is to remove the noise in the image with a 5x5 Gaussian filter. We have already seen this in previous chapters.
22+
23+
2. **Finding Intensity Gradient of the Image**
24+
25+
Smoothened image is then filtered with a Sobel kernel in both horizontal and vertical direction to get first derivative in horizontal direction (:math:`G_x`) and vertical direction (:math:`G_y`). From these two images, we can find edge gradient and direction for each pixel as follows:
26+
27+
.. math::
28+
29+
Edge\_Gradient \; (G) = \sqrt{G_x^2 + G_y^2}
30+
31+
Angle \; (\theta) = \tan^{-1} \bigg(\frac{G_y}{G_x}\bigg)
32+
33+
Gradient direction is always perpendicular to edges. It is rounded to one of four angles representing vertical, horizontal and two diagonal directions.
34+
35+
3. **Non-maximum Suppression**
36+
37+
After getting gradient magnitude and direction, a full scan of image is done to remove any unwanted pixels which may not constitute the edge. For this, at every pixel, pixel is checked if it is a local maximum in its neighborhood in the direction of gradient. Check the image below:
38+
39+
.. image:: images/nms.svg
40+
:alt: Non-Maximum Suppression
41+
:align: center
42+
43+
Point A is on the edge ( in vertical direction). Gradient direction is normal to the edge. Point B and C are in gradient directions. So point A is checked with point B and C to see if it forms a local maximum. If so, it is considered for next stage, otherwise, it is suppressed ( put to zero).
44+
45+
In short, the result you get is a binary image with "thin edges".
46+
47+
4. **Hysteresis Thresholding**
48+
49+
This stage decides which are all edges are really edges and which are not. For this, we need two threshold values, `minVal` and `maxVal`. Any edges with intensity gradient more than `maxVal` are sure to be edges and those below `minVal` are sure to be non-edges, so discarded. Those who lie between these two thresholds are classified edges or non-edges based on their connectivity. If they are connected to "sure-edge" pixels, they are considered to be part of edges. Otherwise, they are also discarded. See the image below:
50+
51+
.. image:: images/hysteresis.svg
52+
:alt: Hysteresis Thresholding
53+
:align: center
54+
55+
The edge A is above the `maxVal`, so considered as "sure-edge". Although edge C is below `maxVal`, it is connected to edge A, so that also considered as valid edge and we get that full curve. But edge B, although it is above `minVal` and is in same region as that of edge C, it is not connected to any "sure-edge", so that is discarded. So it is very important that we have to select `minVal` and `maxVal` accordingly to get the correct result.
56+
57+
This stage also removes small pixels noises on the assumption that edges are long lines.
58+
59+
So what we finally get is strong edges in the image.
60+
61+
Canny Edge Detection in OpenCV
62+
===============================
63+
64+
OpenCV puts all the above in single function, **cv2.Canny()**. We will see how to use it. First argument is our input image. Second and third arguments are our `minVal` and `maxVal` respectively. Third argument is `aperture_size`. It is the size of Sobel kernel used for find image gradients. By default it is 3. Last argument is `L2gradient` which specifies the equation for finding gradient magnitude. If it is ``True``, it uses the equation mentioned above which is more accurate, otherwise it uses this function: :math:`Edge\_Gradient \; (G) = |G_x| + |G_y|`. By default, it is ``False``.
65+
::
66+
67+
import cv2
68+
import numpy as np
69+
from matplotlib import pyplot as plt
70+
71+
img = cv2.imread('messi5.jpg',0)
72+
edges = cv2.Canny(img,100,200)
73+
74+
plt.subplot(121),plt.imshow(img,cmap = 'gray')
75+
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
76+
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
77+
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
78+
79+
plt.show()
80+
81+
See the result below:
82+
83+
.. image:: images/canny1.jpg
84+
:alt: Canny Edge Detection
85+
:align: center
86+
87+
Additional Resources
88+
=======================
89+
90+
#. Canny edge detector at `Wikipedia <http://en.wikipedia.org/wiki/Canny_edge_detector>`_
91+
#. `Canny Edge Detection Tutorial <http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/can_tut.html>`_ by Bill Green, 2002.
92+
93+
94+
Exercises
95+
===========
96+
97+
#. Write a small application to find the Canny edge detection whose threshold values can be varied using two trackbars. This way, you can understand the effect of threshold values.

source/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.rst

+31-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ Image Processing in OpenCV
7575
:height: 90pt
7676
:width: 90pt
7777

78+
* :ref:`Canny`
79+
80+
.. tabularcolumns:: m{100pt} m{300pt}
81+
.. cssclass:: toctableopencv
82+
83+
=========== ===================================================================
84+
|imgproc_8| Learn to find edges with Canny Edge Detection
85+
86+
=========== ===================================================================
87+
88+
.. |imgproc_8| image:: images/histogram.jpg
89+
:height: 90pt
90+
:width: 90pt
91+
7892
* :ref:`Table-Of-Content-Contours`
7993

8094
.. tabularcolumns:: m{100pt} m{300pt}
@@ -117,6 +131,20 @@ Image Processing in OpenCV
117131
:height: 90pt
118132
:width: 90pt
119133

134+
* :ref:`Template_Matching`
135+
136+
.. tabularcolumns:: m{100pt} m{300pt}
137+
.. cssclass:: toctableopencv
138+
139+
=========== ===================================================================
140+
|imgproc_9| Learn to search for an object in an image using Template Matching
141+
142+
=========== ===================================================================
143+
144+
.. |imgproc_9| image:: images/histogram.jpg
145+
:height: 90pt
146+
:width: 90pt
147+
120148
.. raw:: latex
121149

122150
\pagebreak
@@ -130,7 +158,9 @@ Image Processing in OpenCV
130158
../py_geometric_transformations/py_geometric_transformations
131159
../py_filtering/py_filtering
132160
../py_gradients/py_gradients
161+
../py_canny/py_canny
133162
../py_contours/py_table_of_contents_contours/py_table_of_contents_contours
134163
../py_histograms/py_table_of_contents_histograms/py_table_of_contents_histograms
135-
../py_transforms/py_table_of_contents_transforms/py_table_of_contents_transforms
164+
../py_transforms/py_table_of_contents_transforms/py_table_of_contents_transforms
165+
../py_template_matching/py_template_matching
136166

Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)