johancc
johancc

Reputation: 65

OpenCV Gamma Correction exr

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

Upvotes: 2

Views: 1376

Answers (1)

johancc
johancc

Reputation: 65

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)

Upvotes: 1

Related Questions