阅读 554

OpenCV-Python图像轮廓之轮廓特征详解

图像轮廓是指由位于边缘、连续的、具有相同颜色和强度的点构成的曲线,它可以用于形状分析以及对象检测和识别。本文将带大家详细了解一下图像的轮廓特征,感兴趣的可以学习一下

目录
  • 前言

  • 一、轮廓的矩

  • 二、轮廓的面积

  • 三、轮廓的长度

  • 四、轮廓的近似多边形

  • 五、轮廓的凸包

  • 六、轮廓的直边界矩形

  • 七、轮廓的旋转矩形

  • 八、轮廓的最小外包圆

  • 九、轮廓的拟合椭圆

  • 十、轮廓的拟合直线

  • 十一、轮廓的最小外包三角形

前言

图像轮廓是指由位于边缘、连续的、具有相同颜色和强度的点构成的曲线,它可以用于形状分析以及对象检测和识别。

一、轮廓的矩

轮廓的矩包含了轮廓的各种几何特征,如面积、位置、角度、形状等。cv2.moments()函数用于返回轮廓的矩,其基本格式如下:

1
2
3
4
5
ret = cv2.moments(array[, binaryImage])
 
ret为返回的轮廓的矩,是一个字典对象, 大多数矩的含义比较抽象, 但其中的零阶矩(m00)表示轮廓的面积
array为表示轮廓的数组
binaryImage值为True时,会将array对象中的所有非0值设置为1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cv2
import numpy as np
import matplotlib.pyplot as plt
 
