ClassFlowAlignment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "ClassFlowAlignment.h"
  2. #include "ClassFlowMakeImage.h"
  3. #include "ClassFlow.h"
  4. #include "CRotateImage.h"
  5. #include "esp_log.h"
  6. #include "ClassLogFile.h"
  7. #include "../../include/defines.h"
  8. static const char *TAG = "ALIGN";
  9. // #define DEBUG_DETAIL_ON
  10. void ClassFlowAlignment::SetInitialParameter(void)
  11. {
  12. initalrotate = 0;
  13. anz_ref = 0;
  14. initialmirror = false;
  15. use_antialiasing = false;
  16. initialflip = false;
  17. SaveAllFiles = false;
  18. namerawimage = "/sdcard/img_tmp/raw.jpg";
  19. FileStoreRefAlignment = "/sdcard/config/align.txt";
  20. ListFlowControll = NULL;
  21. AlignAndCutImage = NULL;
  22. ImageBasis = NULL;
  23. ImageTMP = NULL;
  24. previousElement = NULL;
  25. disabled = false;
  26. SAD_criteria = 0.05;
  27. }
  28. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow*>* lfc)
  29. {
  30. SetInitialParameter();
  31. ListFlowControll = lfc;
  32. for (int i = 0; i < ListFlowControll->size(); ++i)
  33. {
  34. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  35. {
  36. ImageBasis = ((ClassFlowMakeImage*) (*ListFlowControll)[i])->rawImage;
  37. }
  38. }
  39. if (!ImageBasis) // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
  40. {
  41. ESP_LOGD(TAG, "CImageBasis had to be created");
  42. ImageBasis = new CImageBasis(namerawimage);
  43. }
  44. }
  45. bool ClassFlowAlignment::ReadParameter(FILE* pfile, string& aktparamgraph)
  46. {
  47. std::vector<string> splitted;
  48. int suchex = 40;
  49. int suchey = 40;
  50. int alg_algo = 0;
  51. aktparamgraph = trim(aktparamgraph);
  52. if (aktparamgraph.size() == 0)
  53. if (!this->GetNextParagraph(pfile, aktparamgraph))
  54. return false;
  55. if (aktparamgraph.compare("[Alignment]") != 0) //Paragraph does not fit MakeImage
  56. return false;
  57. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  58. {
  59. splitted = ZerlegeZeile(aktparamgraph);
  60. if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1))
  61. {
  62. if (toUpper(splitted[1]) == "TRUE")
  63. initialflip = true;
  64. }
  65. if ((toUpper(splitted[0]) == "INITIALMIRROR") && (splitted.size() > 1))
  66. {
  67. if (toUpper(splitted[1]) == "TRUE")
  68. initialmirror = true;
  69. }
  70. if (((toUpper(splitted[0]) == "INITALROTATE") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
  71. {
  72. this->initalrotate = std::stod(splitted[1]);
  73. }
  74. if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1))
  75. {
  76. suchex = std::stod(splitted[1]);
  77. }
  78. if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1))
  79. {
  80. suchey = std::stod(splitted[1]);
  81. }
  82. if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1))
  83. {
  84. if (toUpper(splitted[1]) == "TRUE")
  85. use_antialiasing = true;
  86. }
  87. if ((splitted.size() == 3) && (anz_ref < 2))
  88. {
  89. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  90. References[anz_ref].target_x = std::stod(splitted[1]);
  91. References[anz_ref].target_y = std::stod(splitted[2]);
  92. anz_ref++;
  93. }
  94. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  95. {
  96. if (toUpper(splitted[1]) == "TRUE")
  97. SaveAllFiles = true;
  98. }
  99. if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1))
  100. {
  101. #ifdef DEBUG_DETAIL_ON
  102. std::string zw2 = "Alignment mode selected: " + splitted[1];
  103. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  104. #endif
  105. if (toUpper(splitted[1]) == "HIGHACCURACY")
  106. alg_algo = 1;
  107. if (toUpper(splitted[1]) == "FAST")
  108. alg_algo = 2;
  109. }
  110. }
  111. for (int i = 0; i < anz_ref; ++i)
  112. {
  113. References[i].search_x = suchex;
  114. References[i].search_y = suchey;
  115. References[i].fastalg_SAD_criteria = SAD_criteria;
  116. References[i].alignment_algo = alg_algo;
  117. #ifdef DEBUG_DETAIL_ON
  118. std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
  119. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  120. #endif
  121. }
  122. LoadReferenceAlignmentValues();
  123. return true;
  124. }
  125. string ClassFlowAlignment::getHTMLSingleStep(string host)
  126. {
  127. string result;
  128. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  129. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  130. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  131. return result;
  132. }
  133. bool ClassFlowAlignment::doFlow(string time)
  134. {
  135. if (!ImageTMP)
  136. {
  137. ImageTMP = new CImageBasis(ImageBasis);
  138. if (!ImageTMP)
  139. {
  140. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate ImageTMP -> Exec this round aborted!");
  141. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  142. return false;
  143. }
  144. }
  145. delete AlignAndCutImage;
  146. AlignAndCutImage = new CAlignAndCutImage(ImageBasis, ImageTMP);
  147. if (!AlignAndCutImage)
  148. {
  149. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  150. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  151. return false;
  152. }
  153. CRotateImage rt(AlignAndCutImage, ImageTMP, initialflip);
  154. if (initialflip)
  155. {
  156. int _zw = ImageBasis->height;
  157. ImageBasis->height = ImageBasis->width;
  158. ImageBasis->width = _zw;
  159. }
  160. if (initialmirror)
  161. {
  162. ESP_LOGD(TAG, "do mirror");
  163. rt.Mirror();
  164. if (SaveAllFiles)
  165. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
  166. }
  167. if ((initalrotate != 0) || initialflip)
  168. {
  169. if (use_antialiasing)
  170. rt.RotateAntiAliasing(initalrotate);
  171. else
  172. rt.Rotate(initalrotate);
  173. if (SaveAllFiles)
  174. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  175. }
  176. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  177. {
  178. SaveReferenceAlignmentValues();
  179. }
  180. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  181. if (SaveAllFiles)
  182. {
  183. if (initialflip)
  184. {
  185. int _zw = ImageTMP->width;
  186. ImageTMP->width = ImageTMP->height;
  187. ImageTMP->height = _zw;
  188. }
  189. DrawRef(ImageTMP);
  190. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  191. }
  192. // must be deleted to have memory space for loading tflite
  193. delete ImageTMP;
  194. ImageTMP = NULL;
  195. LoadReferenceAlignmentValues();
  196. return true;
  197. }
  198. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  199. {
  200. FILE* pFile;
  201. std::string zwtime, zwvalue;
  202. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  203. if (strlen(zwtime.c_str()) == 0)
  204. {
  205. time_t rawtime;
  206. struct tm* timeinfo;
  207. char buffer[80];
  208. time(&rawtime);
  209. timeinfo = localtime(&rawtime);
  210. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  211. zwtime = std::string(buffer);
  212. }
  213. fputs(zwtime.c_str(), pFile);
  214. fputs("\n", pFile);
  215. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  216. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
  217. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
  218. fputs(zwvalue.c_str(), pFile);
  219. fputs("\n", pFile);
  220. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  221. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
  222. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
  223. fputs(zwvalue.c_str(), pFile);
  224. fputs("\n", pFile);
  225. fclose(pFile);
  226. }
  227. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  228. {
  229. FILE* pFile;
  230. char zw[1024];
  231. string zwvalue;
  232. std::vector<string> splitted;
  233. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  234. if (pFile == NULL)
  235. return false;
  236. fgets(zw, 1024, pFile);
  237. ESP_LOGD(TAG, "%s", zw);
  238. fgets(zw, 1024, pFile);
  239. splitted = ZerlegeZeile(std::string(zw), " \t");
  240. if (splitted.size() < 6)
  241. {
  242. fclose(pFile);
  243. return false;
  244. }
  245. References[0].fastalg_x = stoi(splitted[0]);
  246. References[0].fastalg_y = stoi(splitted[1]);
  247. References[0].fastalg_SAD = stof(splitted[2]);
  248. References[0].fastalg_min = stoi(splitted[3]);
  249. References[0].fastalg_max = stoi(splitted[4]);
  250. References[0].fastalg_avg = stof(splitted[5]);
  251. fgets(zw, 1024, pFile);
  252. splitted = ZerlegeZeile(std::string(zw));
  253. if (splitted.size() < 6)
  254. {
  255. fclose(pFile);
  256. return false;
  257. }
  258. References[1].fastalg_x = stoi(splitted[0]);
  259. References[1].fastalg_y = stoi(splitted[1]);
  260. References[1].fastalg_SAD = stof(splitted[2]);
  261. References[1].fastalg_min = stoi(splitted[3]);
  262. References[1].fastalg_max = stoi(splitted[4]);
  263. References[1].fastalg_avg = stof(splitted[5]);
  264. fclose(pFile);
  265. /*#ifdef DEBUG_DETAIL_ON
  266. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  267. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  268. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  269. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  270. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  271. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  272. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  273. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  274. #endif*/
  275. return true;
  276. }
  277. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  278. {
  279. if (_zw->ImageOkay())
  280. {
  281. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  282. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  283. }
  284. }