1, cv2.line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
参数 |
含义 |
img |
图片 |
pt1 |
点1 起点坐标 |
pt2 |
点2 终点坐标 |
color |
线段颜色 |
thickness |
线条的粗细, -1标识填充 |
lineType |
线条的类型 |
shift |
缩小倍数(一般默认不填) |
lineType
OpenCV里的线型-lineType
参数 |
含义 |
LINE_4 = 4 |
4联通 |
LINE_8 = 8 |
8联通 |
LINE_AA = 16 |
抗锯齿 |
2, cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift)
绘制矩形
参数 |
含义 |
img |
图片 |
pt1 |
点1 左上角坐标 |
pt2 |
点2 右下坐标 |
color |
矩形线颜色 |
thickness |
线的粗细,-1标识填充 |
lineType |
线的类型 |
shift |
缩小倍数(一般默认不填) |
3, cv2.circle(img, center, radius, color, thickness, lineType, shift)
绘制圆
参数 |
含义 |
img |
图片 |
center |
圆心坐标 |
radius |
半径 |
color |
圆线颜色 |
thickness |
线的粗细,-1标识填充 |
lineType |
线的类型 |
shift |
缩小倍数(一般默认不填) |
4,ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift)
绘制椭圆
参数 |
含义 |
img |
图片 |
center |
圆心坐标 |
axes |
长轴和短轴 |
angle |
椭圆沿逆时针旋转的角度 |
startAngle |
绘制椭圆圆弧顺时针旋转的起始角度 |
endAngle |
顺时针旋转的终止角度 |
color |
圆线颜色 |
thickness |
线的粗细,-1标识填充 |
lineType |
线的类型 |
shift |
缩小倍数(一般默认不填) |
5, polylines(img, pts, isClosed, color, thickness, lineType, shift)
绘制多边形
参数 |
含义 |
img |
图片 |
pts |
点集合 |
isClosed |
是否闭合 |
color |
线颜色 |
thickness |
线的粗细,-1标识填充 |
lineType |
线的类型 |
shift |
缩小倍数(一般默认不填) |
6, putText(img, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)
绘制文字
参数 |
含义 |
img |
图片 |
text |
文字 |
org |
左上角顶点位置 |
fontFace |
字体 |
fontScale |
字体的大小 |
isClosed |
是否闭合 |
color |
线颜色 |
thickness |
线的粗细,-1标识填充 |
lineType |
线的类型 |
bottomLeftOrigin |
参数为True时,图片的原点时在左下角,默认情况下是在左上角 |
font
参数 |
含义 |
FONT_HERSHEY_SIMPLEX = 0 |
正常大小无衬线字体 |
FONT_HERSHEY_PLAIN = 1 |
小号无衬线字体 |
FONT_HERSHEY_DUPLEX = 2 |
正常大小无衬线字体比 CV_FONT_HERSHEY_SIMPLEX 更复杂) |
FONT_HERSHEY_COMPLEX = 3 |
正常大小有衬线字体. |
FONT_HERSHEY_TRIPLEX = 4 |
正常大小有衬线字体 ( 比 CV_FONT_HERSHEY_COMPLEX更复杂) |
FONT_HERSHEY_COMPLEX_SMALL = 5 |
CV_FONT_HERSHEY_COMPLEX 的小译本 |
FONT_HERSHEY_SCRIPT_SIMPLEX = 6 |
手写风格字体. |
FONT_HERSHEY_SCRIPT_COMPLEX = 7 |
比 CV_FONT_HERSHEY_SCRIPT_SIMPLEX 更复杂. |
FONT_ITALIC = 16 |
斜体字. |
import cv2
import numpy as np
from PIL import Image,ImageDraw,ImageFont
def show(img):
cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = np.zeros((500, 500, 3), np.uint8)
cv2.line(img, (100, 100), (400, 400), (0,0,255), 1, cv2.LINE_AA)
cv2.rectangle(img, (100,100), (400,400), (255,0,0), 1, cv2.LINE_AA)
cv2.circle(img, (250, 250), 150, (0, 255,0), 1, cv2.LINE_AA)
cv2.ellipse(img, (250,250), (170, 130), 45, 0, 180, (0,255,255),1,cv2.LINE_AA)
pts = np.array([[250, 100], [100, 250], [250, 250]],np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts] , True, (255, 255,255), 2, cv2.LINE_8)
font=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
cv2.putText(img, 'China', (100, 100),font , 2, (255, 255,255), 1, cv2.LINE_AA)
pil_img=Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
draw=ImageDraw.Draw(pil_img)
font=ImageFont.truetype('simhei.ttf',20,encoding="utf-8")
draw.text((50,200),"中国",(0,0,255),font)
frame=cv2.cvtColor(np.array(pil_img),cv2.COLOR_RGB2BGR)
show(frame)