stackercpp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "stackercpp.h"
  2. Stacker::Stacker()
  3. {
  4. Stacker::layers = 0;
  5. cv::setNumThreads(0);
  6. Stacker::stopwatch = clock();
  7. }
  8. void
  9. Stacker::add_frame(unsigned char *data, int width, int height)
  10. {
  11. stopwatch_start();
  12. Mat warp_matrix = Mat::eye(3, 3, CV_32F);
  13. Mat mat = Mat(height, width, CV_8UC3, data);
  14. Mat grayscale = Mat(height, width, CV_8UC1);
  15. cv::cvtColor(mat, grayscale, cv::COLOR_BGR2GRAY);
  16. stopwatch_mark("grayscale input");
  17. stopwatch_start();
  18. Scalar mean, stddev;
  19. meanStdDev(grayscale, mean, stddev);
  20. printf("mean: %f, dev: %f\n", mean[0], stddev[0]);
  21. if (mean[0] < 20) {
  22. return;
  23. }
  24. stopwatch_mark("filter");
  25. int number_of_iterations = 5;
  26. double termination_eps = 1e-10;
  27. TermCriteria criteria(TermCriteria::COUNT + TermCriteria::EPS, number_of_iterations, termination_eps);
  28. if (layers == 0) {
  29. // First image in the stack is used as the reference to align the next frames to
  30. Stacker::reference = grayscale;
  31. // Create black image to accumulate the average
  32. Stacker::stacked = Mat(height, width, CV_64FC3);
  33. Stacker::stacked.setTo(Scalar(0, 0, 0, 0));
  34. // Add first frame to the stack
  35. Stacker::stacked += mat;
  36. Stacker::layers += 1;
  37. } else {
  38. // All frames after the initial one are stacked
  39. Mat warped = Mat(Stacker::stacked.rows, Stacker::stacked.cols, CV_32FC1);
  40. stopwatch_start();
  41. cv::findTransformECC(grayscale, Stacker::reference, warp_matrix, MOTION_HOMOGRAPHY, criteria);
  42. stopwatch_mark("find alignment");
  43. stopwatch_start();
  44. warpPerspective(mat, warped, warp_matrix, warped.size(), INTER_LINEAR);
  45. stopwatch_mark("warp image");
  46. // Add the warped image to the stack
  47. Stacker::stacked += warped;
  48. Stacker::layers += 1;
  49. }
  50. }
  51. Mat
  52. Stacker::postprocess_mat(Mat input)
  53. {
  54. stopwatch_start();
  55. Mat sharpened;
  56. GaussianBlur(input, sharpened, Size(0, 0), 1.7);
  57. addWeighted(input, 2.5, sharpened, -1.5, 0, sharpened);
  58. stopwatch_mark("sharpen");
  59. stopwatch_start();
  60. Mat lab;
  61. cvtColor(sharpened, lab, COLOR_BGR2Lab);
  62. std::vector<cv::Mat> lab_planes(3);
  63. cv::split(lab, lab_planes);
  64. stopwatch_mark("to Lab");
  65. /*
  66. stopwatch_start();
  67. cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
  68. clahe->setClipLimit(2);
  69. clahe->setTilesGridSize(Size(8, 8));
  70. cv::Mat dst;
  71. clahe->apply(lab_planes[0], dst);
  72. dst.copyTo(lab_planes[0]);
  73. stopwatch_mark("clahe");
  74. */
  75. stopwatch_start();
  76. Mat result;
  77. cv::merge(lab_planes, lab);
  78. cvtColor(lab, result, COLOR_Lab2BGR);
  79. stopwatch_mark("to RGB");
  80. return result;
  81. }
  82. char *
  83. Stacker::get_result()
  84. {
  85. // Complete the averaging and go back to an 8-bit image
  86. stopwatch_start();
  87. Stacker::stacked.convertTo(Stacker::stacked, CV_8U, 1. / Stacker::layers);
  88. stopwatch_mark("average");
  89. // Run the final-frame postprocessing
  90. Mat result = postprocess_mat(Stacker::stacked);
  91. // Convert mat to bytes
  92. size_t size = result.total() * result.elemSize();
  93. char *data = (char *) malloc(size);
  94. std::memcpy(data, result.data, size * sizeof(char));
  95. return data;
  96. }
  97. char *
  98. Stacker::postprocess(unsigned char *data, int width, int height)
  99. {
  100. // Convert bytes to mat
  101. Mat mat = Mat(height, width, CV_8UC3, data);
  102. // Run the final-frame postprocessing
  103. Mat result = postprocess_mat(mat);
  104. // Convert mat to bytes
  105. size_t size = result.total() * result.elemSize();
  106. char *outdata = (char *) malloc(size);
  107. std::memcpy(outdata, result.data, size * sizeof(char));
  108. return outdata;
  109. }
  110. void
  111. Stacker::stopwatch_start()
  112. {
  113. Stacker::stopwatch = clock();
  114. }
  115. void
  116. Stacker::stopwatch_mark(const char *name)
  117. {
  118. printf("[%.1fms] %s\n", float(clock() - Stacker::stopwatch) / CLOCKS_PER_SEC * 1000, name);
  119. }