A tool to construct HDR-images from a series of exposures.

haader.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef HAADER_ILUCF4Z0
  2. #define HAADER_ILUCF4Z0
  3. #include <vector>
  4. #include "image.h"
  5. namespace haader {
  6. class ResponseFunction {
  7. friend class InverseResponseFunction;
  8. public:
  9. // Returns a pixel value (0-255) given the logarithm of a luminance value.
  10. int lookup(double value) const;
  11. private:
  12. // minimum log of luminance value
  13. double m_min_value;
  14. // maximum log of luminance value
  15. double m_max_value;
  16. // Precomputed value:
  17. // 1.0 / (m_max_value - m_min_value)
  18. double m_recip_range;
  19. // Response function is implemented as a lookup table of with 256 values
  20. std::vector<int> m_lookup_table;
  21. };
  22. class InverseResponseFunction {
  23. friend class HdrImageStack;
  24. public:
  25. // Return logarithm of luminance given a pixel value (0-255)
  26. double lookup(int index) const;
  27. // The inverse of the lookup function:
  28. // y == lookup(x) -> x == inverse_lookup(y)
  29. // O(n) complexity, proportional to size of lookup table.
  30. int inverse_lookup(double value) const;
  31. // Construct a ResponseFunction object that allows efficient O(1) inverse_lookup.
  32. // bins: Size of the lookup table for the response function
  33. ResponseFunction to_response_function(int bins=1024);
  34. private:
  35. // Returns true if a monotonically increasing lookup table without missing values
  36. // can be constructed.
  37. bool repair();
  38. // Remove missing values by linear interpolation of neighboring values
  39. void fill_zeros();
  40. // Apply binomial low pass filter
  41. void low_pass_filter();
  42. // Returns true if values are monotonically increasing
  43. bool is_monotonic();
  44. // Inverse response function is implemented as a lookup table with 256 values
  45. std::vector<double> m_lookup_table;
  46. };
  47. // High Dynamic Range Image.
  48. // Stores the logarithm of the scene luminance for each pixel/channel in a high resolution.
  49. class HdrImage {
  50. friend class HdrImageStack;
  51. public:
  52. HdrImage();
  53. // Construct low dynamic range image with the logarithm of the scene luminance.
  54. Image get_log_image();
  55. // Construct low dynamic range image by a simulated exposure with a given response function.
  56. // exposure_time: Exposure time in seconds
  57. // compression:
  58. // A value > 1.0 will compress the luminace values and give more contrast.
  59. // A value < 1.0 will reduce the contrast but lower the risk of
  60. // under-/overexposure in parts of the image.
  61. Image expose(double exposure_time, const ResponseFunction &rf, double compression=1.0);
  62. private:
  63. // Pixel values (logarithm of scene luminance). The channels are interleaved (RGBRGBRGB...).
  64. std::vector<double> m_image_data;
  65. // Width in pixels
  66. unsigned int m_width;
  67. // Height in pixels
  68. unsigned int m_height;
  69. // Number of channels (usually 3 for RGB images)
  70. unsigned int m_components;
  71. };
  72. // A stack of images/photographs of the same scene, with the same resolution but different exposure times.
  73. class HdrImageStack {
  74. public:
  75. HdrImageStack();
  76. // Read images from the given array of file paths
  77. bool read_from_files(char * const* file_paths, int number_of_paths);
  78. bool get_average_image(Image &output);
  79. bool get_average_image_slow(Image &output);
  80. // Approximate the inverse response function from samples of random pixel locations.
  81. // num_samples: Number of samples
  82. // Returns true if a well-formed inverse response function could be found.
  83. bool get_inverse_response_function(InverseResponseFunction &output, unsigned int num_samples);
  84. // Return a HDR-image given an inverse response function
  85. HdrImage get_hdr_image(const InverseResponseFunction &invRespFunc);
  86. private:
  87. // Sort the images by exposure time
  88. void sort_by_exposure();
  89. // The images
  90. std::vector<Image> m_images;
  91. };
  92. }
  93. #endif // HAADER_ILUCF4Z0