does cv2.imwrite support float16 (half)?
import cv2 as cv
import random
import numpy as np
# Reading an existing exr file
img = cv.imread('Tree.exr', cv.IMREAD_UNCHANGED)
print (img.dtype) \\ this prints float32
img_h = np.float16(img) \\ hoping to typecast float32(float) to float16 (half)
print (img_h.dtype) \\ this prints float16
#cv.imwrite('cropped_tree.exr', img) #this works fine write float32 into exr
#cv.imwrite('cropped_tree.exr', img, cv.IMWRITE_EXR_TYPE_FLOAT) # this doesn't work, gives:
# cv.imwrite('cropped_tree.exr', img, cv.IMWRITE_EXR_TYPE_FLOAT)
# SystemError: <built-in function imwrite> returned NULL without setting an error
cv.imwrite('cropped_tree.exr', img_h, cv.IMWRITE_EXR_TYPE_HALF) # this doesn't work, gives :
# cv.imwrite('cropped_tree.exr', img_h, cv.IMWRITE_EXR_TYPE_HALF)
# TypeError: img data type = 23 is not supported
Python version = 3.6 numpy version = 1.14.5 opencv-python = 3.4.1.15 also tried on other pc with opencv = 3.4.1
the problem seems like opencv doesn't support fp16 even though documentation says so. Also am I using the imwrite function properly?
it should be like:
(a list of key-value pairs)
Hi @berak, Thanks for pointing that out. i didn't know that. Even after that change FLOAT works but HALF is still not working. Thanks though! let me know if you think that type casting is proper for fp16.
hmm, looking at the c++ code, it seems to convert it on it's own, internally. can you try to skip the
np.float16
, and feed it in as float32 ?whoa @berak, that did it i guess. file size is almost half size. not able to check if it worked, but atleast no errors. Thanks a lot! cv.imwrite('cropped_tree_half.exr', img, [cv.IMWRITE_EXR_TYPE, cv.IMWRITE_EXR_TYPE_HALF])