【Opencv-Python 官方教程】3.Opencv画图函数

本节目标

  • 学会用Opencv画不同的几何形状
  • 将会学到以下几个函数:cv2.line()、cv2.circle()、cv2.rectangle()、cv2.ellipse()、cv2.putText()

所有上述函数,你将用到一些共同的参数如下

  • img,你画图的画板,或者说背景图。
  • color,当前绘画的颜色。如在BGR模式下,传递(255,0,0)表示蓝色画笔。灰度图下,只需要传递亮度值即可。
  • thickness,画笔的粗细,线宽。若是-1表示画封闭图像,如填充的圆。默认值是1.
  • lineType,线条的类型,如8-connected类型、anti-aliased线条(反锯齿),默认情况下是8-connected样式ide,cv2.LINE_AA表示反锯齿线条,在曲线的时候视觉效果更佳。

原文地址:Drawing Functions in OpenCV


画直线

画一条线,只需要传递起始点和终点坐标即可,下面的代码创建一个黑色的画板,然后在坐上到右下对角线上画一个蓝色的线条。

line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None) ,pt1、pt2,起止点坐标

import numpy as np
import cv2

# 创建一个图像作为背景图,size:512*512,channel:3
img = np.zeros((512, 512, 3), np.uint8)

# 1.画蓝色对角线
img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)


# 显示并回收资源
cv2.imshow('draw',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

画矩形

需要传递矩形的左上角和右下角两个点坐标,比如,如下代码在画板的右上角画一个绿色的矩形

# 2.画矩形
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

画圆

需要给出中心坐标和半径。下面代码在上述画出的矩形中画一个红色的圆,通过thickness=-1设置为填充。

# 3.画圆
img = cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)

画椭圆

画椭圆需要多传递几个参数。首先是中心点坐标,然后是长短轴的长度,之后是椭圆逆时针旋转的角度angle,然后是startAngle和endAngle分别代表椭圆开始和结束的角度(该角度是沿长轴顺时针旋转的角度,这里给两个角度因为它可以画椭圆弧,而不仅仅是满的椭圆),比如当给参数stratAngle和endAngle分别传递0和360,表示画一个满的椭圆。更多细节,可查看cv2.ellipse()的函数文档。下面我们画一个半椭圆在画布的中心。

注意,angle是整个椭圆的旋转角度(逆时针)。startAngle和endAngle是画笔画过的角度(顺时针)

# 4.画椭圆
img = cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)

画多边形(Polygon)

画多边形,首先需要给出一组(数组)顶点的坐标,形式是ROWSx1x2,rows表示顶点的数量,它的类型是int32.这里我们用黄色画一个小的4边形。

# 5.画多边形
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img, [pts], True, (0, 255, 255))

事实上,pts = pts.reshape((-1,1,2))注掉并不影响效果。

如果第三个参数设为False,只会得到顶点依次相连的图形(首尾不连),而不会得到所有顶点封闭连接的图形

tips:cv2.polylines()可以被用来同时画多条线,只需要同时创建多个线段,传入函数中,它将独立的画出所有线,这比重复为每条线调用cv2.line()更快更好。

# 6.polylines同时画多条线
line1 = np.array([[100, 50], [200, 300]], np.int32)
line2 = np.array([[120, 60], [250, 350]], np.int32)
line3 = np.array([[180, 80], [100, 380]], np.int32)
img = cv2.polylines(img, [line1, line2, line3], False, (0, 255, 255))

添加文本

在画布中添加文本,需要指定以下个参数:

  • Text,想要输出到图像上的的文本
  • Position,输出位置的坐标
  • Font type,字体,可以用cv2.putText()函数文档查看支持的字体
  • Font Scale,指定字体大小
  • 其他,如颜色,线宽,线型等,推荐使用lineType = cv2.LINE_AA
# 7.输出文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

完整代码及结果如下

