~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_contours/py_contours_begin/py_contours_begin.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
Contours : Getting Started {#tutorial_py_contours_begin}
 
2
==========================
 
3
 
 
4
Goal
 
5
----
 
6
 
 
7
-   Understand what contours are.
 
8
-   Learn to find contours, draw contours etc
 
9
-   You will see these functions : **cv2.findContours()**, **cv2.drawContours()**
 
10
 
 
11
What are contours?
 
12
------------------
 
13
 
 
14
Contours can be explained simply as a curve joining all the continuous points (along the boundary),
 
15
having same color or intensity. The contours are a useful tool for shape analysis and object
 
16
detection and recognition.
 
17
 
 
18
-   For better accuracy, use binary images. So before finding contours, apply threshold or canny
 
19
    edge detection.
 
20
-   findContours function modifies the source image. So if you want source image even after
 
21
    finding contours, already store it to some other variables.
 
22
-   In OpenCV, finding contours is like finding white object from black background. So remember,
 
23
    object to be found should be white and background should be black.
 
24
 
 
25
Let's see how to find contours of a binary image:
 
26
@code{.py}
 
27
import numpy as np
 
28
import cv2
 
29
 
 
30
im = cv2.imread('test.jpg')
 
31
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
 
32
ret,thresh = cv2.threshold(imgray,127,255,0)
 
33
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 
34
@endcode
 
35
See, there are three arguments in **cv2.findContours()** function, first one is source image, second
 
36
is contour retrieval mode, third is contour approximation method. And it outputs the contours and
 
37
hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a
 
38
Numpy array of (x,y) coordinates of boundary points of the object.
 
39
 
 
40
@note We will discuss second and third arguments and about hierarchy in details later. Until then,
 
41
the values given to them in code sample will work fine for all images.
 
42
 
 
43
How to draw the contours?
 
44
-------------------------
 
45
 
 
46
To draw the contours, cv2.drawContours function is used. It can also be used to draw any shape
 
47
provided you have its boundary points. Its first argument is source image, second argument is the
 
48
contours which should be passed as a Python list, third argument is index of contours (useful when
 
49
drawing individual contour. To draw all contours, pass -1) and remaining arguments are color,
 
50
thickness etc.
 
51
 
 
52
To draw all the contours in an image:
 
53
@code{.py}
 
54
cv2.drawContours(img, contours, -1, (0,255,0), 3)
 
55
@endcode
 
56
To draw an individual contour, say 4th contour:
 
57
@code{.py}
 
58
cv2.drawContours(img, contours, 3, (0,255,0), 3)
 
59
@endcode
 
60
But most of the time, below method will be useful:
 
61
@code{.py}
 
62
cnt = contours[4]
 
63
cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
 
64
@endcode
 
65
 
 
66
@note Last two methods are same, but when you go forward, you will see last one is more useful.
 
67
 
 
68
Contour Approximation Method
 
69
============================
 
70
 
 
71
This is the third argument in cv2.findContours function. What does it denote actually?
 
72
 
 
73
Above, we told that contours are the boundaries of a shape with same intensity. It stores the (x,y)
 
74
coordinates of the boundary of a shape. But does it store all the coordinates ? That is specified by
 
75
this contour approximation method.
 
76
 
 
77
If you pass cv2.CHAIN_APPROX_NONE, all the boundary points are stored. But actually do we need all
 
78
the points? For eg, you found the contour of a straight line. Do you need all the points on the line
 
79
to represent that line? No, we need just two end points of that line. This is what
 
80
cv2.CHAIN_APPROX_SIMPLE does. It removes all redundant points and compresses the contour, thereby
 
81
saving memory.
 
82
 
 
83
Below image of a rectangle demonstrate this technique. Just draw a circle on all the coordinates in
 
84
the contour array (drawn in blue color). First image shows points I got with cv2.CHAIN_APPROX_NONE
 
85
(734 points) and second image shows the one with cv2.CHAIN_APPROX_SIMPLE (only 4 points). See, how
 
86
much memory it saves!!!
 
87
 
 
88
![image](images/none.jpg)
 
89
 
 
90
Additional Resources
 
91
--------------------
 
92
 
 
93
Exercises
 
94
---------