【OpenCV 例程200篇】226. 区域特征之紧致度/圆度/偏心率

『youcans 的 OpenCV 例程200篇 - 总目录』


【youcans 的 OpenCV 例程200篇】226. 区域特征之紧致度/圆度/偏心率


特征通常是针对于图像中的某个目标而言的。

我们把感兴趣的人或物称为目标,目标所处的区域就是目标区域。图像分割之后,还要对目标区域进行适当的表示和描述,以便下一步处理。


4.1 基本区域特征描述子

针对目标所在区域的特征描述符(Region descriptors),称为区域特征描述子。
例如:

  • 紧致度(compactness),周长的平方与面积之比,具有平移、尺度、旋转不变性
  • 圆度(circularity),面积与周长的平方之比,具有平移、尺度、旋转不变性
  • 偏心率(Eccentricity),椭圆的偏心率定义为焦距与椭圆长轴的长度之比
  • 拓扑描述子,欧拉数 E 是目标区域的连通分量的数量 C 与孔洞数量 H 的差: E = C − H E=C-H E=CH
  • 纹理,纹理提供了诸如平滑度、粗糙度和规定性等的测度,可以用统计方法和谱方法描述
  • 不变矩,由二阶和三阶归一化中心距可以推导出 7 个二维矩不变量,对于平移、缩放、镜像和旋转是不变的。

区域的面积 A 定义为该区域中的像素数量,区域的周长 p 是区域边界的长度。使用面积和周长作为特征描述子时,要进行归一化处理后才有意义。

(1)紧致度(Compactness),周长的平方与面积之比,具有平移、尺度、旋转不变性。

c o m p a c t n e s s = p 2 / A compactness = {p^2} / {A} compactness=p2/A

紧致度是一个无量纲的测度,圆的紧致度最小,为 4 π 4\pi 4π,正方形的紧致度 是 16。

(2)圆度(Circularity),面积与周长的平方之比,具有平移、尺度、旋转不变性

c i r c u l a r i t y = 4 π A / p 2 circularity = {4 \pi A} / {p^2} circularity=4πA/p2

圆度也是一个无量纲的测度,圆的圆度为 1 最大,正方形的圆度为 π / 4 \pi / 4 π/4

(3)偏心率(Eccentricity),椭圆的偏心率定义为焦距与椭圆长轴的长度之比
e c c e n t r i c i t y = 2 c 2 a = a 2 − b 2 a = 1 − ( b / a ) 2 , a > b eccentricity= \frac{2c}{2a} = \frac{\sqrt{a^2-b^2}}{a} = \sqrt{1-(b/a)^2}, \quad a \gt b eccentricity=2a2c=aa2b2 =1(b/a)2 ,a>b
偏心率的值域范围为 [0,1],圆的偏心率为 0 最小,直线的偏心率为 1最大。

通常将不同的特征描述子构造为一组特征向量,容易在特征空间中进行分类和识别。