img = cv2.imread('shape2.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
 
img1 = cv2.drawContours(img1, contours, -1,(0,255,0),2)
cv2.imshow('Contours',img1)
 
m0 = cv2.moments(contours[0])
m1 = cv2.moments(contours[1])
 
print('轮廓0的矩:', m0)
print('轮廓1的矩:', m1)
 
print('轮廓0的面积:', m0['m00'])
print('轮廓1的面积:', m1['m00'])
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

在这里插入图片描述

二、轮廓的面积

cv2.contourArea()函数用于返回轮廓的面积,其基本格式如下:

1
2
3
4
5
ret = cv2.contourArea(contour[, oriented])
 
ret为返回的面积
contour为轮廓
oriented为可选参数, 其参数值为True时, 返回值的正与负表示表示轮廓是顺时针还是逆时针, 参数值为False(默认值)时, 函数返回值为绝对值
1
2
3
4
5
6
7
8
9
10
11
12
13
img = cv2.imread('shape2.jpg')
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
m0 = cv2.contourArea(contours[0])
m1 = cv2.contourArea(contours[1])
 
print('轮廓0的面积:', m0)
print('轮廓1的面积:', m1)

在这里插入图片描述

三、轮廓的长度

cv2.arcLength()函数用于返回轮廓的长度,其基本格式如下:

1
2
3
4
5
ret = cv2.cv2.arcLength(contour, closed)
 
ret为返回的长度
contour为轮廓
closed为布尔值, 为True时表示轮廓是封闭的
1
2
3
4
5
6
7
8
9
10
11
12
13
img = cv2.imread('shape2.jpg')
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
m0 = cv2.arcLength(contours[0], True)
m1 = cv2.arcLength(contours[1], True)
 
print('轮廓0的长度:', m0)
print('轮廓1的长度:', m1)

在这里插入图片描述

四、轮廓的近似多边形

cv2.approxPolyDP()函数用于返回轮廓的近似多边形,其基本格式如下:

1
2
3
4
5
6
ret = cv2.cv2.arcLength(contour, epsilon, closed)
 
ret为返回的近似多边形
contour为轮廓
epsilon为精度, 表示近似多边形接近轮廓的最大距离
closed为布尔值, 为True时表示轮廓是封闭的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
img = cv2.imread('shape3.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
 
arcl = cv2.arcLength(contours[0], True)
 
img2 = img1.copy()
app = cv2.approxPolyDP(contours[0], arcl*0.05, True)
img2 = cv2.drawContours(img2, [app], -1, (255,0,0), 2)
cv2.imshow('contours',img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

五、轮廓的凸包

cv2.convexHull()函数用于返回轮廓的凸包,其基本格式如下:

1
2
3
4
5
6
hull = cv2.convexHull(contours[, clockwise[, returnPointss]])
 
hull为返回的凸包, 是一个numpy.ndarray对象, 包含了凸包的关键点
contours为轮廓
clockwise为方向标记, 为True时, 凸包为顺时针方向, 为False(默认值)时, 凸包为逆时针方向
returnPointss为True时(默认值)时, 返回的hull中包含的是凸包关键点的坐标, 为False时, 返回的是凸包关键点在轮廓中的索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
img = cv2.imread('shape3.jpg')
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
 
hull = cv2.convexHull(contours[0])
print('returnPoints = Treu 时返回的凸包;\n',hull)
 
hull2 = cv2.convexHull(contours[0], returnPoints=False)
print('returnPoints = False时返回的凸包;\n',hull2)
 
cv2.polylines(img1, [hull], True, (255,0,0),2)
cv2.imshow('ConvecHull',img1)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

在这里插入图片描述

六、轮廓的直边界矩形

轮廓的直边界矩形是指可容纳轮廓的矩形,且矩形的两条边必须是平行的,直边界矩形不一定是面积最小的边界矩形。

cv2.boundingRect()函数用于返回轮廓的直边界矩形,其基本格式如下:

1
2
3
4
ret = cv2.boundingRect(contours)
 
ret为返回的直边界矩形, 它是一个四元组, 其格式为(矩形左上角x坐标, 矩形左上角y坐标, 矩形的宽度, 矩形的高度)
contours为用于计算直边界矩形的轮廓


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
 
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
 
ret = cv2.boundingRect(contours[0])
print('直边界矩形:\n', ret)
 
pt1 = (ret[0], ret[1])
pt2 = (ret[0] + ret[2], ret[1] + ret[3])
img2 = img1.copy()
img2 = cv2.rectangle(img2, pt1, pt2, (255,0,0), 1)
cv2.imshow('Rectangle', img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

在这里插入图片描述

七、轮廓的旋转矩形

轮廓的旋转矩形是指可容纳轮廓的面积最小的矩形。

cv2.minAreaRect()函数用于返回轮廓的旋转矩形,其基本格式如下:

1
2
3
4
box = cv2.minAreaRect(contour)
 
box为返回的旋转矩阵, 它是一个三元组, 其格式为((矩形中心点x坐标, 矩形中心点y坐标), (矩形的宽度, 矩形的高度), 矩形的旋转角度)
contour为用于计算矩形的轮廓

cv2.minAreaRect()函数返回的结果不能直接用于绘制旋转矩形,可以使用cv2.boxPoints()函数将其转换为矩形的顶点坐标,其基本格式如下:

1
2
3
4
points = cv2.boxPoints(box)
 
points为返回的矩形顶点坐标, 坐标数据为浮点数
box为cv2.minAreaRect()函数返回的矩形数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
 
# 计算最小旋转矩形
ret = cv2.minAreaRect(contours[0])
rect = cv2.boxPoints(ret)
rect = np.int0(rect)
 
img2 = img1.copy()
cv2.drawContours(img2, [rect], 0, (255,0,0), 2)
cv2.imshow('Rectangle', img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

八、轮廓的最小外包圆

cv2.minEnclosingCircle()函数用于返回可容纳轮廓的最小外包圆,其基本格式如下:

1
2
3
4
5
center, radius = cv2.minEnclosingCircle(contours)
 
center为圆心
radius为半径
contours为用于计算最小外包圆的轮廓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
 
# 计算最小外包圆
(x, y), radius = cv2.minEnclosingCircle(contours[0])
center = (int(x),int(y))
radius = int(radius)
 
img2 = img1.copy()
cv2.circle(img2, center, radius, (255,0,0),2)
cv2.imshow('Circle',img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

九、轮廓的拟合椭圆

cv2.fitEllipse()函数用于返回轮廓的拟合椭圆,其基本格式如下:

1
2
3
4
ellipse = cv2.fitEllipse(contours)
 
ellipse为返回的椭圆
contours为用于计算拟合椭圆的轮廓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
 
# 计算拟合椭圆
ellipse = cv2.fitEllipse(contours[0])
 
img2 = img1.copy()
cv2.ellipse(img2, ellipse, (255,0,0),2)
cv2.imshow('Circle',img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

十、轮廓的拟合直线

cv2.fitLine()函数用于返回轮廓的拟合直线,其基本格式如下:

1
2
3
4
5
6
7
8
line = cv2.fitLine(contours, distType, param, reps, aeps)
 
line为返回的拟合直线
contours为用于计算拟合直线的轮廓
distType为距离参数类型, 决定如何计算拟合直线
param为距离参数, 与距离参数类型有关, 其设置为0时, 函数将自动选择最优值
reps为计算拟合直线需要的径向精度, 通常设置为0.01
aeps为计算拟合直线需要的轴向精度, 通常设置为0.01

param距离参数类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
img = cv2.imread('shape4.jpg')           
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255     
cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
 
#计算拟合直线
img2 = img1.copy()
rows, cols = img.shape[:2]
[vx, vy, x, y] = cv2.fitLine(contours[0], cv2.DIST_L1, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((cols - x) * vy / vx) + y)
cv2.line(img2, (0, lefty), (cols-1, righty), (255,0,0), 2)  
 
cv2.imshow('FitLine',img2) 
cv2.waitKey(0)
cv2.destroyAllWindows()  

在这里插入图片描述

十一、轮廓的最小外包三角形

cv2.minEnclosingTriangle()函数用于返回可容纳轮廓的最小外包三角形,其基本格式如下:

1
2
3
4
5
retval, triangle = cv2.minEnclosingTriangle(contours)
 
retval为最小外包三角形的面积
triangle为最小外包三角形
contours为用于计算最小外包三角形的轮廓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
 
# 计算最小外包三角形
retval, triangle = cv2.minEnclosingTriangle(contours[0])
triangle = np.int0(triangle)
 
img2 = img1.copy()
cv2.polylines(img2, [triangle], True, (255,0,0),2)
cv2.imshow('Triangle',img2)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

以上就是OpenCV-Python图像轮廓之轮廓特征详解的详细内容

原文链接:https://blog.csdn.net/weixin_43843069/article/details/122039788

伪原创工具 SEO网站优化  https://www.237it.com/ 


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