1

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.

2
  • You need to provide your current code. Use comments in the code to highlight where your problem is. Commented Apr 4, 2016 at 16:01
  • Declare a global char c=waitKey(10).When you start drawing the rectangle, event is LButtonDown, and set char c to waitKey(0). That might freeze the video Commented Apr 8, 2016 at 6:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.