瀏覽代碼

Generate histograms for HDR and LDR images

Johannes Hofmann 7 年之前
父節點
當前提交
01766736b7
共有 4 個檔案被更改,包括 87 行新增15 行删除
  1. 42
    12
      haader.cpp
  2. 11
    2
      haader.h
  3. 26
    1
      image.cpp
  4. 8
    0
      image.h

+ 42
- 12
haader.cpp 查看文件

8
 
8
 
9
 #include "haader.h"
9
 #include "haader.h"
10
 
10
 
11
+#define max_number(a, b) (a) > (b) ? (a) : (b)
12
+#define min_number(a, b) (a) < (b) ? (a) : (b)
13
+
11
 using namespace std;
14
 using namespace std;
12
 using namespace haader;
15
 using namespace haader;
13
 
16
 
262
     return output;
265
     return output;
263
 }
266
 }
264
 
267
 
268
+Histogram HdrImage::histogram(unsigned int number_of_bins) const {
269
+    Histogram hist;
270
+
271
+    hist.m_bins.resize(number_of_bins, 0);
272
+
273
+    if (m_image_data.size() == 0) {
274
+        return hist;
275
+    }
276
+
277
+    hist.m_min_value = m_image_data[0];
278
+    hist.m_max_value = m_image_data[0];
279
+
280
+    for (unsigned int i = 0; i < m_image_data.size(); i++) {
281
+        double val = m_image_data[i];
282
+        hist.m_min_value = min_number(hist.m_min_value, val);
283
+        hist.m_max_value = max_number(hist.m_max_value, val);
284
+    }
285
+
286
+    double scale = (double)number_of_bins / (hist.m_max_value - hist.m_min_value);
287
+
288
+    for (unsigned int i = 0; i < m_image_data.size(); i++) {
289
+        double val = m_image_data[i];
290
+        int bin = (val - hist.m_min_value) * scale;
291
+        bin = bin < 0 ? 0 : (bin >= (int)number_of_bins ? (number_of_bins - 1) : bin);
292
+        hist.m_bins[bin]++;
293
+    }
294
+
295
+    hist.m_max_freq = 0;
296
+    for (unsigned int i = 0; i < number_of_bins; i++) {
297
+        hist.m_max_freq = max_number(hist.m_max_freq, hist.m_bins[i]);
298
+    }
299
+
300
+    return hist;
301
+}
302
+
265
 
303
 
266
 HdrImageStack::HdrImageStack() {
304
 HdrImageStack::HdrImageStack() {
267
 }
305
 }
283
                      << file_paths[i]
321
                      << file_paths[i]
284
                      << "\")"
322
                      << "\")"
285
                      << endl;
323
                      << endl;
324
+                m_images.clear();
286
                 return false;
325
                 return false;
287
             }
326
             }
288
             if (m_images[i].get_exposure_time() == 0.0) {
327
             if (m_images[i].get_exposure_time() == 0.0) {
290
                      << file_paths[i]
329
                      << file_paths[i]
291
                      << "\")"
330
                      << "\")"
292
                      << endl;
331
                      << endl;
332
+                m_images.clear();
293
                 return false;
333
                 return false;
294
             }
334
             }
295
         } else {
335
         } else {
336
+            m_images.clear();
296
             return false;
337
             return false;
297
         }
338
         }
298
     }
339
     }
475
         }
516
         }
476
     }
517
     }
477
 
518
 
478
-    if (output.repair()) {
479
-        cout << "Inverse response function" << endl;
480
-        cout << endl;
481
-        for (unsigned int i = 0; i < 256; i++) {
482
-            cout << i << "," << output.m_lookup_table[i] << endl;
483
-        }
484
-        cout << endl;
485
-
486
-        return true;
487
-    } else {
488
-        return false;
489
-    }
519
+    return output.repair();
490
 }
520
 }
491
 
521
 
492
 HdrImage HdrImageStack::get_hdr_image(const InverseResponseFunction &invRespFunc) {
522
 HdrImage HdrImageStack::get_hdr_image(const InverseResponseFunction &invRespFunc) {

+ 11
- 2
haader.h 查看文件

27
             // 1.0 / (m_max_value - m_min_value)
27
             // 1.0 / (m_max_value - m_min_value)
28
             double m_recip_range;
28
             double m_recip_range;
29
 
29
 
30
-            // Response function is implemented as a lookup table of with 256 values
30
+            // Response function is implemented with a lookup table
31
             std::vector<int> m_lookup_table;
31
             std::vector<int> m_lookup_table;
32
     };
32
     };
33
 
33
 
