To draw an ellipse using opencv in the Python environment, you need to use the cv2.ellipse() function.
The following sample program is to use this function to draw a white ellipse with a center at (260,240), a major axis 170, a minor axis 130, and a line width of 3 on a black background.
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()
The cv2.ellipse() function is more complicated. The parameters involved are described in detail below:
Function prototype:
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift)
The meaning of each parameter is as follows:
img: the image to be drawn
center: the coordinates of the center point of the ellipse
axes: ellipse size (ie long and short axis)
angle: rotation angle (clockwise)
startAngle: the starting angle of drawing (clockwise)
endAngle: the end angle of drawing (for example, drawing the entire ellipse is 0,360, drawing the lower half of the ellipse is 0,180)
color: line color (BGR)
thickness: thickness of the line (default = 1)
lineType: line type (default value=8)
shift: the precision of the center coordinate point and the number axis (default value=0)
By the way, introduce several commonly used drawing functions:
1. Draw a straight line cv2.line()
cv2.line(img, Point pt1, Point pt2, color, thickness=1, line_type=8, shift=0)
pt1 and pt2 respectively represent the two end points of the straight line.
2. Draw a rectangle cv2.rectangle()
cv2.rectangle(img, Point pt1, Point pt2, color, thickness=1, line_type=8, shift=0)
pt1 and pt2 respectively represent the upper left corner and the lower right corner of the rectangle.
3. Draw a circle cv2.circle()
cv2.circle(img, center, radius, color, thickness, lineType, shift)
center, radius represents the center and radius of the circle.