光流文件(.flo),numpy,图像(.png)相互转换——完全python实现

光流文件(.flo),numpy,图像(.png)相互转换——完全python实现

网上有很多都是借用imageLib进行实现,并不适合用来进行开发使用,这里完全使用python进行实现

1. 光流图像转numpy

import numpy as np

def load_flow_to_numpy(path):
    with open(path, 'rb') as f:
        magic = np.fromfile(f, np.float32, count=1)
        assert (202021.25 == magic), 'Magic number incorrect. Invalid .flo file'
        h = np.fromfile(f, np.int32, count=1)[0]
        w = np.fromfile(f, np.int32, count=1)[0]
        data = np.fromfile(f, np.float32, count=2 * w * h)
    data2D = np.resize(data, (w, h, 2))
    return data2D


if __name__ == '__main__':
    flo = load_flow_to_numpy('frame_0001.flo')
    print(flo.shape)  # (436, 1024, 2)

最终的输出结果为二维图像,分别代表着图像上每一个像素点之间的x和y坐标像素的偏移量

2. 光流图像转图像

import numpy as np
from matplotlib.colors import hsv_to_rgb
import matplotlib.pyplot as plt

def load_flow_to_numpy(path):
    with open(path, 'rb') as f:
        magic = np.fromfile(f, np.float32, count=1)
        assert (202021.25 == magic), 'Magic number incorrect. Invalid .flo file'
        h = np.fromfile(f, np.int32, count=1)[0]
        w = np.fromfile(f, np.int32, count=1)[0]
        data = np.fromfile(f, np.float32, count=2 * w * h)
    data2D = np.resize(data, (w, h, 2))
    return data2D


def load_flow_to_png(path):
    flow = load_flow_to_numpy(path)
    image = flow_to_image(flow)
    return image


def flow_to_image(flow, max_flow=256):
    if max_flow is not None:
        max_flow = max(max_flow, 1.)
    else:
        max_flow = np.max(flow)

    n = 8
    u, v = flow[:, :, 0], flow[:, :, 1]
    mag = np.sqrt(np.square(u) + np.square(v))
    angle = np.arctan2(v, u)
    im_h = np.mod(angle / (2 * np.pi) + 1, 1)
    im_s = np.clip(mag * n / max_flow, a_min=0, a_max=1)
    im_v = np.clip(n - im_s, a_min=0, a_max=1)
    im = hsv_to_rgb(np.stack([im_h, im_s, im_v], 2))
    return (im * 255).astype(np.uint8)


if __name__ == '__main__':
    image = load_flow_to_png('frame_0001.flo')
    plt.imshow(image)
    plt.show()

image-20211128164438337

剩余的转换关系以此类推就可以啦

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://quguai.cn/archives/guang-liu-wen-jian---f-l-o---n-u-m-p-y--tu-xiang---p-n-g--xiang-hu-zhuan-huan---wan-quan-p-y-t-h-o-n-shi-xian