I have exr formatted single channel (luminance or 'Y') images which I am pre-processing with "OpenEXR" library and having them as a single channel np.array. I want to read this np.array using opencv2 library to access the different algorithms offered by it. I am not able to achieve this directly and have to resort to save this np.array as an "jpeg/png" image and then reading this image using opencv2 as follows-
# Read EXR file-
img_exr_file = OpenEXR.InputFile(path_to_files + "some_img.exr")
# Get information about the read files-
img_exr_file.header()
'''
{'channels': {'Y': FLOAT (1, 1)},
'compression': PIZ_COMPRESSION,
'dataWindow': (0, 0) - (1887, 2999),
'displayWindow': (0, 0) - (1887, 2999),
'exposure_time_0_0': None,
'gain_0_0': None,
'lineOrder': INCREASING_Y,
'pixelAspectRatio': 1.0,
'screenWindowCenter': (0.0, 0.0),
'screenWindowWidth': 1.0,
'timestamp': None}
'''
print(f"Bytes of 'Y' channel data for exr file = {len(img_exr_file.channel('Y'))}")
# Bytes of 'Y' channel data for exr file = 22656000
# Compute image size-
data_window = img_exr_file.header()['dataWindow']
w, h = data_window.max.x - data_window.min.x + 1, data_window.max.y - data_window.min.y + 1
print(f"exr file, width = {w} & height = {h}")
# exr file, width = 1888 & height = 3000
# Read the 3 color channels as 32-bit floats-
FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
print(f"Attempt to read/process the 3 clor channels as 32-bit {FLOAT}")
# Attempt to read/process the 3 clor channels as 32-bit FLOAT
# Our data has only the luminence 'Y' channel-
Y_data = [array.array('f', img_exr_file.channel('Y', FLOAT)).tolist()]
# Sanity check-
len(Y_data[0])
# 5664000
# Copy to numpy array-
x = np.array(Y_data)
x = x[0, :]
x = x.reshape(h, w)
# Sanity check-
x.shape
# (3000, 1888)
# Final processed np array-
x.shape
# (3000, 1888)
# Save as image-
plt.imsave('exr_processed_image.jpeg', x)
# Read processed exr file as jpeg-
image = cv2.imread("exr_processed_image.jpeg")
# do something with 'image'...
How to access 'x' directly using 'cv2' without the intermediate saving step?