extract_stats_images_all.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = 150;
  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. unsigned nfiles = 10000;
  144. std::string outputFolder;
  145. for (int i = 1; i < argc; ++i) {
  146. if (!strcmp(argv[i], "--folder") || !strcmp(argv[i], "-folder")) {
  147. folderName = argv[++i];
  148. } else if (!strcmp(argv[i], "--bwidth") || !strcmp(argv[i], "-bwidth")) {
  149. blockHeight = atoi(argv[++i]);
  150. } else if (!strcmp(argv[i], "--bheight") || !strcmp(argv[i], "-bheight")) {
  151. blockWidth = atoi(argv[++i]);
  152. } else if (!strcmp(argv[i], "--output") || !strcmp(argv[i], "-output")) {
  153. outputFolder = argv[++i];
  154. } else if (!strcmp(argv[i], "--nfiles") || !strcmp(argv[i], "-nfiles")) {
  155. nfiles = atoi(argv[++i]);
  156. }
  157. }
  158. // create outputs directory
  159. mkdir(outputFolder.c_str(), 0755);
  160. auto elements = split(folderName, '/');
  161. std::string sceneName = elements.at(elements.size() - 1);
  162. for (int i = 0; i < estimators.size(); i++) {
  163. mkdir((outputFolder + "/" + estimators[i]).c_str(), 0755);
  164. mkdir((outputFolder + "/" + estimators[i] + "/" + sceneName).c_str(), 0755);
  165. }
  166. // get all files path
  167. std::vector<std::string> imagesPath;
  168. for (const auto & entry : std::filesystem::directory_iterator(folderName)){
  169. std::string imageName = entry.path().string();
  170. if (rawls::HasExtension(imageName, ".rawls") || rawls::HasExtension(imageName, ".rawls_20")){
  171. imagesPath.push_back(imageName);
  172. }
  173. }
  174. if (imagesPath.size() != nfiles) {
  175. return 0;
  176. }
  177. std::sort(imagesPath.begin(), imagesPath.end());
  178. std::tuple<unsigned, unsigned, unsigned> data = rawls::getDimensionsRAWLS(imagesPath.at(0));
  179. unsigned outputWidth = std::get<0>(data);
  180. unsigned outputHeight = std::get<1>(data);
  181. unsigned nbChanels = std::get<2>(data);
  182. std::vector<float*> outputBuffers;
  183. std::vector<std::string> outputFiles;
  184. std::vector<std::string> selectedEstimators;
  185. // new buffer size as new output buffer image (default 3 channels)
  186. for (int i = 0; i < estimators.size(); i++) {
  187. std::string outputFile = outputFolder + "/" + estimators[i] + "/" + sceneName + "/" + sceneName + ".rawls";
  188. std::ifstream ifile;
  189. ifile.open(outputFile);
  190. if(!ifile) {
  191. // create new buffer entry
  192. selectedEstimators.push_back(estimators[i]);
  193. outputFiles.push_back(outputFile);
  194. outputBuffers.push_back(new float[outputHeight * outputWidth * nbChanels]);
  195. } else {
  196. ifile.close();
  197. }
  198. }
  199. // get all tiles to apply
  200. unsigned nWidth = ceil(outputWidth / (float)blockWidth);
  201. unsigned nHeight = ceil(outputHeight / (float)blockHeight);
  202. std::vector<Tile> tiles;
  203. for (unsigned i = 0; i < nWidth; i++) {
  204. for (unsigned j = 0; j < nHeight; j++) {
  205. unsigned x1 = i * blockWidth;
  206. unsigned y1 = j * blockHeight;
  207. unsigned x2 = i * blockWidth + blockWidth;
  208. unsigned y2 = j * blockHeight + blockHeight;
  209. x2 = x2 > outputWidth ? outputWidth: x2;
  210. y2 = y2 > outputHeight ? outputHeight: y2;
  211. Point p1 = {x1, y1};
  212. Point p2 = {x2, y2};
  213. Tile tile = {p1, p2};
  214. tiles.push_back(tile);
  215. }
  216. }
  217. unsigned nsamples = imagesPath.size();
  218. unsigned nloop = tiles.size() * nsamples;
  219. unsigned nloopCounter = 0;
  220. for (unsigned t_index = 0; t_index < tiles.size(); t_index++){
  221. Tile tile = tiles.at(t_index);
  222. //std::cout << "Tile: (" << tile.p1.x << ", " << tile.p1.y << ")" << " => " << "(" << tile.p2.x << ", " << tile.p2.y << ")" << std::endl;
  223. unsigned nvalues = (tile.p2.x - tile.p1.x) * (tile.p2.y - tile.p1.y) * 3;
  224. std::vector<std::vector<float>> rgbValues(nvalues);
  225. for (unsigned i = 0; i < nsamples; i++) {
  226. try {
  227. float* RGBpixels = rawls::getPixelsRAWLS(imagesPath.at(i));
  228. unsigned index = 0;
  229. for (int y = tile.p1.y; y < tile.p2.y; ++y) {
  230. for (int x = tile.p1.x; x < tile.p2.x; ++x) {
  231. rgbValues.at(index).push_back(RGBpixels[3 * (y * outputWidth + x) + 0]);
  232. rgbValues.at(index + 1).push_back(RGBpixels[3 * (y * outputWidth + x) + 1]);
  233. rgbValues.at(index + 2).push_back(RGBpixels[3 * (y * outputWidth + x) + 2]);
  234. index += 3;
  235. }
  236. }
  237. delete RGBpixels;
  238. } catch(std::exception& e){
  239. std::cout << "Error occurs when reading file" << std::endl;
  240. }
  241. // display progress
  242. nloopCounter += 1;
  243. writeProgress(nloopCounter / (float)nloop);
  244. }
  245. for (int i = 0; i < outputFiles.size(); i++) {
  246. // extract stat and add predicted value into output buffer
  247. unsigned index = 0;
  248. for (int y = tile.p1.y; y < tile.p2.y; ++y) {
  249. for (int x = tile.p1.x; x < tile.p2.x; ++x) {
  250. outputBuffers.at(i)[3 * (y * outputWidth + x) + 0] = getEstimator(selectedEstimators.at(i), rgbValues.at(index + 0));
  251. outputBuffers.at(i)[3 * (y * outputWidth + x) + 1] = getEstimator(selectedEstimators.at(i), rgbValues.at(index + 1));
  252. outputBuffers.at(i)[3 * (y * outputWidth + x) + 2] = getEstimator(selectedEstimators.at(i), rgbValues.at(index + 2));
  253. index += 3;
  254. }
  255. }
  256. }
  257. }
  258. // Save here new rawls image
  259. std::string comments = rawls::getCommentsRAWLS(imagesPath.at(0));
  260. for (int i = 0; i < outputFiles.size(); i++) {
  261. // construct specific outfile name
  262. bool success = rawls::saveAsRAWLS(outputWidth, outputHeight, nbChanels, comments, outputBuffers[i], outputFiles[i]);
  263. if (success) {
  264. std::cout << "New image saved into " << outputFiles[i] << std::endl;
  265. }
  266. else
  267. {
  268. std::cout << "Error while saving current image " << outputFiles[i] << std::endl;
  269. }
  270. delete outputBuffers[i];
  271. }
  272. }