Firstly, I want to explain step by step what I am trying to do:
- I have game motion vectors stored in .exr file (have no information about pixel format, likely float32), however I am provided with function that decodes velocity.
- I want to save them to .flo format, and then I use flowiz to check if they are saved correctly Current result can be seen in image I provided. The flow is saved in 1/4th of image and duplicated (I am also sure that this flow is correct, but for some reason scaled and duplicated).
Here is my code to read .exr: iio -imageiov3
def read_exr_np(path):
return np.array(iio.imread(str(path),index=0)[..., :3]).transpose((2,0,1))
then I use this code to convert function:
def generate_flow_from_exr(exr_path):
flow_exr=read_exr_np(exr_path)
flow = decode_velocity(flow_exr,[1440,2560])
write_flow(flow,2560,1440,save_path+str(i-1)+".flo")
os.system("python -m flowiz "+save_path+str(i-1)+".flo")
Write function:
def write_flow(tensor, width, height, filename):
f = open(filename, 'wb')
magic = np.array([202021.25], dtype=np.float32)
w = np.array([width], dtype=np.int32)
h = np.array([height], dtype=np.int32)
magic.tofile(f)
w.tofile(f)
h.tofile(f)
flow = np.asarray(tensor).astype('float32')
flow.tofile(f)
f.close()
I tried various changes in way of reading image, as I expect that's what causes the issue. Tried to use opencv to read .exr :
im2=cv2.imread(path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH).transpose(2,0,1)
#also tried this:
im2=cv2.imread(path,-1)
but they both return some corrupted data, the final flow is totally random. Also read this: reading middlebury 'flow' files with python (bytes array & numpy) And That's the format I want to write, however I cannot read exr in a way they provided, as its not saved such way.
EDIT1: Here is a link to example .exr files: https://drive.google.com/drive/folders/1sKRkPdNR-B27bPOaph09kwEQaw-1_w_o?usp=sharing