Create flowio.py
Browse files
flowio.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
python by: youngjung uh, Clova ML, Naver
|
3 |
+
contact: youngjung.uh@navercorp.com
|
4 |
+
date: 17 Dec 2018
|
5 |
+
|
6 |
+
-------------------------------------------------------------------
|
7 |
+
---- below comment came from the original (readFlowFile.m) -------
|
8 |
+
---- below comment came from the original (writeFlowFile.m) -------
|
9 |
+
-------------------------------------------------------------------
|
10 |
+
readFlowFile read a flow file FILENAME into 2-band image IMG
|
11 |
+
writeFlowFile writes a 2-band image IMG into flow file FILENAME
|
12 |
+
|
13 |
+
According to the c++ source code of Daniel Scharstein
|
14 |
+
Contact: schar@middlebury.edu
|
15 |
+
|
16 |
+
Author: Deqing Sun, Department of Computer Science, Brown University
|
17 |
+
Contact: dqsun@cs.brown.edu
|
18 |
+
$Date: 2007-10-31 15:36:40 (Wed, 31 Oct 2006) $
|
19 |
+
|
20 |
+
Copyright 2007, Deqing Sun.
|
21 |
+
|
22 |
+
All Rights Reserved
|
23 |
+
|
24 |
+
Permission to use, copy, modify, and distribute this software and its
|
25 |
+
documentation for any purpose other than its incorporation into a
|
26 |
+
commercial product is hereby granted without fee, provided that the
|
27 |
+
above copyright notice appear in all copies and that both that
|
28 |
+
copyright notice and this permission notice appear in supporting
|
29 |
+
documentation, and that the name of the author and Brown University not be used in
|
30 |
+
advertising or publicity pertaining to distribution of the software
|
31 |
+
without specific, written prior permission.
|
32 |
+
|
33 |
+
THE AUTHOR AND BROWN UNIVERSITY DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
34 |
+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
|
35 |
+
PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR OR BROWN UNIVERSITY BE LIABLE FOR
|
36 |
+
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
37 |
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
38 |
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
39 |
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
40 |
+
'''
|
41 |
+
|
42 |
+
import os
|
43 |
+
import numpy as np
|
44 |
+
from struct import pack, unpack
|
45 |
+
|
46 |
+
|
47 |
+
def readFlowFile(fname):
|
48 |
+
'''
|
49 |
+
args
|
50 |
+
fname (str)
|
51 |
+
return
|
52 |
+
flow (numpy array) numpy array of shape (height, width, 2)
|
53 |
+
'''
|
54 |
+
|
55 |
+
TAG_FLOAT = 202021.25 # check for this when READING the file
|
56 |
+
|
57 |
+
ext = os.path.splitext(fname)[1]
|
58 |
+
|
59 |
+
assert len(ext) > 0, ('readFlowFile: extension required in fname %s' % fname)
|
60 |
+
assert ext == '.flo', exit('readFlowFile: fname %s should have extension ''.flo''' % fname)
|
61 |
+
|
62 |
+
try:
|
63 |
+
fid = open(fname, 'rb')
|
64 |
+
except IOError:
|
65 |
+
print('readFlowFile: could not open %s', fname)
|
66 |
+
|
67 |
+
tag = unpack('f', fid.read(4))[0]
|
68 |
+
width = unpack('i', fid.read(4))[0]
|
69 |
+
height = unpack('i', fid.read(4))[0]
|
70 |
+
|
71 |
+
assert tag == TAG_FLOAT, ('readFlowFile(%s): wrong tag (possibly due to big-endian machine?)' % fname)
|
72 |
+
assert 0 < width and width < 100000, ('readFlowFile(%s): illegal width %d' % (fname, width))
|
73 |
+
assert 0 < height and height < 100000, ('readFlowFile(%s): illegal height %d' % (fname, height))
|
74 |
+
|
75 |
+
nBands = 2
|
76 |
+
|
77 |
+
# arrange into matrix form
|
78 |
+
flow = np.fromfile(fid, np.float32)
|
79 |
+
flow = flow.reshape(height, width, nBands)
|
80 |
+
|
81 |
+
fid.close()
|
82 |
+
|
83 |
+
return flow
|
84 |
+
|
85 |
+
def writeFlowFile(img, fname):
|
86 |
+
TAG_STRING = 'PIEH' # use this when WRITING the file
|
87 |
+
|
88 |
+
ext = os.path.splitext(fname)[1]
|
89 |
+
|
90 |
+
assert len(ext) > 0, ('writeFlowFile: extension required in fname %s' % fname)
|
91 |
+
assert ext == '.flo', exit('writeFlowFile: fname %s should have extension ''.flo''', fname)
|
92 |
+
|
93 |
+
height, width, nBands = img.shape
|
94 |
+
|
95 |
+
assert nBands == 2, 'writeFlowFile: image must have two bands'
|
96 |
+
|
97 |
+
try:
|
98 |
+
fid = open(fname, 'wb')
|
99 |
+
except IOError:
|
100 |
+
print('writeFlowFile: could not open %s', fname)
|
101 |
+
|
102 |
+
# write the header
|
103 |
+
# fid.write(TAG_STRING.encode(encoding='utf-8', errors='strict'))
|
104 |
+
# code = unpack('f', bytes(TAG_STRING, 'utf-8'))[0]
|
105 |
+
# fid.write(pack('f', code))
|
106 |
+
fid.write(bytes(TAG_STRING, 'utf-8'))
|
107 |
+
fid.write(pack('i', width))
|
108 |
+
fid.write(pack('i', height))
|
109 |
+
|
110 |
+
# arrange into matrix form
|
111 |
+
tmp = np.zeros((height, width*nBands), np.float32)
|
112 |
+
|
113 |
+
tmp[:, np.arange(width) * nBands] = img[:, :, 0]
|
114 |
+
tmp[:, np.arange(width) * nBands + 1] = np.squeeze(img[:, :, 1])
|
115 |
+
|
116 |
+
fid.write(bytes(tmp))
|
117 |
+
|
118 |
+
fid.close()
|