python OpenCV学习笔记(三):画图

1.创建一个黑色图片

import numpy as np
import cv2 as cv

# Create a black image
img = np.zeros((512, 512, 3), np.uint8)

np.zeros()函数创建一个都是0的多元数组,可以理解为512行、512列、每格中有三个0,引入cv.imshow,之后,是一个512*512的黑色图片,每格中的三个0对应BGR,改变数值则会改变颜色

2.画一条线

cv.line(img, (0,0), (511,511), (255,0,0), 5)

cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

  • pt1:为起始点坐标
  • pt2:为终止点坐标
  • color:颜色
  • thickness:线宽度
  • lineType:线类型
  • shift:按比例缩小(整数类型,>=0,默认为0,填入数字,坐标值按比例缩小,比如填入1,则所有坐标值/2^1)

cv.line(img, (10,500), (500,10), (255,0,0), 5, cv.LINE_AA, 1)
等价于
cv.line(img, (5,250), (250,5), (255,0,0), 5, cv.LINE_AA, 0)

3.画一个矩形

cv.rectangle(img, (384,0), (510,128), (0,255,0), 3)

cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]] )

  • pt1:矩形的一个顶点坐标
  • pt2:与pt1相对的对角点的坐标

4.画一个圆

cv.circle(img,(447,63), 63, (0,0,255), -1)

cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]] )

  • center:圆心坐标
  • radius:半径

5.画椭圆

cv.ellipse(img,(256,256),(100,50),0,0,180,(255,0,0), -1)

cv.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])

  • axes:轴,参数为元组,(x轴半径,y轴半径)
  • angle:角度,设定初始角度,相对于水平向右x轴的角度
  • startAngle:相对于angle的起始角度
  • endAngle:相对于angle的终止角度
  • thickness:如果>=0,则是一个椭圆线,否则是一个填充的椭圆

6.画多边形

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv.polylines(img,[pts],True,(0,255,255))

cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
reshape()函数,详见https://www.zhihu.com/question/52684594

7.添加文字

font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img, 'OpenCV', (10,500), font, 4, (255,255,255), 2, cv.LINE_AA)

cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

  • org:图像文本字符串的左下角坐标
  • fontFace:字体类型,详见cv::HersheyFonts
  • bottomLeftOrigin:当正确的时候,图像数据的来源就在左下角。否则,它就在左上角。默认为False。True相对于False,在x轴向下翻转。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值