Create a High Dynamic Range Image (HDR) with OpenCV and Python (Mertens, Robertson, Debevec)

Introduction

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.

environment

Please refer to here for environment construction from now on. OpenCV 3 and Python 3 environment construction

Image before processing

サムネイル.png

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.

After HDR processing

Image processed by OpenCV.

program

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)

in conclusion

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

Create a High Dynamic Range Image (HDR) with OpenCV and Python (Mertens, Robertson, Debevec)
Create a striped illusion with gamma correction for Python3 and openCV3
Create a dummy image with Python + PIL.
Notes on HDR and RAW image processing with Python
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Image editing with python OpenCV
Create a directory with python
Create a web surveillance camera with Raspberry Pi and OpenCV
Let's create a PRML diagram with Python, Numpy and matplotlib.
Create a simple video analysis tool with python wxpython + openCV
Shining life with Python and OpenCV
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Draw a watercolor illusion with edge detection in Python3 and openCV3
Find image similarity with Python + OpenCV
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Create your own virtual camera with Python + OpenCV and apply original effects
[Python] Create a linebot to write a name and age on an image
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Capturing images with Pupil, python and OpenCV
A memo with Python2.7 and Python3 on CentOS
Image processing with Python & OpenCV [Tone Curve]
Create and decrypt Caesar cipher with python
Image acquisition from camera with Python + OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create a word frequency counter with Python 3.4
Light image processing with Python x OpenCV
Create a Python image in Django without a dummy image file and test the image upload
Create a simple reception system with the Python serverless framework Chalice and Twilio
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Hello World and face detection with OpenCV 4.3 + Python
Create a frame with transparent background with tkinter [Python]
Building a python environment with virtualenv and direnv
I tried "differentiating" the image with Python + OpenCV
Create polka dot wallpaper with Python Image Library
Create a LINE BOT with Minette for Python
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
How to crop an image with Python + OpenCV
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Create a web map using Python and GDAL
Create a color bar with Python + Qt (PySide)
I tried "binarizing" the image with Python + OpenCV
Create an image with characters in python (Japanese)
Steps to create a Twitter bot with python
Launch a web server with Python and Flask
Create a decision tree from 0 with Python (1. Overview)