我试着用这段代码在视频上画一个动画点。
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()
但整个动画发生在一帧中,而不是连续的帧。另外,我还想在红色球圈上添加一定的抖动随机性元素,如何实现这两点?
啊,通过调整睡眠定时器,跳过帧,解决了这个问题。
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()