绘制直线
line(img, 开始点,结束点,颜色,线宽,线型(默认为8))
python
import cv2
import numpy as np
img = np.zeros((640, 480, 3), np.uint8)
#画线,坐标点为(x, y)
cv2.line(img, (10, 20), (10, 220), (0, 0, 255), 5, 4)
# 16 线条更平滑
cv2.line(img, (90, 100), (380, 480), (0, 0, 255), 5, 16)
cv2.imshow('img', img)
cv2.waitKey(0)
绘制矩形
rectangle(img,开始点,结束点, 颜色, 线框)
python
import cv2
import numpy as np
img = np.zeros((640, 480, 3), np.uint8)
#画矩形
cv2.rectangle(img, (10, 10), (210, 210), (0, 255, 0), -1)
cv2.imshow('img', img)
cv2.waitKey(0)
## 绘制椭圆
ellipse(img, 中心点, 长宽的一半, 角度, 从哪个角度开始, 从哪个角度结束, ...)
python
import cv2
import numpy as np
img = np.zeros((640, 480, 3), np.uint8)
# 画椭圆
# 度是按顺时针计算的
# 0度是从左侧开始的
cv2.ellipse(img, (320, 240), (100, 50), 0, 0, 360, (0, 0, 255), -1)
cv2.imshow('img', img)
cv2.waitKey(0)
绘制多边形
fillPoly(img, 点集, 是否闭环, 颜色, ...)
python
import cv2
import numpy as np
img = np.zeros((640, 480, 3), np.uint8)
#画多边形
pts = np.array([(300, 10), (150, 100), (450, 100)], np.int32)
cv2.polylines(img, [pts], True, (0, 0, 255))
#填充多边形
cv2.fillPoly(img, [pts], (255, 255, 255))
cv2.imshow('img', img)
cv2.waitKey(0)
绘制文本
putText(img, 字符串, 起始点, 字体, 字号, ...)
python
import cv2
import numpy as np
img = np.zeros((640, 480, 3), np.uint8)
#绘制文本
cv2.putText(img, "Hello Computer!", (100, 400), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0))
cv2.imshow('img', img)
cv2.waitKey(0)