内容纲要
  • cv2.moments() 矩
  • cv2.contourArea() 面积
  • cv2.arcLength() 周长
  • cv2.approxPolyDP() 近似
  • cv2.convexHull() 凸包
  • cv2.isContourConvex() 是否凸包
  • cv2.minAreaRect() 最小矩形面积,可能变换
  • cv2.cv.BoxPoints() 矩形中心点
  • cv2.minEnclosingCircle() 最小包围圆
  • cv2.fitEllipse() 椭圆拟合

moments = cv2.moments(cnt)
{'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}

面积

area = moments['m00']

面积

area = cv2.contourArea(cnt)

周长

perimeter = cv2.arcLength(cnt,True)

近似


approximation
epsilon = 10% of arc length


approximation
epsilon = 1% of arc length

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

凸包

hull = cv2.convexHull(cnt)

是否凸包

k = cv2.isContourConvex(cnt)

矩形包围

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

最小矩形包围

rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect)
box = np.int0(box)
cv2.drawContours(im,[box],0,(0,0,255),2)

最小包围圆

(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(im,center,radius,(0,255,0),2)

拟合椭圆

ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(im,ellipse,(0,255,0),2)

发表评论

电子邮件地址不会被公开。 必填项已用*标注