OpenCV提取IsoGD数据集u、v光流

@OpenCV提取IsoGD数据集u、v光流

import cv2
import glob
import numpy as np
import os

def cal_for_frames(video_path):
    for subDirList in os.listdir(video_path):
        path = './a/' + subDirList + '//'
        frames = glob.glob(os.path.join(path, '*.jpg'))
        frames.sort()
        print(frames)
        flow = []
        prev = cv2.imread(frames[0])
        prev = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
        for i, frame_curr in enumerate(frames[1:]):
            curr = cv2.imread(frame_curr)
            curr = cv2.cvtColor(curr, cv2.COLOR_BGR2GRAY)
            tmp_flow = compute_TVL1(prev, curr)
            flow.append(tmp_flow)
            prev = curr
 
    return flow
 
 
def compute_TVL1(prev, curr, bound=15):
    """Compute the TV-L1 optical flow."""
    TVL1=cv2.optflow.DualTVL1OpticalFlow_create()
    # TVL1 = cv2.DualTVL1OpticalFlow_create()
    # TVL1=cv2.createOptFlow_DualTVL1()
    flow = TVL1.calc(prev, curr, None)
    assert flow.dtype == np.float32
 
    flow = (flow + bound) * (255.0 / (2 * bound))
    flow = np.round(flow).astype(int)
    flow[flow >= 255] = 255
    flow[flow <= 0] = 0
 
    return flow
 
 
def save_flow(video_flows, flow_path):
    for subDirList in os.listdir(flow_path):
        path = './out/' + subDirList + '/'
        if not os.path.exists(os.path.join(path, 'u')):
            os.mkdir(os.path.join(path, 'u'))
        if not os.path.exists(os.path.join(path, 'v')):
            os.mkdir(os.path.join(path, 'v'))
        for i, flow in enumerate(video_flows):
            cv2.imwrite(os.path.join(path,'u', "{:06d}.jpg".format(i)),
                        flow[:, :, 0])
            cv2.imwrite(os.path.join(path,'v', "{:06d}.jpg".format(i)),
                        flow[:, :, 1])
 
 
def extract_flow(video_path, flow_path):
    flow = cal_for_frames(video_path)
    save_flow(flow, flow_path)
    print('complete:' + flow_path)
    return

if __name__ == '__main__':
    dirs = "./a"
    save_path='./out'
    extract_flow(dirs,save_path)

#从指定文件夹写入到指定文件夹

import cv2
import glob
import numpy as np
import os

def cal_for_frames(video_path, flow_path):
    for subDirList in os.listdir(video_path):
        path = 'G:/IsoGD/dataset/train/RGB/' + subDirList + '/'
        print(path)
        frames = glob.glob(os.path.join(path, '*.jpg'))
        frames.sort()
        #print(frames)
        flows = []
        prev = cv2.imread(frames[0])
        prev = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
        for i, frame_curr in enumerate(frames[1:]):
            curr = cv2.imread(frame_curr)
            curr = cv2.cvtColor(curr, cv2.COLOR_BGR2GRAY)
            tmp_flow = compute_TVL1(prev, curr)
            flows.append(tmp_flow)
            prev = curr
        for subDirList in os.listdir(flow_path):
            subPath = 'G:/IsoGD/dataset/train/flow/' + subDirList + '/'
            #print('save path', end = ' ')
            #print(subPath[29: -1])
            if (path[28: -1] == subPath[29: -1]):
                #print('A')
                if not os.path.exists(os.path.join(subPath, 'u')):
                    os.mkdir(os.path.join(subPath, 'u'))
                if not os.path.exists(os.path.join(subPath, 'v')):
                    os.mkdir(os.path.join(subPath, 'v'))
                for i, flow in enumerate(flows):
                    cv2.imwrite(os.path.join(subPath,'u', "{:06d}.jpg".format(i)), flow[:, :, 0])
                    #print('save direction of u has successful!')
                    cv2.imwrite(os.path.join(subPath,'v', "{:06d}.jpg".format(i)), flow[:, :, 1])
                    #print('save direction of v has successful!')
                print('save path: {0}'.format(subPath))
 
def compute_TVL1(prev, curr, bound=15):
    #print('calculate optical-flow...')
    TVL1=cv2.optflow.DualTVL1OpticalFlow_create()
    flow = TVL1.calc(prev, curr, None)
    assert flow.dtype == np.float32
 
    flow = (flow + bound) * (255.0 / (2 * bound))
    flow = np.round(flow).astype(int)
    flow[flow >= 255] = 255
    flow[flow <= 0] = 0
 
    return flow

if __name__ == '__main__':
    dirs = "G:/IsoGD/dataset/train/RGB"
    save_path='G:/IsoGD/dataset/train/flow'
    cal_for_frames(dirs, save_path)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值