【Python】matplotlib替代cv画虚线矩形框

import cv2 as cv
import matplotlib.pyplot as plt
import matplotlib.patches as patches
plt.ion() #开启动态绘图
plt.figure() #建立图窗
def main():
    cap = cv.VideoCapture("./data/testvideo1.mp4")
    sz = (int(cap.get(cv.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
    fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
    video_write = cv.VideoWriter("./data/1.mp4", fourcc, 30, sz, True)
    frame_cnt = 1
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        a = (2, 3)
        b = (140, 160)
        cv.rectangle(frame, a, b, (255, 0, 0), 2, )
        rect = patches.Rectangle(a, b[0]-a[0], b[1]-a[1], linewidth=1, edgecolor='black', facecolor='none', linestyle='dotted')
        cv.imshow('Demo', frame)
        cv.imwrite("./image/{}.jpg".format(frame_cnt), frame)
        frame_cnt = + 1
        cv.waitKey(100)  # 显示 1000 ms 即 1s 后消失
        plt.imshow(frame[:, :, ::-1])
        currentAxis = plt.gca()
        currentAxis.add_patch(rect)
        plt.pause(0.01)
if __name__ == "__main__":
    main()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xi_shui/article/details/129580616