-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfunc_pfm.py
114 lines (96 loc) · 4.8 KB
/
func_pfm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 17:56:02 2018
@author: shinyonsei2
"""
'''
Load a PFM file into a Numpy array. Note that it will have
a shape of H x W, not W x H. Returns a tuple containing the
loaded image and the scale factor from the file.
'''
import numpy as np
import sys
import re
############################################################################
# This file is part of the 4D Light Field Benchmark. #
# #
# This work is licensed under the Creative Commons #
# Attribution-NonCommercial-ShareAlike 4.0 International License. #
# To view a copy of this license, #
# visit http://creativecommons.org/licenses/by-nc-sa/4.0/. #
# #
# Authors: Katrin Honauer & Ole Johannsen #
# Contact: contact@lightfield-analysis.net #
# Website: www.lightfield-analysis.net #
# #
# The 4D Light Field Benchmark was jointly created by the University of #
# Konstanz and the HCI at Heidelberg University. If you use any part of #
# the benchmark, please cite our paper "A dataset and evaluation #
# methodology for depth estimation on 4D light fields". Thanks! #
# #
# @inproceedings{honauer2016benchmark, #
# title={A dataset and evaluation methodology for depth estimation on #
# 4D light fields}, #
# author={Honauer, Katrin and Johannsen, Ole and Kondermann, Daniel #
# and Goldluecke, Bastian}, #
# booktitle={Asian Conference on Computer Vision}, #
# year={2016}, #
# organization={Springer} #
# } #
# #
############################################################################
def write_pfm(data, fpath, scale=1, file_identifier=b'Pf', dtype="float32"):
# PFM format definition: http://netpbm.sourceforge.net/doc/pfm.html
data = np.flipud(data)
height, width = np.shape(data)[:2]
values = np.ndarray.flatten(np.asarray(data, dtype=dtype))
endianess = data.dtype.byteorder
print(endianess)
if endianess == '<' or (endianess == '=' and sys.byteorder == 'little'):
scale *= -1
with open(fpath, 'wb') as file:
file.write((file_identifier))
file.write(('\n%d %d\n' % (width, height)).encode())
file.write(('%d\n' % scale).encode())
file.write(values)
def read_pfm(fpath, expected_identifier="Pf"):
# PFM format definition: http://netpbm.sourceforge.net/doc/pfm.html
def _get_next_line(f):
next_line = f.readline().decode('utf-8').rstrip()
# ignore comments
while next_line.startswith('#'):
next_line = f.readline().rstrip()
return next_line
with open(fpath, 'rb') as f:
# header
identifier = _get_next_line(f)
if identifier != expected_identifier:
raise Exception('Unknown identifier. Expected: "%s", got: "%s".' % (expected_identifier, identifier))
try:
line_dimensions = _get_next_line(f)
dimensions = line_dimensions.split(' ')
width = int(dimensions[0].strip())
height = int(dimensions[1].strip())
except:
raise Exception('Could not parse dimensions: "%s". '
'Expected "width height", e.g. "512 512".' % line_dimensions)
try:
line_scale = _get_next_line(f)
scale = float(line_scale)
assert scale != 0
if scale < 0:
endianness = "<"
else:
endianness = ">"
except:
raise Exception('Could not parse max value / endianess information: "%s". '
'Should be a non-zero number.' % line_scale)
try:
data = np.fromfile(f, "%sf" % endianness)
data = np.reshape(data, (height, width))
data = np.flipud(data)
with np.errstate(invalid="ignore"):
data *= abs(scale)
except:
raise Exception('Invalid binary values. Could not create %dx%d array from input.' % (height, width))
return data