python - 如何使用opencv在视频上绘制尾随线
问题描述
我试图在特定时间戳间隔内使用视频上的点绘制路径。代码工作正常,但点的前一个位置消失了,我不希望它消失。任何人都可以帮助我在此代码中进行哪些调整以保留所有点。
from collections import deque
from imutils.video import VideoStream
import numpy as np
import cv2
import imutils
import time
from numpy import random
vs = cv2.VideoCapture('/media/intercept.mp4')
pts = deque(maxlen=64) #buffer size
#Position to start drawing the dots
i=0
j=330
# 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=1800)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
i+=2
j=j-random.randint(-10,10) #introduce some jitter/randomness
i=i+random.randint(-10,10)
timestamps = vs.get(cv2.CAP_PROP_POS_MSEC)
if (15000<timestamps<20000):
print (i,j, "DRAWING")
cv2.circle(frame,(i, j),10, (0,0,255), -1) #draw dot
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
from numpy import random
vs = cv2.VideoCapture('/media/intercept.mp4')
pts = deque(maxlen=64) #buffer size
color = np.random.randint(0,255,(100,3))
ret, old_frame = vs.read()
old_frame = imutils.resize(old_frame,width=1800)
mask = np.zeros_like(old_frame)
i=0
j=330
ct=0
# 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=1800)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
x=i
y=j
i+=2
j=j-random.randint(-10,10)
i=i+random.randint(-10,10)
print (int(i%330))
ct+=10
timestamps = vs.get(cv2.CAP_PROP_POS_MSEC)
if (1000<timestamps<20000):
print (i,j, "Drawing")
#cv2.circle(frame,(i, j),10, (0,0,255), -1) #draw circle
mask = cv2.line(mask, (x,y),(i,j),[255,0,9], 2)
frame = cv2.circle(frame,(i,j),5,[0,255,222],-1)
img = cv2.add(frame,mask)
cv2.imshow("Frame", img)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.release()
推荐阅读
- c++ - 在 C++ 中创建字符串的邻接列表
- git - mac os上git的.config目录在哪里找?
- javascript - 使用 Javascript/Node js 的 Julian 日期转换器
- php - 我可以在网页中设置“默认书签”,以便浏览器在请求页面时自动转到(HTML5、CSS、PHP 等)?
- amazon-s3 - 如何从亚马逊 S3 存储桶中获取特定文件到雪花阶段表
- python - 如何腌制sklearn管道对象?
- python - 错误,应用程序无法启动,python 未定义,pip 未定义
- nginx - 在 ubuntu 18.04 上重新安装 nginx 的问题
- android - 如何在颤动中发送带有请求的文件
- google-apps-script - 使用 Resumable Upload 在 Google Drive 上上传大尺寸视频