I am trying to get some HDR related functions working with OpenCV-Python: specifically I'm trying to reproduce the OpenCV C++ HDR tutorial. Unfortunately, the resulting hdr image/array comes out completely white (all values are Inf). Here is an MCVE. 1.jpg, 2.jpg, 3.jpg are all 870 × 580 RGB (Internal RGB KODAK sRGB Display) JPG images with exposure times of 1/3200, 1/800, and 1/200 respectively. I've tested this with 2 other JPG image sets now, one of which is available on Wikimedia.
>>> import cv2
>>> import numpy as np
>>>
>>> img = cv2.imread("1.jpg")
>>> img2 = cv2.imread("2.jpg")
>>> img3 = cv2.imread("3.jpg")
>>>
>>> images = np.array([img, img2, img3])
>>> times = np.array([1.0/3200,1.0/800,1.0/200])
>>>
>>> merger = cv2.createMergeDebevec()
>>> hdr = merger.process(images, times)
>>> hdr
array([[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]],
[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]],
[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]],
...,
[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]],
[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]],
[[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf],
...,
[ inf, inf, inf],
[ inf, inf, inf],
[ inf, inf, inf]]], dtype=float32)
An interesting thing to note is that the "times" array is modified after the merger.process call
>>> times
array([-8.07090609, -6.68461173, -5.29831737])
I am using OpenCV version:
>>> cv2.__version__
'3.0.0'
The merger.process call has a signature as follows:
>>> import inspect
>>> inspect.getdoc(merger.process)
'process(src, times, response[, dst]) -> dst or process(src, times[, dst]) -> dst'
I managed to get it working with the help of Velimir's answer. My issue was that I had to construct the array of images in descending order of EVs. While Velimir's answer does what I need, I'm making this a separate answer because I want to emphasize that the times
array represents the exposure times instead of the EVs. I've also added the tonemapping method that should be applied after building the radiance map.
import cv2
import numpy as np
img = cv2.imread("bright.jpg") # Exposure time 1/8
img2 = cv2.imread("normal.jpg") # Exposure time 1/13
img3 = cv2.imread("dark.jpg") # Exposure time 1/15
images = [img, img2, img3]
times = np.array([1/8.,1/13.,1/15.])
merger = cv2.createMergeDebevec()
hdr = merger.process(images, times)
tonemap = cv2.createTonemapDurand(2.2)
tonemapped_image = tonemap.process(hdr)
cv2.imwrite('tonemapped_image.jpg', tonemapped_image * 255)
The sample images are from http://ttic.uchicago.edu/~cotter/projects/hdr_tools/
Bright Image
Normal Image