extract_stats_images.cpp 9.3 KB

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