OpenCV轮廓特征

1.特征矩

主要使用cv.moments()函数来实现。

cv.moments ( InputArray  array, bool binaryImage = false )   

参数如下:

  • array:输入数组,可以是光栅图像(单通道,8-bit或浮点型二维数组),或者是一个二维数组(1 X N或N X 1),二维数组类型为Point或Point2f
  • binaryImage:默认值是false,如果为true,则所有非零的像素都会按值1对待,也就是说相当于对图像进行了二值化处理,阈值为1,此参数仅对图像有效

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\logo1.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
M = cv.moments(cnt)
print(M)

2.轮廓面积

主要使用cv.contourArea()函数来实现。

cv.contourArea  ( InputArray contour, bool oriented = false )   

参数如下:

  • contour:是一个向量,二维点,可以是vector或Mat类型
  • oriented:有默认值false,面向区域标识符,如果为true,该函数返回一个带符号的面积,其正负取决于轮廓的方向(顺时针还是逆时针)。根据这个特性可以根据面积的符号来确定轮廓的位置。如果是默认值false,则面积以绝对值的形式返回

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\logo1.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
area = cv.contourArea(cnt)
print(area)

3.轮廓周长

主要使用cv.arcLength()函数来实现。

cv.arcLength  (  InputArray  curve, bool  closed )

参数如下:

  • curve:输入二维点集,Mat类型
  • closed:曲线是否封闭的标志位,true则封闭否则不封闭

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\logo1.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
perimeter = cv.arcLength(cnt,True)
print(perimeter)

4.轮廓近似

主要使用cv.approxPolyDP()函数来实现。

cv.approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)

参数如下:

  • InputArray curve:输入曲线
  • OutputArray approxCurve:输出折线
  • double epsilon:判断点到相对应的line segment的距离的阈值(距离大于此阈值则舍弃,小于此阈值则保留,epsilon越小,折线的形状越“接近”曲线)
  • bool closed:曲线是否闭合的标志位

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac6.png')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
approx = cv.approxPolyDP(cnt, 0.001*perimeter, True)
cv.drawContours(img, approx, -1, (255, 0, 0), 10)
cv.imshow("prac", img)
cv.waitKey(0)

5.轮廓凸包

主要使用cv.convexHull()函数来实现。

hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]

参数如下:

  • points:轮廓点集
  • hull:输出凸包
  • clockwise:方向标记。如果为True,则输出凸包为顺时针方向。否则,其方向为逆时针方向
  • returnPoints:默认情况下为True。然后返回凸包的坐标。如果为False,则返回与凸包点相对应的轮廓点的索引

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac6.png')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
hull = cv.convexHull(cnt, returnPoints=False)
print(hull)

6.检查凸度

主要使用cv.isContourConvex()函数来实现。函数具有检查曲线是否凸出的功能,返回True或False。

cv.isContourConvex(points)

参数如下:

  • points:轮廓点集

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac6.png')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
print(cv.isContourConvex(cnt))

7.边界矩形

有两种类型的边界矩形:直角矩形和旋转矩形。

直角矩形

主要使用cv.boundingRect()函数来实现。

cv2.boundingRect(points)

参数如下:

  • points:轮廓点集

函数返回四个值,分别是x,y,w,h;(x,y)是矩形左上点的坐标,w,h是矩形的宽和高。

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x, y, w, h = cv.boundingRect(cnt)
cv.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()

旋转矩形

主要使用cv.minAreaRect()函数来实现。

cv.minAreaRect(points)

参数如下:

  • points:轮廓点集

函数返回一个Box2D结构,包含最小外接矩形的(中心(x,y), (宽,高), 旋转角度),之后再使用cv.boxPoints()函数获取矩形的四个顶点坐标。

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img, [box], 0, (0, 0, 255), 2)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()

8.最小闭合圈

主要使用cv.minEnclosingCircle()函数来实现。

cv.minEnclosingCircle(InputArray points);

参数如下:

  • points:输入的二维点集

函数输出圆的圆心(x, y)以及圆的半径。

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
(x, y), radius = cv.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv.circle(img, center, radius, (0, 255, 0), 2)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()

9.拟合一个椭圆

主要使用cv.fitEllipse()函数来实现。

ellipse = cv.fitEllipse(points)

参数如下:

  • points:输入的二维点集

函数返回值:ellipse = [ (x, y) , (a, b), angle ]
(x, y)代表椭圆中心点的位置
(a, b)代表长短轴长度,应注意a、b为长短轴的直径,而非半径
angle 代表了中心旋转的角度

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
ellipse = cv.fitEllipse(cnt)
cv.ellipse(img, ellipse, (0, 255, 0), 2)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()

10.拟合直线

主要使用cv.fitLine()函数来实现。

cv2.fitLine(InputArray  points, distType, param, reps, aeps)

参数如下:

  • Points: 待拟合的直线的集合,必须是矩阵形式
  • distType:距离类型。这里的距离的类型有以下几种:
参数名称含义
cv2.DIST_USERUser defined distance
cv2.DIST_L1distance =abs(x1-x2) + abs(y1-y2)
cv2.DIST_L2欧式距离,此时与最小二乘法相同
cv2.DIST_Cdistance = max(abs(x1-x2),(y1-y2))
cv2.DIST_L12L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
cv2.DIST_FAIRdistance = c^2(abs(x)/c-log(1+abs(x)/c)), c = 1.3998
cv2.DIST_WELSCHdistance = c2/2(1-exp(-(x/c)2)), c = 2.9846
cv2.DIST_HUBERdistance = abs(x)<c ? x^2/2 : c(abs(x)-c/2), c=1.345
  • param: 距离参数,跟所选的距离类型有关,值可以设置为0。
  • reps, aeps: 第5/6个参数用于表示拟合直线所需要的径向和角度精度,通常情况下两个值均被设定为1e-2.

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
rows, cols = img.shape[:2]
[vx, vy, x, y] = cv.fitLine(cnt, cv.DIST_L2, 0, 0.01, 0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv.line(img, (cols-1, righty), (0, lefty), (0, 255, 0), 2)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值