OpenCV中的绘图函数主要有:cv2.line()、cv2.circle()、cv2.rectangle()、cv2.ellipse()、cv2.putText()
class ll_paint(object): def draw_line(self): # 创建黑色画布 img = np.zeros((512, 512, 3), np.uint8) # 绘制蓝色线条 cv2.line(img, ( 0, 0 ), (511, 511), (255, 0, 0), 5) # 在窗口中显示img cv2.imshow("blue line", img) cv2.waitKey(0)if __name__ == ' __main__': paint = ll_paint() paint.draw_line()
绘制矩形
def draw_rectange(self): # 创建黑色画布 img = np.zeros( (512, 512, 3), np.uint8) # 画一个绿色矩形 cv2.rectangle(img, (100, 100), (300, 200), (0, 255, 0), 3) # 显示img在 window cv2.imshow("green rectangle", img) cv2.waitKey(0)
画一个圆
def draw_circle(self ): # 创建 a black canvas img = np.zeros((512, 512, 3), np.uint8) # 画一个以(200,200)为圆心,100为半径的红色圆 cv2.circle(img, ( 200, 200), 100, (0, 0, 255), 3) # 在窗口中显示img cv2.imshow("red circle", img) cv2.waitKey(0)
画一个椭圆
def draw_ellipse(self): # 创建一个黑色画布 img = np.zeros((512, 512, 3), np.uint8) # 以(200,200)为圆心,以50为短半径,以100为圆心画一个圆 长半径蓝色椭圆# cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) cv2.ellipse(img, (200, 200), (100, 50), 0, 0, 360, 255, 3) # 在窗口中显示img cv2.imshow("ellipse", img) cv2.waitKey(0)
注:ellispe参数 描述
center:椭圆的中心坐标(x,y)
axes:椭圆的轴距(y,x),x和y分别代表椭圆的最大和最小半径 椭圆,如果x=y,画出来的是一个圆; 如果 x! =y 画成椭圆
angle:旋转角度,椭圆整体在画布上的旋转角度
startAngle:开始角度,endAngle:结束角度,要画出一个完整的椭圆,这两个值需要 填充0和360。如果画半椭圆,可以传入0、180或180,360。 当然,如果需要绘制任意角度的偏椭圆,可以修改这两个角度参数。
color:椭圆边框的颜色
thinkness:椭圆边框的宽度
绘制多边形
def draw_reshape(self ):
# 创建一个黑色画布
img = np. zeros((512, 512, 3), np.uint8)
pts = np.array([[100, 50], [200, 300], [400, 500], [500, 100]], np.int32)
分 = 分。 重塑(-1、1、2)
cv2.polylines(img, [pts], True, (0, 255, 255))
# 将 img 显示到窗口
cv2.imshow("红圈", img)
cv2.waitKey(0)
注:cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) ->img
isClosed: 传入False时,多边形不闭合,不绘制最后一行
添加文字
#创建黑色画布
img = np. zeros((512, 512, 3), np.uint8)
字体 = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, "OpenCV", (10, 200), 字体, 4, (255, 255, 255), 2)
# 将 img 显示到窗口
cv2.imshow("文本", img)
cv2.waitKey(0)
#头条创作挑战赛#