opencv-python 4.9.3. 轮廓属性

发布时间 2023-04-03 10:42:47作者: 一枚码农

1. Aspect Ratio(长宽比)

它是对象的边界矩形的宽度与高度的比。
$$ Aspect\ Ratio= \frac{Width}{Height} $$

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

2. Extent(大小比)

它是轮廓区域与边界矩形区域的比。

$$ Extent= \frac{Object\ Area}{Bounding\ Rectangle\ Area} $$

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

3. Solidity(密实比)

Solidity是轮廓区域与其凸包区域的比率。

$$ Solidity= \frac{Contour\ Area}{Convex\ Hull\ Area} $$

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

4. Equivalent Diameter(等效直径)

等效直径是圆的直径,其面积与轮廓面积相同。

$$ Equivalent\ Diameter=\sqrt{\frac{4\times Contour\ Area}{\pi }} $$

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

5. Orientation(方向)

方向是对象定向的角度。以下方法还给出了主轴和短轴长度。

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

6. Mask & Pixel Points(掩模和像素点)

在某些情况下,我们可能需要包含该对象的所有点。它可以如下完成:

mask = np.zeros(imgray.shape,np.uint8)
cv.drawContours(mask,[cnt],0,255,-1)
pixelpoints = np.transpose(np.nonzero(mask))
#pixelpoints = cv.findNonZero(mask)

这里,两个方法,一个使用Numpy函数,另一个使用OpenCV函数(最后一个注释行)给出相同的方法。 结果也相同,但略有不同。 Numpy以(行,列)格式给出坐标,而OpenCV以(x,y)格式给出坐标。所以答案基本上会互换。请注意,row=y和column=x。

7. 最大值,最小值及其位置

我们可以使用掩模图像找到这些参数。

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

8. 平均颜色或平均灰度

在这里,我们可以找到对象的平均颜色。或者它可以是灰度模式下物体的平均强度。我们再次使用相同的掩膜来做到这一点。

mean_val = cv.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])

以上完整代码

点击查看代码
import cv2 as cv
import numpy as np

img = cv.imread(r'C:\Users\yuyalong\Pictures\Saved Pictures\rectangle2.jpg')

imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)

# 获取轮廓点
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
print(99, len(contours))

# 找到最内层的元素, 这里是2是因为图像总共有三层
cnt = contours[1]

# 计算长宽比
x, y, w, h = cv.boundingRect(cnt)
aspect_ratio = float(w) / h
print(111, x, y, w, h, aspect_ratio)

# 计算大小比
area = cv.contourArea(cnt)
x, y, w, h = cv.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print(222, extent)

# 计算密实比
area = cv.contourArea(cnt)
hull = cv.convexHull(cnt)
hull_area = cv.contourArea(hull)
solidity = float(area) / hull_area
print(333, solidity)

# 计算等效直径
area = cv.contourArea(cnt)
equi_diameter = np.sqrt(4 * area / np.pi)
print(444, equi_diameter)

# 计算方向
(x, y), (MA, ma), angle = cv.fitEllipse(cnt)
print(555, x, y, MA, ma, angle)

# 计算掩模和像素点
mask = np.zeros(imgray.shape, np.uint8)
cv.drawContours(mask, [cnt], 0, 255, -1)
pixelpoints = np.transpose(np.nonzero(mask))
# pixelpoints1 = cv.findNonZero(mask)
print(666, pixelpoints)

# 计算最大值,最小值及其位置
mask = np.zeros(imgray.shape, np.uint8)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(imgray, mask=mask)
print(777, min_val, max_val, min_loc, max_loc)

# 计算平均颜色或平均灰度
mask = np.zeros(imgray.shape, np.uint8)
mean_val = cv.mean(img, mask=mask)
print(888, mean_val)

# 计算极点
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])
print(999, leftmost, rightmost, topmost, bottommost)
# 绘制极点
cv.circle(img, leftmost, 5, (255, 0, 0), -1)
cv.circle(img, rightmost, 5, (0, 255, 0), -1)
cv.circle(img, topmost, 5, (0, 0, 255), -1)
cv.circle(img, bottommost, 5, (255, 255, 0), -1)

cv.imshow('img', img)
cv.waitKey(0)

image