## Thought this was cool: Contours – 2 : Brotherhood

This article is the direct continuation of this article : Contours – 1: Getting Started

__1 – Image Moments__

The function cv2.moments() gives a dictionary of moment values calculated. See below :

moments = cv2.moments(cnt)

If you print moments, you get a dictionary:

{'mu02': 10888082.359906793, 'mu03': 0.005234025965704581, 'm11': 368666693.125,

'nu02': 0.10815497152071127, 'm12': 69763579350.98334, 'mu21': 101313.30416250229, 'mu20': 6674463.831166983,

'nu20': 0.06629968636479547, 'm30': 84692116672.95001, 'nu21': 1.0046975468372928e-05, 'mu11': -1980114.5675549507,

'mu12': -33122544.260385513, 'nu11': -0.019669141689288665, 'nu12': -0.0032846761082870463, 'm02': 352044973.5833333,

'm03': 68983799276.15001, 'm00': 10033.5, 'm01': 1850134.5, 'mu30': 8633090.369003296, 'nu30': 0.0008561209988226333,

'm10': 2010061.8333333333, 'm20': 409360323.5833333, 'm21': 74691021944.88333}

Now you can have calculations using these dictionary keys. For example to find the area of the object:

area = moments['m00']

More we will learn in next article.

__2 – Contour Area:__

area = cv2.contourArea(cnt)

__3 – Contour Perimeter:__

It is also called arc length. It can be found out using cv2.arcLength() function.

perimeter = cv2.arcLength(cnt,True)

__4 – Contour Approximation :__

approx = cv2.approxPolyDP(cnt,0.1*cv2.arcLength(cnt,True),True)

approximation epsilon = 10% of arc length |

**In original contour, there was 210 points, while approximated contour has only four points which corresponds to four corners of rectangle.**

**epsilon**, which is maximum distance from contour to approximated contour. It is an accuracy parameter. In above case, i have taken it as

**10% of arc length.**

approximation epsilon = 1% of arc length |

**1% of arc length**? Check out this left image. Approximation detects the defects also. And number of points in approximated contour is now

**22.**

__5 – Convex Hull :__

convex hull |

Syntax :

hull = cv2.convexHull(points[, hull[, clockwise[, returnPoints]]])

So to get a convex hull as in above image, following is sufficient.

hull = cv2.convexHull(cnt)

**[[[234 202]], [[ 51 202]], [[ 51 79]], [[234 79]]]**, where each value denotes the corners of rectangle, actually coordinates of corners of rectangle.

**returnPoints**, which is by default

**True**. Then it returns the coordinates. But if it is

**False**, it return the indices of those of convex hull points with respect to contours.

For example, execute the following :

hull = cv2.convexHull(cnt,returnPoints = False)

**[[129],[ 67],[ 0],[142]]**. If you check corresponding values in cnt, it will be same as coordinates we have already found. for example,

**cnt[129] = [[234, 202]]**and so others.

__6 – Is contour Convex:__

k = cv2.isContourConvex(cnt)

__7 – Bounding Rectangle :__

**upright bounding rectangle**which covers the full object. It doesn’t consider the rotation of the object.

x,y,w,h = cv2.boundingRect(cnt)

cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)

**Rotated rectangle**where a bounding rectangle is drawn with minimum area, so it considers the rotation also. The function used is cv2.minAreaRect(). It returns a Box2D structure – (x,y),(w,h),theta.

rect = cv2.minAreaRect(cnt)

box = cv2.cv.BoxPoints(rect)

box = np.int0(box)

cv2.drawContours(im,[box],0,(0,0,255),2)

(x,y) – center point of the box

(w,h) – width and height of the box

theta – angle of rotation

Bounding rectangle |

Area of normal bounding rect = 15972

Area of rotated rect = 8853

CircumCircle |

__8 – Minimum Enclosing Circle :__

You can see the result in this image.

(x,y),radius = cv2.minEnclosingCircle(cnt)

center = (int(x),int(y))

radius = int(radius)

cv2.circle(im,center,radius,(0,255,0),2)

__9 – Fit Ellipse :__

Next one is to fit an ellipse to an object. It returns the rotated rectangle in which the ellipse is inscribed.

ellipse = cv2.fitEllipse(cnt)

cv2.ellipse(im,ellipse,(0,255,0),2)

Fit ellipse |

————————————————————————————————————

So, these are some major functions related to Contours.

There are some other functions like, cv2.pointPolygonTest(), cv2.convexityDefects() etc which we will deal in another article.

Hope you like this,

Regards,

ARK

from OpenCV-Python: http://opencvpython.blogspot.com/2012/06/contours-2-brotherhood.html

## 发表评论