python - 如何使用 open cv 在视频中绘制虚线
问题描述
我尝试使用此代码在视频上绘制动画点
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()
推荐阅读
- firebase - 为什么 Flutter 一直尝试匿名登录用户?
- java - 创建 Spring Data JPA 存储库时遇到问题
- php - 为什么我的日志文件会重写前一个文件而不是附加到文件中?
- cucumber - 在我的黄瓜步骤定义中使用环境变量
- javascript - 使用 ajax php 和 mariadb 进行动态选择
- javascript - Javascript Object.entries 索引问题
- javascript - JSON 中位置 0 的意外标记 u(但仅有时)
- javascript - 仅接受来自授权 URL 的请求
- php - Woocommerce:产品图片大小不同
- json - 来自 STDIN 的 Python JSON 输入出现问题