4

How can I gamma correct a 32bit .exr file to look like the desired result in the image of this question? Basically converting from liner to sRGB somehow? I got this code(and modified it a bit) from convert EXR to JPEG using ImageIO and Python

I don't quite understand those values (65535), and changing them will result in weird output images.

import os
os.environ['OPENCV_IO_ENABLE_OPENEXR']='True'

import numpy as np
import cv2
im=cv2.imread("D:\CG_CONTENT\HDRIS\HDRI_BROWSER\Concrete_Office_Outside_sm.exr",-1)
im=im*65535
im[im>65535]=65535
im=np.uint16(im)
cv2.imwrite("D:\CG_CONTENT\HDRIS\HDRI_BROWSER\Concrete_Office_Outside_sm_test8.png",im)

On the left of this image I have the output from the code, but I need something like the image on the right. output vs desired output

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
johancc
  • 87
  • 8
  • Update: I tried tonemapping (as described here: https://learnopencv.com/high-dynamic-range-hdr-imaging-using-opencv-cpp-python/), which works great for some images but in others is too dark or too bright, and for some outputs an error with the tonemapping process. – johancc Jun 26 '22 at 06:38
  • New update: the error I am getting are from exr files with an alpha channel. Basically can't process the tonemapping in images with alpha channel. – johancc Jun 26 '22 at 07:29
  • Found how to remove the alpha but still read the full range of pixels: https://stackoverflow.com/questions/35902302/discarding-alpha-channel-from-images-stored-as-numpy-arrays – johancc Jun 26 '22 at 07:41

1 Answers1

1

So in order to properly converted a linear image to sRGB I had to use a transfer function, which for sRGB, can be simplified with gamma.

Here's what I ended up using, and worked very well.

    #Read image (.exr)
    im=cv2.imread(newPath,-1)
    
    #Remove alpha if present
    im = im[:,:,:3]    
    
    #resize image
    dsize = (400,200)
    ldrDrago = cv2.resize(im, dsize)    
        
    #Convert to sRGB
    ldrDrago = ldrDrago ** (1 / 2.2)
    
    
    #Write jpeg
    cv2.imwrite(filename+".jpg", ldrDrago * 255)
johancc
  • 87
  • 8