目录

1.cv2.boundingRect()

2.cv2.minAreaRect()

3. cv2.minEnclosingCircle()

4.cv2.fitEllipse()

 5.cv2.fitLine()


  在计算轮廓时,可能并不需要实际的轮廓,而仅需要一个接近于轮廓的近似多边形。OpenCV提供了多种计算轮廓近似多边形的方法。

1.cv2.boundingRect()

能够返回包围轮廓的矩形的边界信息。

函数样式:retval = cv2.boundingRect( array)

参数介绍:

  retval 表示返回矩形边界左上角顶点的坐标值及矩形边界的宽和高 , 也可以是4个返回值形式 

         x , y ,w ,h  = cv2.boundingRect( array)

    array 是灰度图像或轮廓

然后使用函数cv2.drawContours()来绘制矩形包围框。

代码:

import cv2
import numpy as np
o = cv2.imread("cc.bmp")
cv2.imshow("original" , o)
gray = cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)
ret , binary = cv2.threshold(gray , 127 , 255 ,cv2.THRESH_BINARY)
contours , hierarchy = cv2.findContours(binary , cv2.RETR_LIST ,
        cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])
brcnt = np.array([[[x,y]] ,[[x+w , y]] , [[x+w , y+h]] , [[x,y+h]]] )
cv2.drawContours(o , [brcnt] , -1 ,(255,255,255) , 2)
cv2.imshow("result" ,o)
cv2.waitKey()
cv2.destroyAllWindows()

原图:

效果图:

2.cv2.minAreaRect()

该函数能绘制轮廓的最小包围矩形框,矩形可能会发生旋转 possibly rotated,以保证区域面积最小

函数形式:retval = cv2.minAreaRect( points )

参数介绍:

返回值retval表示返回矩阵特征的信息,结构为(最小外接矩形的中心(x,y) , (宽度, 高度) , 旋转角度)

关于旋转角度的注意事项:

(1)旋转角度是水平轴(x轴)逆时针旋转,与碰到的矩形第一条边的夹角。

(2)“ 第一条边 " 定义为 宽width,另一条边定义为高 height。这里的宽、高不是按照长短来定义的。

(3)在 opencv 中,坐标系原点在图像左上角,将其延伸到整个二维空间,可以发现 “x轴镜像对称”,角度则 逆时针旋转为负、顺时针旋转为正。故θ∈(-90度,0];(笛卡尔坐标系中,逆时针为正、顺时针为负)

(4)旋转角度为角度值,而非弧度制。

points 为 轮廓

返回值retval 结构不能用于函数cv2.drawContours() 参数结构要求, 需要将其转换为合适的参数结构,即使用函数cv2.boxPoints(box)

作用:查找旋转矩形的 4 个顶点(用于绘制旋转矩形的辅助函数)。

cv2.boxPoints(box) -> points

参数:

box - 旋转的矩形

返回值:列表list

points - 矩形 4 个顶点组成的列表 list

返回值示例:

[[614.9866  675.9137 ]

 [161.      232.99997]

 [302.4203   88.04419]

 [756.40686 530.9579 ]]

参考引用:

OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法..._san.hang的博客-CSDN博客

代码:

import cv2                                                                       
import numpy as np 
o = cv2.imread("cc.bmp")
cv2.imshow("original" , o) 
gray = cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)
ret , binary = cv2.threshold(gray , 127, 255 , cv2.THRESH_BINARY)
contours , hierarchy = cv2.findContours(binary , cv2.RETR_LIST ,  
        cv2.CHAIN_APPROX_SIMPLE)
rect = cv2.minAreaRect(contours[0])
print("rect:\n" , rect)
points = cv2.boxPoints(rect)
print("\npoints_back:\n" , points)
points = np.int0(points)
binary = cv2.drawContours(o , [points] , 0 ,(255,255,255) , 2) 
cv2.imshow("result" , o) 
cv2.waitKey()
cv2.destroyAllWindows()

效果图:

3. cv2.minEnclosingCircle()

作用:该函数构造一个对象的面积最小包围圆形。

函数形式:center , radius = cv2.minEnclosingCircle( points)

参数介绍:

  center为最小包围圆形的中心,圆心(x,y)

  radius为最小包围圆形的半径

  points轮廓

代码:

import cv2
o = cv2.imread("cc.bmp")
cv2.imshow("original" , o)
gray = cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)
ret , binary = cv2.threshold(gray , 127 , 255 ,cv2.THRESH_BINARY)
contours , hirrarchy = cv2.findContours( binary , cv2.RETR_LIST ,
        cv2.CHAIN_APPROX_SIMPLE)
(x,y) , radius = cv2.minEnclosingCircle(contours[0])
center = (int(x) , int(y))
radius = int(radius)
cv2.circle(o , center , radius , (255,255,255) , 2)
cv2.imshow("resultl" , o)
cv2.waitKey()
cv2.destroyAllWindows() 

效果图:

4.cv2.fitEllipse()

作用:可以构造最优拟合椭圆,

函数形式:retval = cv2.fitEllipse( points )

参数介绍:

  retval 是RotatedRect()类型的值,这是因为该函数返回的是 拟合椭圆的外接矩形,包含外接矩形的质心,宽,高,旋转角度,正好与椭圆的中心点,轴长度,旋转角度等信息吻合。

 代码:

import cv2
o = cv2.imread("cc.bmp")
gray = cv2.cvtColor( o , cv2.COLOR_BGR2GRAY)
ret , binary = cv2.threshold(gray , 127 , 255, cv2.THRESH_BINARY)
contours , hierarchy = cv2.findContours(binary , cv2.RETR_LIST ,
        cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow("original" , o)
ellipse = cv2.fitEllipse(contours[0])
print("ellipse=" , ellipse)
cv2.ellipse(o , ellipse ,(0 , 255, 0) , 3)
cv2.imshow("result" , o)
cv2.waitKey()
cv2.destroyAllWindows() 

效果图:

 5.cv2.fitLine()

作用:最优拟合直线

函数形式:line = cv2.fitLine( points , distType , param , reps , aeps )

参数介绍:

  line 返回的是最优拟合直线参数

  points是轮廓

  distType是距离类型,

  param是距离参数,与算选激励类型有关, 当参数设置为0时,会自动选择最优值

  reps表示拟合直线所需的径向精度,通常被设置为0.01

  aeps表示拟合直线所需的角度精度,通常被设置为0.01

代码:

import cv2                                                                       
o = cv2.imread("cc.bmp")
cv2.imshow("original" , o) 
gray = cv2.cvtColor(o , cv2.COLOR_BGR2GRAY)
ret , binary = cv2.threshold(gray , 127 ,255 , cv2.THRESH_BINARY)
contours , hierarchy = cv2.findContours(binary ,cv2.RETR_LIST ,  
        cv2.CHAIN_APPROX_SIMPLE)
rows , cols = binary.shape[:2]
[vx , vy , x, y] = cv2.fitLine(contours[0] , cv2.DIST_L2, 0,0.01 , 0.01)
lefty = int((-x*vy/vx) + y) 
righty = int(((cols - x) * vy/vx) + y )
cv2.line(o , (cols -1 ,righty) , (0,lefty) , (0,255,0) , 2) 
cv2.imshow("result" , o) 
cv2.waitKey()
cv2.destroyAllWindows() 

效果图:

 官方资源:OpenCV: Contour Features

参考资源:

https://www.cnblogs.com/miaorn/p/12264072.html

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