当前位置: 首页 > 工具软件 > flow-core-x > 使用案例 >

*.flo to flow_x and flow_y

罗业
2023-12-01

Ref:

http://vision.middlebury.edu/flow/code/flow-code/README.txt

http://www.it1352.com/481373.html

http://www.open-open.com/lib/view/open1453163505917.html

http://blog.csdn.net/junmuzi/article/details/75671572



See flowIO.cpp for sample code for reading and writing .flo files.
Here's an excerpt from this file describing the flow file format:


// ".flo" file format used for optical flow evaluation
//
// Stores 2-band float image for horizontal (u) and vertical (v) flow components.
// Floats are stored in little-endian order.
// A flow value is considered "unknown" if either |u| or |v| is greater than 1e9.
//
//  bytes  contents
//
//  0-3     tag: "PIEH" in ASCII, which in little endian happens to be the float 202021.25
//          (just a sanity check that floats are represented correctly)
//  4-7     width as an integer
//  8-11    height as an integer
//  12-end  data (width*height*2*4 bytes total)
//          the float values for u and v, interleaved, in row order, i.e.,
//          u[row0,col0], v[row0,col0], u[row0,col1], v[row0,col1], ...
//



Python:

# cat flo2xyflow.py 

import numpy as np
import os
import sys
from scipy import misc
import pdb


# WARNING: this will work on little-endian architectures (eg Intel x86) only!
if '__main__' == __name__:
    if len(sys.argv) > 1:
        with open(sys.argv[1], 'rb') as f:
            magic = np.fromfile(f, np.float32, count=1)
            if 202021.25 != magic:
                print 'Magic number incorrect. Invalid .flo file'
            else:
                w = np.fromfile(f, np.int32, count=1)
                h = np.fromfile(f, np.int32, count=1)
                print 'Reading %d x %d flo file' % (w, h)
                data = np.fromfile(f, np.float32, count=2*w*h)
                # Reshape data into 3D array (columns, rows, bands)
                pdb.set_trace();
                data2d = np.resize(data, (w[0], h[0], 2));
                img_x = data2d[:, :, 0];
                img_y = data2d[:, :, 1];
                misc.imshow(img_x);
                misc.imshow(img_y);
                pdb.set_trace();
    else:

        print 'Specify a .flo file on the command line.'

=================================================

C:

#include "CCC/COMCV.h"
#include <fstream>
void writeFlow(Mat_<Point2f> &flow, string outpath)
{
ofstream out(outpath, ios_base::binary);


string tag = "PIEH";//文件标志
out.write(tag.c_str(), tag.size());
out.write((const char*)(&flow.cols), sizeof(int));//行数
out.write((const char*)(&flow.rows), sizeof(int));//列数


for (int i = 0; i < flow.rows; i++)
for (int j = 0; j < flow.cols; j++)
{
out.write((const char*)(&flow(i, j).x), sizeof(float));
out.write((const char*)(&flow(i, j).y), sizeof(float));
}
}


void readFlow(Mat_<Point2f> &flow, string path)
{
ifstream in(path, ios_base::binary);


string tag(4, '0');//文件标志
in.read((char*)tag.data(), 4);


int rows, cols;
in.read((char*)(&cols), sizeof(int));//行数
in.read((char*)(&rows), sizeof(int));//列数


flow.create(rows, cols);
for (int i = 0; i < flow.rows; i++)
for (int j = 0; j < flow.cols; j++)
{
in.read((char*)(&(flow(i, j).x)), sizeof(float));
in.read((char*)(&(flow(i, j).y)), sizeof(float));
}
}


 类似资料:

相关阅读

相关文章

相关问答