Ask Your Question

does cv2.imwrite support float16 (half)?

asked 2018-06-18 02:54:05 -0600

mohit_dessai gravatar image

updated 2020-10-25 06:44:58 -0600

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 = 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?

edit retag flag offensive close merge delete


Also am I using the imwrite function properly?

it should be like:

cv.imwrite('cropped_tree.exr', img, params)

(a list of key-value pairs)

berak gravatar imageberak ( 2018-06-18 03:19:02 -0600 )edit

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.

mohit_dessai gravatar imagemohit_dessai ( 2018-06-18 03:37:23 -0600 )edit

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 ?

berak gravatar imageberak ( 2018-06-18 03:41:16 -0600 )edit

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])

mohit_dessai gravatar imagemohit_dessai ( 2018-06-18 03:55:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-21 23:56:22 -0600

mohit_dessai gravatar image

cv.imwrite('cropped_tree_half.exr', img, [cv.IMWRITE_EXR_TYPE, cv.IMWRITE_EXR_TYPE_HALF]) solved the problem of writing exr with half

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-06-18 02:54:05 -0600

Seen: 7,186 times

Last updated: Jun 21 '18