extract_stats_images.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <tuple>
  8. #include <cmath>
  9. #include <numeric>
  10. #include <algorithm>
  11. #include <filesystem>
  12. #include "rawls.h"
  13. struct Point {
  14. unsigned x;
  15. unsigned y;
  16. };
  17. struct Tile {
  18. Point p1;
  19. Point p2;
  20. };
  21. void writeProgress(float progress, bool moveUp = false){
  22. int barWidth = 200;
  23. if (moveUp){
  24. // move up line
  25. std::cout << "\e[A";
  26. std::cout.flush();
  27. }
  28. std::cout << "[";
  29. int pos = barWidth * progress;
  30. for (int i = 0; i < barWidth; ++i) {
  31. if (i < pos) std::cout << "=";
  32. else if (i == pos) std::cout << ">";
  33. else std::cout << " ";
  34. }
  35. std::cout << "] " << int(progress * 100.0) << " %\r";
  36. std::cout.flush();
  37. }
  38. float getEstimator(std::string estimator, std::vector<float> values) {
  39. if (estimator == "median") {
  40. std::sort(values.begin(), values.end());
  41. unsigned size = values.size();
  42. if (size % 2 == 0)
  43. {
  44. return (values[size / 2 - 1] + values[size / 2]) / 2;
  45. }
  46. else
  47. {
  48. return values[size / 2];
  49. }
  50. } else if (estimator == "mean") {
  51. return std::accumulate(values.begin(), values.end(), 0.0) / values.size();
  52. } else if (estimator == "var") {
  53. // Calculate the mean
  54. const float mean = std::accumulate(values.begin(), values.end(), 0.0) / values.size();
  55. // Now calculate the variance
  56. auto variance_func = [&mean](float accumulator, const float& val) {
  57. return accumulator + pow(val - mean, 2);
  58. };
  59. return std::accumulate(values.begin(), values.end(), 0.0, variance_func) / values.size();
  60. } else if (estimator == "std") {
  61. return sqrt(getEstimator("var", values));
  62. } else if (estimator == "skewness") {
  63. unsigned size = values.size();
  64. float mean = getEstimator("mean", values);
  65. float std = getEstimator("std", values);
  66. // Now calculate the sum of pow 3
  67. auto order3_func = [&mean, &size](float accumulator, const float& val) {
  68. return accumulator + pow(val - mean, 3);
  69. };
  70. float order3 = std::accumulate(values.begin(), values.end(), 0.0, order3_func);
  71. return order3 / ((size -1 ) * pow(std, 3));
  72. } else if (estimator == "kurtosis") {
  73. unsigned size = values.size();
  74. float mean = getEstimator("mean", values);
  75. // Now calculate the sum of pow 4
  76. auto order4_func = [&mean, &size](float accumulator, const float& val) {
  77. return accumulator + pow(val - mean, 4);
  78. };
  79. float order4 = std::accumulate(values.begin(), values.end(), 0.0, order4_func);
  80. // Now calculate the sum of pow 2
  81. auto order2_func = [&mean, &size](float accumulator, const float& val) {
  82. return accumulator + pow(val - pow(mean, 2), 2);
  83. };
  84. float order2 = std::accumulate(values.begin(), values.end(), 0.0, order2_func);
  85. return size * (order4 / order2);
  86. }
  87. // by default
  88. return 0.;
  89. }
  90. int main(int argc, char *argv[]){
  91. std::string folderName;
  92. std::string estimator;
  93. unsigned blockHeight;
  94. unsigned blockWidth;
  95. std::string outfileName;
  96. for (int i = 1; i < argc; ++i) {
  97. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  98. folderName = argv[++i];
  99. } else if (!strcmp(argv[i], "--estimator") || !strcmp(argv[i], "-estimator")) {
  100. estimator = argv[++i];
  101. } else if (!strcmp(argv[i], "--bwidth") || !strcmp(argv[i], "-bwidth")) {
  102. blockHeight = atoi(argv[++i]);
  103. } else if (!strcmp(argv[i], "--bheight") || !strcmp(argv[i], "-bheight")) {
  104. blockWidth = atoi(argv[++i]);
  105. } else if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-outfile")) {
  106. outfileName = argv[++i];
  107. }
  108. }
  109. std::vector<std::string> imagesPath;
  110. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  111. std::string imageName = entry.path().string();
  112. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  113. imagesPath.push_back(imageName);
  114. }
  115. }
  116. std::sort(imagesPath.begin(), imagesPath.end());
  117. std::tuple<unsigned, unsigned, unsigned> data = rawls::getDimensionsRAWLS(imagesPath.at(0));
  118. unsigned outputWidth = std::get<0>(data);
  119. unsigned outputHeight = std::get<1>(data);
  120. unsigned nbChanels = std::get<2>(data);
  121. // new buffer size as new output buffer image (default 3 channels)
  122. float* outputBuffer = new float[outputHeight * outputWidth * nbChanels];
  123. // get all tiles to apply
  124. unsigned nWidth = ceil(outputWidth / (float)blockWidth);
  125. unsigned nHeight = ceil(outputHeight / (float)blockHeight);
  126. std::vector<Tile> tiles;
  127. for (unsigned i = 0; i < nWidth; i++) {
  128. for (unsigned j = 0; j < nHeight; j++) {
  129. unsigned x1 = i * blockWidth;
  130. unsigned y1 = j * blockHeight;
  131. unsigned x2 = i * blockWidth + blockWidth;
  132. unsigned y2 = j * blockHeight + blockHeight;
  133. x2 = x2 > outputWidth ? outputWidth: x2;
  134. y2 = y2 > outputHeight ? outputHeight: y2;
  135. Point p1 = {x1, y1};
  136. Point p2 = {x2, y2};
  137. Tile tile = {p1, p2};
  138. tiles.push_back(tile);
  139. }
  140. }
  141. unsigned nsamples = imagesPath.size();
  142. unsigned nloop = tiles.size() * nsamples;
  143. unsigned nloopCounter = 0;
  144. for (unsigned t_index = 0; t_index < tiles.size(); t_index++){
  145. Tile tile = tiles.at(t_index);
  146. //std::cout << "Tile: (" << tile.p1.x << ", " << tile.p1.y << ")" << " => " << "(" << tile.p2.x << ", " << tile.p2.y << ")" << std::endl;
  147. unsigned nvalues = (tile.p2.x - tile.p1.x) * (tile.p2.y - tile.p1.y) * 3;
  148. std::vector<std::vector<float>> rgbValues(nvalues);
  149. for (unsigned i = 0; i < nsamples; i++) {
  150. try {
  151. float* RGBpixels = rawls::getPixelsRAWLS(imagesPath.at(i));
  152. unsigned index = 0;
  153. for (int y = tile.p1.y; y < tile.p2.y; ++y) {
  154. for (int x = tile.p1.x; x < tile.p2.x; ++x) {
  155. rgbValues.at(index).push_back(RGBpixels[3 * (y * outputWidth + x) + 0]);
  156. rgbValues.at(index + 1).push_back(RGBpixels[3 * (y * outputWidth + x) + 1]);
  157. rgbValues.at(index + 2).push_back(RGBpixels[3 * (y * outputWidth + x) + 2]);
  158. index += 3;
  159. }
  160. }
  161. delete RGBpixels;
  162. } catch(std::exception& e){
  163. std::cout << "Error occurs when reading file" << std::endl;
  164. }
  165. // display progress
  166. nloopCounter += 1;
  167. writeProgress(nloopCounter / (float)nloop);
  168. }
  169. // extract stat and add predicted value into output buffer
  170. unsigned index = 0;
  171. for (int y = tile.p1.y; y < tile.p2.y; ++y) {
  172. for (int x = tile.p1.x; x < tile.p2.x; ++x) {
  173. outputBuffer[3 * (y * outputWidth + x) + 0] = getEstimator(estimator, rgbValues.at(index + 0));
  174. outputBuffer[3 * (y * outputWidth + x) + 1] = getEstimator(estimator, rgbValues.at(index + 1));
  175. outputBuffer[3 * (y * outputWidth + x) + 2] = getEstimator(estimator, rgbValues.at(index + 2));
  176. index += 3;
  177. }
  178. }
  179. }
  180. // Save here new rawls image
  181. std::string comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  182. bool success = rawls::saveAsRAWLS(outputWidth, outputHeight, nbChanels, comments, outputBuffer, outfileName);
  183. if (success) {
  184. std::cout << "New image saved into " << outfileName << std::endl;
  185. }
  186. else
  187. {
  188. std::cout << "Error while saving current image " << outfileName << std::endl;
  189. }
  190. delete outputBuffer;
  191. }