1
Contour Properties {#tutorial_py_contour_properties}
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).
8
*(NB : Centroid, Area, Perimeter etc also belong to this category, but we have seen it in last
14
It is the ratio of width to height of bounding rect of the object.
16
\f[Aspect \; Ratio = \frac{Width}{Height}\f]
18
x,y,w,h = cv2.boundingRect(cnt)
19
aspect_ratio = float(w)/h
25
Extent is the ratio of contour area to bounding rectangle area.
27
\f[Extent = \frac{Object \; Area}{Bounding \; Rectangle \; Area}\f]
29
area = cv2.contourArea(cnt)
30
x,y,w,h = cv2.boundingRect(cnt)
32
extent = float(area)/rect_area
38
Solidity is the ratio of contour area to its convex hull area.
40
\f[Solidity = \frac{Contour \; Area}{Convex \; Hull \; Area}\f]
42
area = cv2.contourArea(cnt)
43
hull = cv2.convexHull(cnt)
44
hull_area = cv2.contourArea(hull)
45
solidity = float(area)/hull_area
48
4. Equivalent Diameter
49
----------------------
51
Equivalent Diameter is the diameter of the circle whose area is same as the contour area.
53
\f[Equivalent \; Diameter = \sqrt{\frac{4 \times Contour \; Area}{\pi}}\f]
55
area = cv2.contourArea(cnt)
56
equi_diameter = np.sqrt(4*area/np.pi)
62
Orientation is the angle at which object is directed. Following method also gives the Major Axis and
65
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
68
6. Mask and Pixel Points
69
------------------------
71
In some cases, we may need all the points which comprises that object. It can be done as follows:
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)
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**.
83
7. Maximum Value, Minimum Value and their locations
84
---------------------------------------------------
86
We can find these parameters using a mask image.
88
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
91
8. Mean Color or Mean Intensity
92
-------------------------------
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.
97
mean_val = cv2.mean(im,mask = mask)
103
Extreme Points means topmost, bottommost, rightmost and leftmost points of the object.
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])
110
For eg, if I apply it to an Indian map, I get the following result :
112
![image](images/extremepoints.jpg)
120
-# There are still some features left in matlab regionprops doc. Try to implement them.