extract_stats_images_all.cpp 11 KB

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