1High Dynamic Range Imaging {#tutorial_hdr_imaging}
2==========================
3
4Introduction
5------------
6
7Today most digital images and imaging devices use 8 bits per channel thus limiting the dynamic range
8of the device to two orders of magnitude (actually 256 levels), while human eye can adapt to
9lighting conditions varying by ten orders of magnitude. When we take photographs of a real world
10scene bright regions may be overexposed, while the dark ones may be underexposed, so we can’t
11capture all details using a single exposure. HDR imaging works with images that use more that 8 bits
12per channel (usually 32-bit float values), allowing much wider dynamic range.
13
14There are different ways to obtain HDR images, but the most common one is to use photographs of the
15scene taken with different exposure values. To combine this exposures it is useful to know your
16camera’s response function and there are algorithms to estimate it. After the HDR image has been
17blended it has to be converted back to 8-bit to view it on usual displays. This process is called
18tonemapping. Additional complexities arise when objects of the scene or camera move between shots,
19since images with different exposures should be registered and aligned.
20
21In this tutorial we show how to generate and display HDR image from an exposure sequence. In our
22case images are already aligned and there are no moving objects. We also demonstrate an alternative
23approach called exposure fusion that produces low dynamic range image. Each step of HDR pipeline can
24be implemented using different algorithms so take a look at the reference manual to see them all.
25
26Exposure sequence
27-----------------
28
29![](images/memorial.png)
30
31Source Code
32-----------
33
34@include cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp
35
36Explanation
37-----------
38
39-#  **Load images and exposure times**
40    @code{.cpp}
41    vector<Mat> images;
42    vector<float> times;
43    loadExposureSeq(argv[1], images, times);
44    @endcode
45    Firstly we load input images and exposure times from user-defined folder. The folder should
46    contain images and *list.txt* - file that contains file names and inverse exposure times.
47
48    For our image sequence the list is following:
49    @code{.none}
50    memorial00.png 0.03125
51    memorial01.png 0.0625
52    ...
53    memorial15.png 1024
54    @endcode
55
56-#  **Estimate camera response**
57    @code{.cpp}
58    Mat response;
59    Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
60    calibrate->process(images, response, times);
61    @endcode
62    It is necessary to know camera response function (CRF) for a lot of HDR construction algorithms.
63    We use one of the calibration algorithms to estimate inverse CRF for all 256 pixel values.
64
65-#  **Make HDR image**
66@code{.cpp}
67Mat hdr;
68Ptr<MergeDebevec> merge_debevec = createMergeDebevec();
69merge_debevec->process(images, hdr, times, response);
70@endcode
71We use Debevec's weighting scheme to construct HDR image using response calculated in the previous
72item.
73
74-#  **Tonemap HDR image**
75    @code{.cpp}
76    Mat ldr;
77    Ptr<TonemapDurand> tonemap = createTonemapDurand(2.2f);
78    tonemap->process(hdr, ldr);
79    @endcode
80    Since we want to see our results on common LDR display we have to map our HDR image to 8-bit range
81    preserving most details. It is the main goal of tonemapping methods. We use tonemapper with
82    bilateral filtering and set 2.2 as the value for gamma correction.
83
84-#  **Perform exposure fusion**
85    @code{.cpp}
86    Mat fusion;
87    Ptr<MergeMertens> merge_mertens = createMergeMertens();
88    merge_mertens->process(images, fusion);
89    @endcode
90    There is an alternative way to merge our exposures in case when we don't need HDR image. This
91    process is called exposure fusion and produces LDR image that doesn't require gamma correction. It
92    also doesn't use exposure values of the photographs.
93
94-#  **Write results**
95    @code{.cpp}
96    imwrite("fusion.png", fusion * 255);
97    imwrite("ldr.png", ldr * 255);
98    imwrite("hdr.hdr", hdr);
99    @endcode
100    Now it's time to look at the results. Note that HDR image can't be stored in one of common image
101    formats, so we save it to Radiance image (.hdr). Also all HDR imaging functions return results in
102    [0, 1] range so we should multiply result by 255.
103
104Results
105-------
106
107### Tonemapped image
108
109![](images/ldr.png)
110
111### Exposure fusion
112
113![](images/fusion.png)
114