jmonroe
jmonroe

Reputation: 61

mouse click events on a saved video using opencv and python

I want to draw a rectangle in a saved video. 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 saved video using opencv and python.

Upvotes: 1

Views: 1513

Answers (1)

arccoder
arccoder

Reputation: 57

I was in need of a ROI selection mechanism using opencv and I finally figured how to implement it.

The implementation can be found here (opencvdragrect). It uses opencv 3.1.0 and Python 2.7

For a saved video till the time you don't read another frame and display it, the video is considered as paused.

In terms of how to add it to a paused video (frame), this code below might help.

import cv2
import selectinwindow
wName = "select region"

video = cv2.VideoCapture(videoPath)

while(video.isOpened()):

    # Read frame 
    ret, RGB = video.read()
    frameCounter += 1

    if frameCounter == 1 : # you can pause any frame you like

        rectI = selectinwindow.dragRect
        selectinwindow.init(rectI, I, wName, I.shape[1], I.shape[0])
        cv2.namedWindow(wName)
        cv2.setMouseCallback(wName, selectinwindow.dragrect, rectI)

        while True:

            # display the image
            cv2.imshow(wName, rectI.image)
            key = cv2.waitKey(1) & 0xFF

            # if returnflag is set break 
            # this loop and run the video
            if rectI.returnflag == True:
                break

        box = [rectI.outRect.x,rectI.outRect.y,rectI.outRect.w,rectI.outRect.h]

    # process the video
    # ...
    # ...

In the (opencvdragrect) library you use double-click to stop the rectangle selection process and continue with video.

Hope this helps.

Upvotes: 1

Related Questions