When shooting with a smartphone or digital camera in a dark room with strong contrast such as dusk, night view, backlight, or outside light, the bright part may be crushed white, or the dark part may be crushed black. Have you ever been disappointed? This is a phenomenon that occurs because the gradation in bright and dark areas is insufficient. This time, I will try to create a High Dynamic Range Image by synthesizing a bright image and a dark image in a good way.
Please refer to here for environment construction from now on. OpenCV 3 and Python 3 environment construction
Three JPEG images taken with two different exposures. In order to combine beautifully, it is recommended to fix the camera, use the same aperture, change only the exposure time, and shoot continuously.
Image processed by OpenCV.
hdr.py
# -*- coding: utf-8 -*-
import cv2
import numpy as np
#Read 3 image files
img_fn = ["img1.jpg ", "img2.jpg ", "img3.jpg "]
img_list = [cv2.imread(fn) for fn in img_fn]
#Set the exposure time for 3 images
exposure_times = np.array([0.2, 0.05, 0.0125], dtype=np.float32)
#HDR synthesis by Debevec method
merge_debvec = cv2.createMergeDebevec()
hdr_debvec = merge_debvec.process(img_list, times=exposure_times.copy())
tonemap1 = cv2.createTonemapDurand(gamma=2.2)
res_debvec = tonemap1.process(hdr_debvec.copy())
#HDR synthesis by Robertson method
merge_robertson = cv2.createMergeRobertson()
hdr_robertson = merge_robertson.process(img_list, times=exposure_times.copy())
tonemap2 = cv2.createTonemapDurand(gamma=2.2)
res_robertson = tonemap2.process(hdr_robertson.copy())
#HDR synthesis by Mertens method
merge_mertens = cv2.createMergeMertens()
res_mertens = merge_mertens.process(img_list)
#Convert to 8-bit data
res_debvec_8bit = np.clip(res_debvec*255, 0, 255).astype('uint8')
res_robertson_8bit = np.clip(res_robertson*255, 0, 255).astype('uint8')
res_mertens_8bit = np.clip(res_mertens*255, 0, 255).astype('uint8')
#Save image to file
cv2.imwrite("ldr_debvec.jpg ", res_debvec_8bit)
cv2.imwrite("ldr_robertson.jpg ", res_robertson_8bit)
cv2.imwrite("fusion_mertens.jpg ", res_mertens_8bit)
If the number of photos is 3, and the exposure is -2, 0, +2, it will be as follows.
img_fn = ["img1.jpg ", "img2.jpg ", "img3.jpg "]
exposure_times = np.array([0.2, 0.05, 0.0125], dtype=np.float32)
If you have 5 photos and the exposure is -2, -1, 0, +1, +2, change this part as follows.
img_fn = ["img1.jpg ", "img2.jpg ", "img3.jpg ", "img4.jpg ", "img5.jpg "]
exposure_times = np.array([0.2, 0.1, 0.05, 0.025, 0.0125], dtype=np.float32)
This time, we were able to synthesize a powerful image using the Martens method without any parameter adjustment. I tried various things with other sample images [1], but of the three, the Martens method was the best looking. .. As HDR related functions, OpenCV also has functions for adjusting the tone map (Drago, Durand, Reinhard, Mantiuk) and functions for calibration (Debevec, Robertson). I found that OpenCV can be used for automatic and fairly good composition, so I would like to use it in some cases.
Recommended Posts