*.flo to flow_x and flow_y

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));
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值