加快光流算法(如果适用)Python OpenCV [英] Speed Up Optical Flow algorithm (If applicable) Python OpenCV

查看:529
本文介绍了加快光流算法(如果适用)Python OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这种有趣的情况(加快光流(createOptFlow_DualTVL1)),但它不适用于我的需求.我的一般问题是,如果适用,我想尽可能地加快以下代码的速度.请记住,我希望框架为灰度并在保持纵横比锁定的同时将它们调整为height = 300的大小.另外,我想从该视频每秒采样2帧,因此我假设每个视频都在30fps附近.最后,我想使用TV-L1光流算法.有没有一种方法可以增强这种算法,因为对于1分钟的视频来说,估计光流大约需要3分钟,这对于我的需求而言太耗时了.

I came across this interesting situation (Speeding up optical flow (createOptFlow_DualTVL1)) but it doesn't apply to my needs. My general problem is I want to speed up as much as possible the following code if it is applicable. Keep in mind, I want the frames to be grayscale and resize them to height = 300 while keeping aspect ratio locked. Also, I want to sample 2 frames per second from that video so I assume every video to be around 30fps. Finally, I want to use the TV-L1 optical flow algorithm. Is there a way to boost this algorithm because for a 1-minute video it takes around 3 minutes to estimate the optical flow which is too time-consuming for my needs.

预先感谢, 埃文

import math, imutils, cv2
print ("Entering Optical Flow Module...")
        cap = cv2.VideoCapture(video_path)
        current_framerate = cap.get(5)
        ret, frame1 = cap.read()
        prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
        prvs = imutils.resize(prvs, height = 300)


        all_frames_flow=list()
        while(cap.isOpened()):
            frameId = cap.get(1)
            ret, frame2 = cap.read()
            if ret == True:
                if (frameId % (math.floor(current_framerate)/2)==0): # assume videos are 30 fps and we want only 2 frames per second.
                    next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
                    next = imutils.resize(next, height = 300)
                    optical_flow = cv2.DualTVL1OpticalFlow_create()
                    flow = optical_flow.calc(prvs, next, None)
                    all_frames_flow.append(flow)
                    prvs = next
                else:
                    continue
            else:
                break
        cap.release()

推荐答案

对于cv2版本'4.1.0'":

For cv2 version "'4.1.0'":

下面的代码更快,但根据下面对超参数的解释,其准确性较低.根据要求调整这些参数,以解决速度与精度之间的权衡.

Code below is faster but is less accurate as per the explanation of hyperparameters below. Tune these parameters to solve speed vs accuracy trade-off as per requirement.

optical_flow= cv2.optflow.DualTVL1OpticalFlow_create(nscales=1,epsilon=0.05,warps=1)
flow = optical_flow.calc(new_prvs, new_nxt, None)

  • int"nscales":用于创建图像金字塔的比例数.

    • int "nscales" : Number of scales used to create the pyramid of images.

      int翘曲":每刻度的翘曲数.表示每标度计算I1(x + u0)和grad(I1(x + u0))的次数.这是确保方法稳定性的参数.它还会影响运行时间,因此是速度和准确性之间的折衷.

      int "warps" : Number of warpings per scale. Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale. This is a parameter that assures the stability of the method. It also affects the running time, so it is a compromise between speed and accuracy.

      double epsilon:数值方案中使用的停止标准阈值,这是精度和运行时间之间的权衡.较小的值将产生更准确的解决方案,但会降低收敛速度.

      double epsilon : Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time. A small value will yield more accurate solutions at the expense of slower convergence.

      其他要调整的参数可以在此处

      other parameters to tune can be found here

      这篇关于加快光流算法(如果适用)Python OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