# opencv 9 -- 轮廓 性质

## 1 长宽比

```x,y,w,h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h```

## 2 Extent

```area = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area```

## 3 Solidity

```area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area)/hull_area```

## 4 Equivalent Diameter

```area = cv2.contourArea(cnt)
equi_diameter = np.sqrt(4*area/np.pi)```

## 5 方向

`(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)`

## 6 掩模和像素点

```mask = np.zeros(imgray.shape,np.uint8)
# 这里一定要使用参数-1, 绘制填充的的轮廓
//方法一 bumpy方法
//方法二 opencv方法

## 7 最大值和最小值及它们的位置

`min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)`

## 8 平均颜色及平均灰度

`mean_val = cv2.mean(im,mask = mask)`

## 9 极点

```leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])```

189 篇文章29 人订阅

0 条评论