Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (155 loc) · 6.5 KB
/
Copy pathmain.cpp
File metadata and controls
193 lines (155 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright(C) 2026, IDS Imaging Development Systems GmbH.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <iomanip>
#include <iostream>
#include <string>
#include <peak_icv/peak_icv.hpp>
#ifndef DATA_PATH
# error "Define DATA_PATH to the examples data folder"
#endif
// ---------------------------------------------------------------------------------------------------------------------
// DECLARATIONS
// ---------------------------------------------------------------------------------------------------------------------
namespace
{
std::vector<uint32_t> GetExposureTimesCalibrationMicroSeconds();
std::vector<uint32_t> GetExposureTimesProcessMicroSeconds();
std::string GetCalibrationImageFilePath();
std::string GetProcessingImageFilePath();
std::string GetToneMappedLdrImageFilePath();
std::string GetHdrImageFilePath();
std::vector<peak::icv::Image> ReadImagesFromDir(
const std::string& directoryPath, const std::vector<uint32_t>& exposureTimes);
void SetImagesExposureMetaData(std::vector<peak::icv::Image>& images, const std::vector<uint32_t>& exposureTimes);
std::vector<peak::icv::Image> ReadCalibrationImagesFromDir(const std::string& directoryPath);
std::vector<peak::icv::Image> ReadProcessingImagesFromDir(const std::string& directoryPath);
} // namespace
// ---------------------------------------------------------------------------------------------------------------------
// MAIN
// ---------------------------------------------------------------------------------------------------------------------
int main()
{
try
{
peak::icv::library::Init();
const peak::icv::ImageWriter writer;
std::cout << "Initialize HDR" << std::endl;
peak::icv::HDR hdr;
std::cout << "Loading calibration images from " << GetCalibrationImageFilePath() << std::endl;
auto calibrationImages = ReadCalibrationImagesFromDir(GetCalibrationImageFilePath());
// It is important to set the exposure time of the images to the images metadata
SetImagesExposureMetaData(calibrationImages, GetExposureTimesCalibrationMicroSeconds());
std::cout << "Estimate camera response curve" << std::endl;
hdr.EstimateResponseCurve(calibrationImages);
std::cout << "Loading processing images from " << GetProcessingImageFilePath() << std::endl;
auto processingImages = ReadProcessingImagesFromDir(GetProcessingImageFilePath());
// It is important to set the exposure time of the images to the images metadata
SetImagesExposureMetaData(processingImages, GetExposureTimesProcessMicroSeconds());
// It is also possible to skip the Calibrate method, then calibration is done on first call of Process method
std::cout << "Process HDR image" << std::endl;
const auto hdrImage = hdr.Process(processingImages);
writer.Write(GetHdrImageFilePath(), hdrImage);
std::cout << "HDR image saved to " << GetHdrImageFilePath() << std::endl;
std::cout << "Initialize tone mapping" << std::endl;
peak::icv::DragoToneMapping toneMapping;
std::cout << "Tone mapping of HDR image" << std::endl;
const auto ldrImage = toneMapping.Process(hdrImage);
writer.Write(GetToneMappedLdrImageFilePath(), ldrImage);
std::cout << "Tone mapped ldr image saved to " << GetToneMappedLdrImageFilePath() << std::endl;
peak::icv::library::Exit();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}
// ---------------------------------------------------------------------------------------------------------------------
// INPUT/OUTPUT UTILITIES
// ---------------------------------------------------------------------------------------------------------------------
namespace
{
std::vector<uint32_t> GetExposureTimesCalibrationMicroSeconds()
{
return { 502, 803, 1'296, 2'085, 3'353, 5'400, 8'685, 13'982, 22'501 };
}
std::vector<uint32_t> GetExposureTimesProcessMicroSeconds()
{
return { 502, 1'296, 3'353, 8'685, 22'501 };
}
#ifndef DATA_PATH
# error "Define DATA_PATH to the examples data folder"
#endif
std::string GetCalibrationImageFilePath()
{
return DATA_PATH + std::string("/hdr_from_file/calibration");
}
std::string GetProcessingImageFilePath()
{
return DATA_PATH + std::string("/hdr_from_file/processing");
}
std::string GetToneMappedLdrImageFilePath()
{
return "tone_mapped_ldr_image.png";
}
std::string GetHdrImageFilePath()
{
return "hdr_image.tiff";
}
std::string ExposureToString(const double exposure)
{
std::ostringstream ss;
ss << std::fixed << std::setprecision(3) << exposure;
std::string s = ss.str();
for (char& c : s)
{
if (c == '.')
{
c = '_';
}
}
return s;
}
std::vector<peak::icv::Image> ReadImagesFromDir(
const std::string& directoryPath, const std::vector<uint32_t>& exposureTimes)
{
std::vector<peak::icv::Image> images;
images.reserve(exposureTimes.size());
for (double exposureTime : exposureTimes)
{
const auto filePath = directoryPath + "/image_" + ExposureToString(exposureTime / 1'000) + "_ms.png";
images.emplace_back(filePath);
}
return images;
}
void SetImagesExposureMetaData(std::vector<peak::icv::Image>& images, const std::vector<uint32_t>& exposureTimes)
{
for (size_t i = 0; i < exposureTimes.size(); ++i)
{
peak::common::Metadata metaData;
metaData.SetValueByKey<peak::common::MetadataKey::DeviceExposureTime>(exposureTimes[i]);
images[i].SetMetadata(metaData);
}
}
std::vector<peak::icv::Image> ReadCalibrationImagesFromDir(const std::string& directoryPath)
{
return ReadImagesFromDir(directoryPath, GetExposureTimesCalibrationMicroSeconds());
}
std::vector<peak::icv::Image> ReadProcessingImagesFromDir(const std::string& directoryPath)
{
return ReadImagesFromDir(directoryPath, GetExposureTimesProcessMicroSeconds());
}
} // namespace