spynet(三):光流估计代码读取后缀为.flow的光流文件

9. 读取后缀为.flow的光流文件

将 .flo文件读取为 ndarray
首先读取 header,然后读取宽和高,最后读取 flow
注意fromfile的用法即可

def read_flow(filename: str) -> np.ndarray:

    f = open(filename, 'rb')

    header = f.read(4)
    if header.decode("utf-8") != 'PIEH':
        raise Exception('Flow file header does not contain PIEH')

    width = np.fromfile(f, np.int32, 1).squeeze()
    height = np.fromfile(f, np.int32, 1).squeeze()

    flow = np.fromfile(f, np.float32, width * height * 2)
    flow = flow.reshape((height, width, 2))

    return flow.astype(np.float32)

你可能感兴趣的:(图像处理算法,python,机器学习,算法)