~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/doc/py_tutorials/py_imgproc/py_canny/py_canny.markdown

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

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