main.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "cuda_helper.h"
  2. #include "hdr_synthesis.h"
  3. #include <cuda_profiler_api.h>
  4. #include <nppi_color_conversion.h>
  5. #include <nppi_filtering_functions.h>
  6. #include <opencv2/core/cuda.hpp>
  7. #include <opencv2/imgcodecs.hpp>
  8. #include <boost/iostreams/device/mapped_file.hpp>
  9. #include <iostream>
  10. #include <chrono>
  11. static constexpr auto image_width = 2448;
  12. static constexpr auto image_height = 2048;
  13. cv::Mat download_image(void *ptr, size_t pitch, size_t width, size_t height, int type) {
  14. auto gpu_mat = cv::cuda::GpuMat{(int) height, (int) width, type, ptr, pitch};
  15. cv::Mat mat;
  16. gpu_mat.download(mat);
  17. return mat;
  18. }
  19. struct image_buffer {
  20. void *pyr_image_f32, *pyr_weight_f32;
  21. size_t image_pitch, weight_pitch;
  22. };
  23. int main() {
  24. auto path_a = "/home/tpx/project/HDRSynthesis/data/phantom2_5ms.raw";
  25. auto path_b = "/home/tpx/project/HDRSynthesis/data/phantom2_20ms.raw";
  26. using boost::iostreams::mapped_file;
  27. auto img_file_a = mapped_file{path_a, boost::iostreams::mapped_file_base::readonly};
  28. auto img_file_b = mapped_file{path_b, boost::iostreams::mapped_file_base::readonly};
  29. auto hdr = hdr_synthesizer{image_width, image_height};
  30. void *buf_a, *buf_b;
  31. hdr.malloc_buffer(&buf_a);
  32. hdr.malloc_buffer(&buf_b);
  33. hdr.preprocess_image(buf_a, (uint8_t *) img_file_a.const_data());
  34. hdr.preprocess_image(buf_b, (uint8_t *) img_file_b.const_data());
  35. auto img_hdr_dev = cv::cuda::GpuMat{image_height, image_width, CV_8UC3};
  36. hdr.merge_image(buf_b, buf_a, img_hdr_dev.cudaPtr(), img_hdr_dev.step1());
  37. // for (int i = 0; i < 8; ++i) {
  38. // auto start_ts = std::chrono::system_clock::now();
  39. // cudaDeviceSynchronize();
  40. // hdr.preprocess_image(buf_a, (uint8_t *) img_file_a.const_data());
  41. // hdr.preprocess_image(buf_b, (uint8_t *) img_file_b.const_data());
  42. // cudaDeviceSynchronize();
  43. // std::cout << std::chrono::duration_cast<std::chrono::microseconds>(
  44. // std::chrono::system_clock::now() - start_ts).count() << std::endl;
  45. // }
  46. //
  47. // cudaProfilerStart();
  48. // hdr.preprocess_image(buf_a, (uint8_t *) img_file_a.const_data());
  49. // cudaProfilerStop();
  50. //
  51. auto real_ptr = (image_buffer *) buf_b;
  52. auto host_rgb_a = download_image(img_hdr_dev.cudaPtr(), img_hdr_dev.step1(),
  53. image_width, image_height, CV_8UC3);
  54. // auto host_rgb_a = download_image((char *) real_ptr->pyr_image_f32, real_ptr->image_pitch,
  55. // image_width, image_height, CV_32FC3);
  56. // void *ptr;
  57. // size_t pitch;
  58. // hdr.test_func(&ptr, &pitch);
  59. // auto host_rgb_a = download_image(ptr, pitch,
  60. // image_width, image_height, CV_32FC3);
  61. double min_val, max_val;
  62. cv::minMaxLoc(host_rgb_a, &min_val, &max_val);
  63. std::cout << min_val << " " << max_val << " " << cv::mean(host_rgb_a) << std::endl;
  64. cv::imwrite("test.bmp", host_rgb_a);
  65. return 0;
  66. }