69
             std::vector<double> m_lookup_table;
69
             std::vector<double> m_lookup_table;
70
     };
70
     };
71
 
71
 
72
+    class Histogram {
73
+        public:
74
+            std::vector<unsigned int> m_bins;
75
+            double m_min_value;
76
+            double m_max_value;
77
+            unsigned int m_max_freq;
78
+    };
79
+
72
     // High Dynamic Range Image.
80
     // High Dynamic Range Image.
73
     // Stores the logarithm of the scene luminance for each pixel/channel in a high resolution.
81
     // Stores the logarithm of the scene luminance for each pixel/channel in a high resolution.
74
     class HdrImage {
82
     class HdrImage {
88
             //     under-/overexposure in parts of the image.
96
             //     under-/overexposure in parts of the image.
89
             Image expose(double exposure_time, const ResponseFunction &rf, double compression=1.0);
97
             Image expose(double exposure_time, const ResponseFunction &rf, double compression=1.0);
90
 
98
 
99
+            Histogram histogram(unsigned int number_of_bins) const;
100
+
91
         private:
101
         private:
92
             // Pixel values (logarithm of scene luminance). The channels are interleaved (RGBRGBRGB...).
102
             // Pixel values (logarithm of scene luminance). The channels are interleaved (RGBRGBRGB...).
93
             std::vector<double> m_image_data;
103
             std::vector<double> m_image_data;
131
             // The images
141
             // The images
132
             std::vector<Image> m_images;
142
             std::vector<Image> m_images;
133
     };
143
     };
134
-
135
 }
144
 }
136
 
145
 
137
 #endif // HAADER_ILUCF4Z0
146
 #endif // HAADER_ILUCF4Z0

+ 26
- 1
image.cpp 查看文件

11
 #include "image.h"
11
 #include "image.h"
12
 #include "exif.h"
12
 #include "exif.h"
13
 
13
 
14
+#define max_number(a, b) (a) > (b) ? (a) : (b)
15
+#define min_number(a, b) (a) < (b) ? (a) : (b)
16
+
14
 using namespace std;
17
 using namespace std;
15
 using namespace haader;
18
 using namespace haader;
16
 
19
 
62
         cerr << "Can not read \"" << file_path << "\" as an image." << endl;
65
         cerr << "Can not read \"" << file_path << "\" as an image." << endl;
63
         return false;
66
         return false;
64
     }
67
     }
65
-    
68
+
66
     stbi_image_free(data);
69
     stbi_image_free(data);
67
 }
70
 }
68
 
71
 
104
     }
107
     }
105
 }
108
 }
106
 
109
 
110
+LdrHistogram Image::histogram() const {
111
+    LdrHistogram hist;
112
+
113
+    hist.m_bins.resize(256, 0);
114
+
115
+    if (m_image_data.size() == 0) {
116
+        return hist;
117
+    }
118
+
119
+    for (unsigned int i = 0; i < m_image_data.size(); i++) {
120
+        hist.m_bins[m_image_data[i]]++;
121
+    }
122
+
123
+    hist.m_max_freq = 0;
124
+    for (unsigned int i = 0; i < 256; i++) {
125
+        hist.m_max_freq = max_number(hist.m_max_freq, hist.m_bins[i]);
126
+    }
127
+
128
+    return hist;
129
+}
130
+
131
+
107
 unsigned int Image::get_width() const {
132
 unsigned int Image::get_width() const {
108
     return m_width;
133
     return m_width;
109
 }
134
 }

+ 8
- 0
image.h 查看文件

6
 
6
 
7
 namespace haader {
7
 namespace haader {
8
 
8
 
9
+    class LdrHistogram {
10
+        public:
11
+            std::vector<unsigned int> m_bins;
12
+            unsigned int m_max_freq;
13
+    };
14
+
9
     // A low dynamic range image/photograph with an associated exposure time.
15
     // A low dynamic range image/photograph with an associated exposure time.
10
     class Image {
16
     class Image {
11
         friend class HdrImageStack;
17
         friend class HdrImageStack;
19
             bool read_from_file(const char *file_path);
25
             bool read_from_file(const char *file_path);
20
             bool save_as_ppm_file(const char *file_path, bool ascii=false) const;
26
             bool save_as_ppm_file(const char *file_path, bool ascii=false) const;
21
 
27
 
28
+            LdrHistogram histogram() const;
29
+
22
             unsigned int get_width() const;
30
             unsigned int get_width() const;
23
             unsigned int get_height() const;
31
             unsigned int get_height() const;
24
             unsigned int get_components() const;
32
             unsigned int get_components() const;