Sunaina Chopra
Sunaina Chopra

Reputation: 89

How to draw a dotted line in a video using open cv

I tried this code to draw an animated dot on a video

from collections import deque
from imutils.video import VideoStream
import numpy as np
import cv2
import imutils
import time

vs = cv2.VideoCapture('/media/intercept.mp4')    
pts = deque(maxlen=64) #buffer size

# keep looping
while True:
    ret,frame = vs.read()

    if frame is None:
        break
    # resize the frame, blur it, and convert it to the HSV
    # color space
    frame = imutils.resize(frame, width=600)
    blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    for i in range(10,260,20):
        time.sleep(0.5)     #To visualise dots one by one
        cv2.circle(frame,(i, i),10, (0,0,255), -1) #draw circle
        cv2.imshow('frame',frame)     #show output image
        if cv2.waitKey(1) == ord('q'):
            break

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

cv2.destroyAllWindows()
vs.release()    

But the entire animation takes place over a single frame rather than being continous over consecutive frames. Also I want to add a certain element of jitter/randomness to the red ball/circle. How can I achieve both ?

Upvotes: 0

Views: 1150

Answers (1)

Sunaina Chopra
Sunaina Chopra

Reputation: 89

Ahh solved it by tweaking the sleep timer, and skipping the frames

from collections import deque
from imutils.video import VideoStream
import numpy as np
import cv2
import imutils
import time

vs = cv2.VideoCapture('/media/intercept.mp4')
pts = deque(maxlen=64) #buffer size

i=0
ct=0
# keep looping
while True:
    ret,frame = vs.read()
    # resize the frame, blur it, and convert it to the HSV
    # color space
    frame = imutils.resize(frame, width=600)
    blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    i+=2
    ct+=10
    #for i in range(10,260,20):
        #time.sleep(0.5)     #To visualise dots one by one
    if ct%10==0:
        cv2.circle(frame,(i, i),10, (0,0,255), -1) #draw circle
        #cv2.imshow('frame',frame)     #show output image
        if cv2.waitKey(1) == ord('q'):
            break

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

cv2.destroyAllWindows()
vs.release()   

Upvotes: 1

Related Questions