例程 14.6:区域特征描述之紧致度,圆度和偏心率

    #  14.6 区域特征描述子之紧致度,圆度和偏心率
    plt.figure(figsize=(9, 6))
    n = 4
    for i in range(1,n+1):
        path = "../images/wingding{}.tif".format(str(i))
        gray = cv2.imread(path, flags=0)
        _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)  # OpenCV4~
        cnts = sorted(contours, key=cv2.contourArea, reverse=True)  # 所有轮廓按面积排序
        cnt = cnts[0]  # 第 0 个轮廓,面积最大的轮廓,(2867, 1, 2)
        cntPoints = np.squeeze(cnt)  # 删除维度为 1 的数组维度,(2867, 1, 2)->(2867,2)

        print("{}, path:{}, shape:{}".format(i, path, gray.shape))
        area = cv2.contourArea(cnt)  # 轮廓面积 (area)
        print("\tarea of contour: ", area)
        perimeter = cv2.arcLength(cnt, True)  # 轮廓周长 (perimeter)
        print("\tperimeter of contour: {:.1f}".format(perimeter))
        compact = perimeter ** 2 / area  # 轮廓的紧致度 (compactness)
        print("\tcompactness of contour: {:.2f}".format(compact))
        circular = 4 * np.pi * area / perimeter ** 2  # 轮廓的圆度 (circularity)
        print("\tcircularity of contour: {:.2f}".format(circular))
        ellipse = cv2.fitEllipse(cnt)  # 轮廓的拟合椭圆
        # 椭圆中心点 (x,y), 长轴短轴长度 (a,b), 旋转角度 ang
        (x, y), (a, b), ang = np.int32(ellipse[0]), np.int32(ellipse[1]), round(ellipse[2], 1)
        # print("Fitted ellipse: (Cx,Cy)={}, (a,b)={}, ang={})".format((x, y), (a, b), ang))
        # 轮廓的偏心率 (eccentricity)
        if (a > b):
            eccentric = np.sqrt(1.0 - (b / a) ** 2)  # a 为长轴
        else:
            eccentric = np.sqrt(1.0 - (a / b) ** 2)
        print("\teccentricity of contour: {:.2f}".format(eccentric))

        imgCnt = np.ones(gray.shape, np.uint8) * 255  # 创建空白图像
        cv2.drawContours(imgCnt, cnt, -1, 0, 2)  # 绘制轮廓
        cv2.rectangle(imgCnt, (2, 3), (452, 452), 0, 1)  # 绘制边框

        notes = np.ones(gray.shape, np.uint8)  # 创建空白图像
        text1 = "Compactness: {:.2f}".format(compact)
        text2 = "Circularity: {:.2f}".format(circular)
        text3 = "Eccentricity: {:.2f}".format(eccentric)
        imgNotes = cv2.putText(notes, text1, (10,50), cv2.FONT_HERSHEY_PLAIN, 2.5, 0)
        imgNotes = cv2.putText(notes, text2, (10,100), cv2.FONT_HERSHEY_PLAIN, 2.5, 0)
        imgNotes = cv2.putText(notes, text3, (10,150), cv2.FONT_HERSHEY_PLAIN, 2.5, 0)

        plt.subplot(3,n,i), plt.axis('off'), plt.title("Figure_{}".format(i))
        plt.imshow(gray, cmap='gray')
        plt.subplot(3,n,n+i), plt.axis('off'), plt.imshow(imgCnt, cmap='gray')
        plt.subplot(3,n,2*n+i), plt.axis('off'), plt.imshow(imgNotes, cmap='gray')

    plt.tight_layout()
    plt.show()

运行结果:

在这里插入图片描述

1, path:../images/wingding1.tif, shape:(454, 454)
	area of contour:  45497.0
	perimeter of contour: 796.0
	compactness of contour: 13.93
	circularity of contour: 0.90
	eccentricity of contour: 0.00
2, path:../images/wingding2.tif, shape:(454, 454)
	area of contour:  37136.0
	perimeter of contour: 1392.6
	compactness of contour: 52.22
	circularity of contour: 0.24
	eccentricity of contour: 0.00
3, path:../images/wingding3.tif, shape:(454, 454)
	area of contour:  57600.0
	perimeter of contour: 960.0
	compactness of contour: 16.00
	circularity of contour: 0.79
	eccentricity of contour: 0.00
4, path:../images/wingding4.tif, shape:(454, 454)
	area of contour:  35197.0
	perimeter of contour: 812.9
	compactness of contour: 18.77
	circularity of contour: 0.67
	eccentricity of contour: 0.73

【本节完】

版权声明:
youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125652989)
Copyright 2022 youcans, XUPT
Crated:2022-7-7

222. 特征提取之弗里曼链码
225. 特征提取之傅里叶描述子

猜你喜欢

转载自blog.csdn.net/youcans/article/details/125652989