~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_contour_properties/py_contour_properties.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
Contour Properties {#tutorial_py_contour_properties}
 
2
==================
 
3
 
 
4
Here we will learn to extract some frequently used properties of objects like Solidity, Equivalent
 
5
Diameter, Mask image, Mean Intensity etc. More features can be found at [Matlab regionprops
 
6
documentation](http://www.mathworks.in/help/images/ref/regionprops.html).
 
7
 
 
8
*(NB : Centroid, Area, Perimeter etc also belong to this category, but we have seen it in last
 
9
chapter)*
 
10
 
 
11
1. Aspect Ratio
 
12
---------------
 
13
 
 
14
It is the ratio of width to height of bounding rect of the object.
 
15
 
 
16
\f[Aspect \; Ratio = \frac{Width}{Height}\f]
 
17
@code{.py}
 
18
x,y,w,h = cv2.boundingRect(cnt)
 
19
aspect_ratio = float(w)/h
 
20
@endcode
 
21
 
 
22
2. Extent
 
23
---------
 
24
 
 
25
Extent is the ratio of contour area to bounding rectangle area.
 
26
 
 
27
\f[Extent = \frac{Object \; Area}{Bounding \; Rectangle \; Area}\f]
 
28
@code{.py}
 
29
area = cv2.contourArea(cnt)
 
30
x,y,w,h = cv2.boundingRect(cnt)
 
31
rect_area = w*h
 
32
extent = float(area)/rect_area
 
33
@endcode
 
34
 
 
35
3. Solidity
 
36
-----------
 
37
 
 
38
Solidity is the ratio of contour area to its convex hull area.
 
39
 
 
40
\f[Solidity = \frac{Contour \; Area}{Convex \; Hull \; Area}\f]
 
41
@code{.py}
 
42
area = cv2.contourArea(cnt)
 
43
hull = cv2.convexHull(cnt)
 
44
hull_area = cv2.contourArea(hull)
 
45
solidity = float(area)/hull_area
 
46
@endcode
 
47
 
 
48
4. Equivalent Diameter
 
49
----------------------
 
50
 
 
51
Equivalent Diameter is the diameter of the circle whose area is same as the contour area.
 
52
 
 
53
\f[Equivalent \; Diameter = \sqrt{\frac{4 \times Contour \; Area}{\pi}}\f]
 
54
@code{.py}
 
55
area = cv2.contourArea(cnt)
 
56
equi_diameter = np.sqrt(4*area/np.pi)
 
57
@endcode
 
58
 
 
59
5. Orientation
 
60
--------------
 
61
 
 
62
Orientation is the angle at which object is directed. Following method also gives the Major Axis and
 
63
Minor Axis lengths.
 
64
@code{.py}
 
65
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
 
66
@endcode
 
67
 
 
68
6. Mask and Pixel Points
 
69
------------------------
 
70
 
 
71
In some cases, we may need all the points which comprises that object. It can be done as follows:
 
72
@code{.py}
 
73
mask = np.zeros(imgray.shape,np.uint8)
 
74
cv2.drawContours(mask,[cnt],0,255,-1)
 
75
pixelpoints = np.transpose(np.nonzero(mask))
 
76
#pixelpoints = cv2.findNonZero(mask)
 
77
@endcode
 
78
Here, two methods, one using Numpy functions, next one using OpenCV function (last commented line)
 
79
are given to do the same. Results are also same, but with a slight difference. Numpy gives
 
80
coordinates in **(row, column)** format, while OpenCV gives coordinates in **(x,y)** format. So
 
81
basically the answers will be interchanged. Note that, **row = x** and **column = y**.
 
82
 
 
83
7. Maximum Value, Minimum Value and their locations
 
84
---------------------------------------------------
 
85
 
 
86
We can find these parameters using a mask image.
 
87
@code{.py}
 
88
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
 
89
@endcode
 
90
 
 
91
8. Mean Color or Mean Intensity
 
92
-------------------------------
 
93
 
 
94
Here, we can find the average color of an object. Or it can be average intensity of the object in
 
95
grayscale mode. We again use the same mask to do it.
 
96
@code{.py}
 
97
mean_val = cv2.mean(im,mask = mask)
 
98
@endcode
 
99
 
 
100
9. Extreme Points
 
101
-----------------
 
102
 
 
103
Extreme Points means topmost, bottommost, rightmost and leftmost points of the object.
 
104
@code{.py}
 
105
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
 
106
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
 
107
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
 
108
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
 
109
@endcode
 
110
For eg, if I apply it to an Indian map, I get the following result :
 
111
 
 
112
![image](images/extremepoints.jpg)
 
113
 
 
114
Additional Resources
 
115
--------------------
 
116
 
 
117
Exercises
 
118
---------
 
119
 
 
120
-#  There are still some features left in matlab regionprops doc. Try to implement them.