extract_stats_images_all.cpp 10 KB

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