python - 如何使用OpenCV在视频上绘制尾线

标签 python opencv video-processing

我试图在特定的时间戳间隔内使用视频上的点绘制路径。
该代码工作正常,但该点的先前位置消失了,我不希望它消失。任何人都可以帮助我调整一下此代码中的所有内容。

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()    

关于python - 如何使用OpenCV在视频上绘制尾线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61548867/

相关文章:

python - 为什么 python 3 给我 "expected string or buffer"

python - Django:json.dumps 破坏 JSON 数组

python - 如何检测轮廓的位置

c++ - libavcodec 视频解码不起作用

c# - 在 Windows Phone 8 中以非标准尺寸录制(或录制后转换)视频

python - Django:相关模型中外键总计值的正确方法

python - 将 Pandas 数据框传递给 Django 模板

python - google colab python3 名称 cv2 未定义

c - 如何从c语言程序中调用LSD(LineSegmentDetector)?

java - 是否可以流式传输 ITU-R BT.656 格式的视频?