how to draw a shape on top of a playing video by clicking a mouse button in opencv python

半世苍凉 提交于 2019-12-04 02:00:05

问题


Well, to begin with, I should admit that it is a pretty long question and I failed to find possible solutions through googling

I have a video in which an intruder tries to intrude into the other side of the fence.

I can track the intruder, but when he is in the other side, I should be able to save the intrusion duration into a file. The intrusion area would be something like this

I thought these steps:

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)

So far, I've done I and II (silly, I know) and should accomplish 1,2,3 subparts of step II.

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()

I hope you can give your advice and instructions!

PS: refer to any related posts, blogs or links, I am excited to find out


回答1:


adding cv2.waitKey(0) will pause your while loop indefinitely! It will resume only after any key press.

I think what you're trying to achieve is object tracking using Background Subtraction. Refer Here and see if it fits your requirements.

EDIT:

I guess you want to draw a freehand shape for intrusion area! This Link will guide you to do it. I hope this helps



来源:https://stackoverflow.com/questions/54125451/how-to-draw-a-shape-on-top-of-a-playing-video-by-clicking-a-mouse-button-in-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!