004Python_Opencv 绘图

Opencv 绘图

  • 1、zeros—构造全0矩阵:
  • zeros(shape, dtype=None, order=’C’)
    shape:矩阵大小;例如:300x300;
    dtype:数据类型;例如:”uint8”
    order:数据排列顺序,默认按列排的
#封装成一个函数
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()
  • 5、ellipse —画椭圆:
  • ellipse( img, center, axes, angle, startAngle, endAngle, color,thickness = 1, lineType = 8, shift = 0 )
    img : 要绘制椭圆的图像
    center : 椭圆圆心
    axes: 椭圆主轴
    angle:椭圆旋转角度度数
    startAngle: 椭圆角度起始弧度
    endAngle: 椭圆角度结束弧度
    color: 椭圆颜色
    thickness: 椭圆弧线的厚度
    lineType: 绘制椭圆线的形态
#一个眼睛
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()
  • 6、fillPoly—多边形:
  • **fillPoly(img, pts,isClosed, color, thinkness,lineType = 8, shift =0
    img:要填满区域所在的图像
    pst:pts:多边形的拐点,numpy 数组
    isClosed:多边形是否要闭合
    color:多边形的颜色
    lineType:多边形的形态
    shift:顶点坐标(vertex coordinate)的部分位数
    offset:轮廓的位移点
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()
  • 7、putText—画字
  • putText(img, text, org, fontFace, fontScale, color,thickness, lineType, bottomLeftOrigin)
    text:文字的字符串
    org:文字放置位置的左下点(x, y)
    fontFace:字体类型
    cv2.FONT_HERSHEY_SIMPLEX:正常尺寸的sans-serif字体
    cv2.FONT_HERSHEY_SPLAIN:小尺寸的sans-serif字体
    cv2.FONT_HERSHEY_COMPLEX:正常尺寸的serif字体
    cv2.FONT_HERSHEY_SCREIPT_SIMPLEX:手写字体风格
    fontScale:字体的缩放因子
    thickness:宽度大于0,标识边线宽度;小于0,画实心矩形
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()

其他

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值