《OpenCV 轻松入门 面向Python》 学习笔记
OpenCV提供了绘制直线的函数 cv2.line()、绘制矩形的函数 cv2.rectangle()、绘制圆的函数 cv2.circle()、绘制椭圆的函数 cv2.ellipse()、绘制多边形的函数 cv2.polylines()、在图像内添加文字的函数 cv2.putText()等多种绘图函数。
这些绘图函数有一些共有参数,主要用于设置原图像,颜色,线条属性等。
img = cv2.line( img, pt1, pt2, color[, thickness[, lineType ]])
import cv2
import numpy as np
n = 300
img = np.zeros((n+1, n+1, 3), dtype=np.uint8)
img = cv2.line(img, (0, 0), (n, n), (255, 0, 0), 1)
img = cv2.line(img, (0, 100), (n, 100), (0, 255, 0), 3)
img = cv2.line(img, (100, 0), (100, n), (0, 0, 255), 6)
cv2.imshow('draw_lines', img)
cv2.waitKey()
cv2.destroyAllWindows()
img = cv2.rectangle( img, pt1, pt2, color[, thickness[, lineType]] )
import cv2
import numpy as np
n = 300
img = np.ones((n+1, n+1, 3), dtype=np.uint8) * 255
img = cv2.rectangle(img, (50, 50), (n-100, n-50), (0, 0, 255), -1)
cv2.imshow('draw_rectangle', img)
cv2.waitKey()
cv2.destroyAllWindows()
img = cv2.circle( img, center, radius, color[, thickness[, lineType]] )
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
(center_x, center_y) = (round(img.shape[1]/2), round(img.shape[0]/2))
for r in range(5, round(d/2), 12):
img = cv2.circle(img, (center_x, center_y), r, (0, 0, 255), 1)
cv2.imshow('draw_circle', img)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
for r in range(100):
# 随机生成原型坐标
center_x = np.random.randint(0, d)
center_y = np.random.randint(0, d)
# 随机生成半径大小
radius = np.random.randint(0, d/5)
# 随机生成颜色
color = np.random.randint(0, 256, (3,)).tolist()
# 画圆
img = cv2.circle(img, (center_x, center_y), radius, color, -1)
cv2.imshow('draw_circle', img)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置椭圆圆心为画布的中心
(center_x, center_y) = (round(d/2), round(d/2))
# 设置椭圆轴长度
size = (100, 200)
for r in range(10):
# 设置随机偏移角度
angle = np.random.randint(0, 361)
# 设置随机颜色
color = np.random.randint(0, 256, (3,)).tolist()
# 设置随机线条粗细
thickness = np.random.randint(1, 9)
# 画圆
img = cv2.ellipse(img, (center_x, center_y), size, angle, 0, 360, color, thickness)
cv2.imshow('draw_ellipse', img)
cv2.waitKey()
cv2.destroyAllWindows()
img = cv2.polylines( img, pts, isClosed, color[, thickness[, lineType[, shift]]])
参数 isClosed=True 时
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置顶点
points = np.array([[200, 50], [300, 200], [200, 350], [100, 200]], np.int32)
points = points.reshape((-1, 1, 2))
img = cv2.polylines(img, [points], True, (0, 250, 0), 8)
cv2.imshow('draw_polylines', img)
cv2.waitKey()
cv2.destroyAllWindows()
参数 isClosed=False 时
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置顶点
points = np.array([[200, 50], [300, 200], [200, 350], [100, 200]], np.int32)
points = points.reshape((-1, 1, 2))
img = cv2.polylines(img, [points], False, (0, 250, 0), 8)
cv2.imshow('draw_polylines', img)
cv2.waitKey()
cv2.destroyAllWindows()
img=cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
import cv2
import numpy as np
d = 400
img = np.ones((d, d, 3), dtype=np.uint8) * 255
# 设置字体
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.putText(img, 'OpenCV', (0, 200), font, 3, (0, 255, 0), 15)
img = cv2.putText(img, 'OpenCV', (0, 200), font, 3, (0, 0, 255), 5)
cv2.imshow('draw_putText', img)
cv2.waitKey()
cv2.destroyAllWindows()