# -*- coding: utf-8 -*-
# @File    : 3_draw.py
# @Time    : 2018/5/23 17:41
# @Author  : hyfine
# @Contact : [email protected]
# @Desc    : draw functions

import numpy as np
import cv2

# 创建一个图像作为背景图,size:512*512,channel:3
img = np.zeros((512, 512, 3), np.uint8)

# 1.画蓝色对角线
img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
# 2.画矩形
img = cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
# 3.画圆
img = cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)
# 4.画椭圆
img = cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
# 5.画多边形
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
# pts = pts.reshape((-1,1,2))
# print(pts.shape)
img = cv2.polylines(img, [pts], False, (0, 255, 255))

# 6.polylines同时画多条线
line1 = np.array([[100, 50], [200, 300]], np.int32)
line2 = np.array([[120, 60], [250, 350]], np.int32)
line3 = np.array([[180, 80], [100, 380]], np.int32)
img = cv2.polylines(img, [line1, line2, line3], False, (0, 255, 255))

# 7.输出文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

# 显示并回收资源
cv2.imshow('draw', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

这里写图片描述


练习

利用所学函数,画opencv logo

# -*- coding: utf-8 -*-
# @File    : 3_draw_exercise.py
# @Time    : 2018/5/23 21:24
# @Author  : hyfine
# @Contact : [email protected]
# @Desc    : draw function exercise:画opencv logo

import numpy as np
import cv2

# 1.创建一个蓝色背景
img = np.zeros((512, 512, 3), np.uint8)
# 三个通道分别赋值,天蓝色的rgb值是(0, 204, 255),opencv通道顺序是BGR
img[:, :, 0] = np.ones([512, 512], np.uint8) * 255
img[:, :, 1] = np.ones([512, 512], np.uint8) * 204
img[:, :, 2] = np.ones([512, 512], np.uint8) * 0

# 2.画三个带缺口的圆,坐标随便计算,半径为30,缺口角度为60
# 最上面的红圈
img = cv2.ellipse(img, (256, 100), (30, 30), 120, 0, 300, (0, 0, 255), -1, cv2.LINE_AA)
# img = cv2.ellipse(img, (256, 100), (10, 10), 120, 0, 300, (255, 204, 0), -1, cv2.LINE_AA)
img = cv2.circle(img, (256, 100), 10, (255, 204, 0), -1, cv2.LINE_AA)

# 画左下的绿色
img = cv2.ellipse(img, (226, 160), (30, 30), 0, 0, 300, (0, 255, 0), -1, cv2.LINE_AA)
img = cv2.circle(img, (226, 160), 10, (255, 204, 0), -1, cv2.LINE_AA)

# 画右下的蓝色
img = cv2.ellipse(img, (290, 160), (30, 30), -60, 0, 300, (255, 0, 0), -1, cv2.LINE_AA)
img = cv2.circle(img, (290, 160), 10, (255, 204, 0), -1, cv2.LINE_AA)

# 3.画文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (160, 240), font, 2, (255, 255, 255), 8, cv2.LINE_AA)

# 显示并回收资源
cv2.imshow('draw', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

注:此处

img = cv2.ellipse(img, (256, 100), (30, 30), 120, 0, 300, (0, 0, 255), -1, cv2.LINE_AA)
#img = cv2.ellipse(img, (256, 100), (10, 10), 120, 0, 300, (255, 204, 0), -1, cv2.LINE_AA)
img = cv2.circle(img, (256, 100), 10, (255, 204, 0), -1)

一开始画圆形的思路时,画两个长短轴相等的椭圆,且开口相同(startAngle、endAngle流出60度),都使用填充画笔,内部填充成背景颜色即可。但是这样处理有个问题,内部会出现一个轮廓线,因为这个轮廓线是属于外层的,且外层和背景色不同,针对这种情况,内部改用整圆画背景色就可以了。

logo中的坐标是粗略估计然后试出来的,所有logo并不精细

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013095718/article/details/80427559