Opencv 绘图
def int_mat(height,width,color=(255,255,255)):
mat = np.zeros((height,width,3),dtype='uint8')
mat[:,:,0]+=color[0]
mat[:,:,1]+=color[1]
mat[:,:,2]+=color[2]
return mat
cat = int_mat(600,600)
cv.imshow('img',cat)
cv.waitKey(0)
cv.destroyAllWindows()
- 2、line—画线 :
- line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
img:在img上绘图;
pt1:起点;例如:(0,0)
pt2:终点;例如:(300,300)
color:线的颜色;例如:(0,255,0)(绿色)
thickness:线的粗细程度,例如:-1,1,2,3…
mat = int_mat(600,600)
cv.line(mat,(200,200),(200,400),color = (0,0,0),thickness = 3)
cv.line(mat,(200,200),(400,200),color = (0,0,0),thickness = 3)
cv.line(mat,(400,200),(400,400),color = (0,0,0),thickness = 3)
cv.line(mat,(200,400),(400,400),color = (0,0,0),thickness = 3)
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
- 3、rectangle—画矩形
- rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
img:在img上绘图;
pt1:起点;例如:(0,0)
pt2:终点;例如:(300,300)
color:线的颜色;例如:(0,255,0)(绿色)
thickness:线的粗细程度,例如:-1,1,2,3…
其它参数默认
mat = int_mat(600,600)
cv.rectangle(mat,(150,150),(450,450),color = (0,0,0),thickness=-1)
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
- 4、circle—画圆形
- circle(img, center, radius, color, thickness=None, lineType=None, shift=None)
img:在img上绘图;
center:圆心;例如:(0,0)
radius:半径;例如:20
color:线的颜色;例如:(0,255,0)(绿色)
thickness:线的粗细程度,例如:-1,1,2,3…
mat = int_mat(600,600)
cv.circle(mat,(250,250),150,color=(0,255,0),thickness=3)
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
mat=int_mat(600,600)
cv.ellipse(img=mat,center=(256,256), axes=(100,50), angle=0, startAngle=180, endAngle=360, color=(100, 100, 50), thickness=3)
cv.ellipse(img=mat,center=(256,256), axes=(100,50), angle=180, startAngle=180, endAngle=360, color=(100, 100, 50), thickness=3)
cv.ellipse(img=mat,center=(256,256), axes=(50,30), angle=90, startAngle=180, endAngle=360, color=(100, 100, 50), thickness=3)
cv.ellipse(img=mat,center=(256,256), axes=(50,30), angle=270, startAngle=180, endAngle=360, color=(100, 100, 50), thickness=3)
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
mat = int_mat(600,600)
pts = [ [100,50],[200,300],[600,200],[500,100] ]
pst = np.array(pts).reshape(-1,1,2)
cv.polylines(mat,pts=[pst],isClosed = True,color=(0,255,0),thickness=3)
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
mat=int_mat(600,600)
cv.putText(img=mat,text='xxx',org=(121,112),thickness=2,fontFace=cv.FONT_HERSHEY_COMPLEX,fontScale=2.0,color=(0, 0, 0))
cv.imshow('img',mat)
cv.waitKey(0)
cv.destroyAllWindows()
其他