opencv绘制椭圆

在Python环境下使用opencv绘制椭圆,需要用到cv2.ellipse() 函数。

下面一段示例程序就是用该函数在黑色背景下,绘制一个圆心在(260,240)、长轴170、短轴130、线宽为3的白色椭圆。

import cv2
import numpy as np
 
img=np.zeros((512,512,3),np.uint8) #设置背景
cv2.ellipse(img, (260, 240), (170, 130), 0, 0, 360, (255, 255, 255), 3) #画椭圆
cv2.imshow("test",img) #显示
cv2.waitKey(0) #按下任意键退出
cv2.destroyAllWindows()

cv2.ellipse()函数比较复杂,下面详细介绍一下涉及到的参数:

函数原型:

cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift) 

其中每个参数代表意义如下:

img:需要绘图的图像

center:椭圆中心点坐标

axes:椭圆尺寸(即长短轴)

angle:旋转角度(顺时针方向)

startAngle:绘制的起始角度(顺时针方向)

endAngle:绘制的终止角度(例如,绘制整个椭圆是0,360,绘制下半椭圆就是0,180)

color:线条颜色(BGR)

thickness:线条粗细(默认值=1)

lineType:线条类型(默认值=8)

shift:圆心坐标点和数轴的精度(默认值=0)


顺便介绍几个常用的绘图函数:

1、画直线 cv2.line()

cv2.line(img, Point pt1, Point pt2, color, thickness=1, line_type=8, shift=0)

pt1,pt2分别代表直线的两个端点。

2、画矩形 cv2.rectangle()

cv2.rectangle(img, Point pt1, Point pt2, color, thickness=1, line_type=8, shift=0)

pt1,pt2分别代表矩形的左上角点和右下角点。

3、画圆 cv2.circle()

cv2.circle(img, center, radius, color, thickness, lineType, shift) 

center,radius代表圆心和半径。


猜你喜欢

转载自blog.csdn.net/as1490047935/article/details/105121732