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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "haader.h"
  2. #include <time.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <iomanip>
  8. using namespace std;
  9. void print_usage(const char *argv_0) {
  10. cout << "Construct an HDR-image from a series of photographs\n";
  11. cout << "with different exposure times and then synthesize\n";
  12. cout << "new images from it.\n\n";
  13. cout << "Usage:\n";
  14. cout << argv_0 << " <image_path_1> <image_path_2> ...\n\n";
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18. if (argc >= 2) {
  19. haader::HdrImageStack stack;
  20. // read image files.
  21. bool ok = stack.read_from_files(argv + 1, argc - 1);
  22. if (ok) {
  23. clock_t start = clock();
  24. // construct and save average image
  25. {
  26. haader::Image img;
  27. stack.get_average_image(img);
  28. img.save_as_ppm_file("average.ppm");
  29. }
  30. // approximate inverse response function
  31. haader::InverseResponseFunction irf;
  32. bool irf_ok = stack.get_inverse_response_function(irf, 1000);
  33. {
  34. fstream s;
  35. s.open("irf.csv", std::ios::out);
  36. irf.to_csv_stream(s);
  37. }
  38. if (irf_ok) {
  39. haader::HdrImage hdr_img;
  40. hdr_img = stack.get_hdr_image(irf);
  41. // save logarithmic image of scene luminance values
  42. {
  43. haader::Image x = hdr_img.get_log_image();
  44. x.save_as_ppm_file("log_image.ppm");
  45. }
  46. // convert irf to response function
  47. haader::ResponseFunction rf;
  48. rf = irf.to_response_function(1024);
  49. {
  50. fstream s;
  51. s.open("rf.csv", std::ios::out);
  52. rf.to_csv_stream(s);
  53. }
  54. {
  55. haader::Image x = hdr_img.expose(0.001, rf, 1.0);
  56. x.save_as_ppm_file("test_1000.ppm");
  57. }
  58. {
  59. haader::Image x = hdr_img.expose(1.0 / 1024.0, rf, 1.0);
  60. x.save_as_ppm_file("test_1024.ppm");
  61. }
  62. // create images by a simulated exposure of the HDR image with the response function
  63. cout << "Expose..." << endl;
  64. for (int i = 0; i < 20; i++) {
  65. // exposure time
  66. double t = pow(2.0, (double)i * (-12.0 / 20.0));
  67. // create image
  68. haader::Image x = hdr_img.expose(t, rf, 1.0);
  69. // save image
  70. stringstream ss;
  71. ss << "expose_" << setw(5) << setfill('0') << i << ".ppm";
  72. string path = ss.str();
  73. x.save_as_ppm_file(path.c_str());
  74. cout << "\nexposure time: " << t << " sec" << endl;
  75. cout << "save as " << path << endl;
  76. }
  77. }
  78. clock_t end = clock();
  79. double seconds = (end - start) / (double)CLOCKS_PER_SEC;
  80. cout << "\nsecs: " << seconds << endl;
  81. }
  82. } else {
  83. print_usage(argv[0]);
  84. }
  85. return 0;
  86. }