[英]Mouse click events on a live stream with Opencv and python
i am new to opencv -python.我是 opencv -python 的新手。 I want to draw a rectangle in a live stream video captured from my webcam.我想在从我的网络摄像头捕获的实时流视频中绘制一个矩形。 While drawing the rectangle,the video must freeze.绘制矩形时,视频必须冻结。 I am successful in drawing a rectangle on a image,but i don't know how to do the same on a live video using opencv and python .我成功地在图像上绘制了一个矩形,但我不知道如何使用 opencv 和 python 在实时视频上做同样的事情。 Please help..请帮忙..
This is a code that I'm using to draw a rectange in videos.这是我用来在视频中绘制矩形的代码。
The code works like that:代码是这样工作的:
click again to start drawing another retangle再次单击以开始绘制另一个 retangle
import numpy as np import cv2 rect = (0,0,0,0) startPoint = False endPoint = False def on_mouse(event,x,y,flags,params): global rect,startPoint,endPoint # get mouse click if event == cv2.EVENT_LBUTTONDOWN: if startPoint == True and endPoint == True: startPoint = False endPoint = False rect = (0, 0, 0, 0) if startPoint == False: rect = (x, y, 0, 0) startPoint = True elif endPoint == False: rect = (rect[0], rect[1], x, y) endPoint = True cap = cv2.VideoCapture('../videos/sample.avi') waitTime = 50 #Reading the first frame (grabbed, frame) = cap.read() while(cap.isOpened()): (grabbed, frame) = cap.read() cv2.namedWindow('frame') cv2.setMouseCallback('frame', on_mouse) #drawing rectangle if startPoint == True and endPoint == True: cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (255, 0, 255), 2) cv2.imshow('frame',frame) key = cv2.waitKey(waitTime) if key == 27: break cap.release() cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.