python Opencv基本操作——画点、线、圆、矩形、动态更新

本文参考了以下系列文章:
https://blog.csdn.net/u011520181/article/details/83933325
直接上代码,详见注释。图像每50ms更新一次,随着变量cnt的改变而变化:

import numpy as np
import cv2 as cv
cnt = 0
while True:
    center = (300, 300)
    img = np.ones((600, 600, 3), np.uint8)  # 生成一个空RGB图像
    # img = np.ones((600, 600), np.uint8)  # 生成一个空灰度图像

    point_color = (255, cnt, cnt)  # BGR (blue, green, red)
    # point_color = 180  # gray 单通道灰度图

    # 线型,生成线条的算法不同,都是实线
    lineType = cv.LINE_8
    # lineType = cv2.LINE_4
    # lineType = cv2.LINE_AA

    thickness = 1
    # 画圆    所在图像 位置    半径     颜色          宽度       线型
    cv.circle(img, center, 10+cnt, point_color, thickness, lineType)

    # 画点,即实心圆,通过控制size(半径)和thickness来控制是否是实心
    point_size = 2
    thickness = 2
    # 要画的点列表
    points_list = [(160+cnt, 160), (136, 160+cnt), (150, 200), (200, 180), (120, 150), (145, 180)]
    for point in points_list:
        cv.circle(img, point, point_size, point_color, thickness)

    # 画矩形
    ptLeftTop = (100, 100)
    ptRightBottom = (500, 500)
    #                 左上角点     右下角点
    cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)

    # 画直线
    ptStart = (400, 200)
    ptEnd = (200, 400)
    cv.line(img, ptStart, ptEnd, point_color, thickness, lineType)

    # 画椭圆
    axesSize = (90, 60)  # 长轴半径为 90,短轴半径为 60
    rotateAngle = 30  # 旋转角度为 30
    startAngle = 0
    endAngle = 270
    cv.ellipse(img, center, axesSize, rotateAngle, startAngle, endAngle, point_color, thickness, lineType)

    text = 'testOpencv'
    org = (50, 50)
    fontFace = cv.FONT_HERSHEY_COMPLEX
    fontScale = 1.5
    thickness = 0
    lineType = 8
    bottomLeftOrigin = False
    # bottomLeftOrigin = True
    cv.putText(img, text, org, fontFace, fontScale, point_color, thickness, lineType, bottomLeftOrigin)
    # cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)

    cv.namedWindow("image")
    cv.imshow('image', img)
    cnt += 1
    key = cv.waitKey(50)
    if key == ord('1'):   # 判断是哪一个键按下
        break
cv.destroyAllWindows()
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老孟的孟不是很老的孟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值