首页 > 解决方案 > 如何通过在opencv python中单击鼠标按钮在播放视频的顶部绘制形状

问题描述

好吧,首先,我应该承认这是一个很长的问题,我无法通过谷歌搜索找到可能的解决方案

我有一段视频,其中入侵者试图闯入围栏的另一侧。

在此处输入图像描述

我可以跟踪入侵者,但是当他在另一边时,我应该能够将入侵持续时间保存到文件中。入侵区域将是这样的

在此处输入图像描述

我想了这些步骤:

I. Reading a video file;
II. Getting the very first frame displayed,
  1. Pausing the video playback;
  2. Manually drawing intrusion area on that frame with a mouse; (making draw and reset buttons as events maybe)
  3. Replaying the video again
III. Waiting for the intruder to appear, etc. (III part is not important)

到目前为止,我已经完成了 I 和 II(我知道这很傻),并且应该完成步骤 II 的 1、2、3 个子部分。

import cv2

file  = "intrusion.mp4"
capture = cv2.VideoCapture(file)

ret, firstFrame= capture.read()

while True:
    cv2.imshow("First Frame", firstFrame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

希望大家多多指教和指点!

PS: 参考任何相关的帖子、博客或链接,我很高兴发现

标签: pythonopencvmouseeventvideo-capture

解决方案


添加 cv2.waitKey(0) 将无限期暂停您的 while 循环!只有在任何按键按下后才会恢复。

我认为您要实现的是使用背景减法进行对象跟踪。请参阅此处,看看它是否符合您的要求。

编辑:

我猜你想为入侵区域画一个手绘的形状!此链接将指导您执行此操作。我希望这有帮助


推荐阅读