ClassFlowAlignment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. static const char *TAG = "ALIGN";
  8. bool AlignmentExtendedDebugging = true;
  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) // die Funktion Bilder aufnehmen existiert nicht --> muss erst erzeugt werden NUR ZU TESTZWECKEN
  40. {
  41. if (AlignmentExtendedDebugging) 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> zerlegt;
  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 passt nich zu MakeImage
  56. return false;
  57. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  58. {
  59. zerlegt = ZerlegeZeile(aktparamgraph);
  60. if ((toUpper(zerlegt[0]) == "FLIPIMAGESIZE") && (zerlegt.size() > 1))
  61. {
  62. if (toUpper(zerlegt[1]) == "TRUE")
  63. initialflip = true;
  64. }
  65. if ((toUpper(zerlegt[0]) == "INITIALMIRROR") && (zerlegt.size() > 1))
  66. {
  67. if (toUpper(zerlegt[1]) == "TRUE")
  68. initialmirror = true;
  69. }
  70. if (((toUpper(zerlegt[0]) == "INITALROTATE") || (toUpper(zerlegt[0]) == "INITIALROTATE")) && (zerlegt.size() > 1))
  71. {
  72. this->initalrotate = std::stod(zerlegt[1]);
  73. }
  74. if ((toUpper(zerlegt[0]) == "SEARCHFIELDX") && (zerlegt.size() > 1))
  75. {
  76. suchex = std::stod(zerlegt[1]);
  77. }
  78. if ((toUpper(zerlegt[0]) == "SEARCHFIELDY") && (zerlegt.size() > 1))
  79. {
  80. suchey = std::stod(zerlegt[1]);
  81. }
  82. if ((toUpper(zerlegt[0]) == "ANTIALIASING") && (zerlegt.size() > 1))
  83. {
  84. if (toUpper(zerlegt[1]) == "TRUE")
  85. use_antialiasing = true;
  86. }
  87. if ((zerlegt.size() == 3) && (anz_ref < 2))
  88. {
  89. References[anz_ref].image_file = FormatFileName("/sdcard" + zerlegt[0]);
  90. References[anz_ref].target_x = std::stod(zerlegt[1]);
  91. References[anz_ref].target_y = std::stod(zerlegt[2]);
  92. anz_ref++;
  93. }
  94. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  95. {
  96. if (toUpper(zerlegt[1]) == "TRUE")
  97. SaveAllFiles = true;
  98. }
  99. if ((toUpper(zerlegt[0]) == "ALIGNMENTALGO") && (zerlegt.size() > 1))
  100. {
  101. #ifdef DEBUG_DETAIL_ON
  102. std::string zw2 = "Alignmentmodus gewählt: " + zerlegt[1];
  103. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  104. #endif
  105. if (toUpper(zerlegt[1]) == "HIGHACCURACY")
  106. alg_algo = 1;
  107. if (toUpper(zerlegt[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 = "Alignmentmodus geschrieben: " + 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. ImageTMP = new CImageBasis(ImageBasis, 5);
  137. if (AlignAndCutImage)
  138. delete AlignAndCutImage;
  139. AlignAndCutImage = new CAlignAndCutImage(ImageBasis, ImageTMP);
  140. CRotateImage rt(AlignAndCutImage, ImageTMP, initialflip);
  141. if (initialflip)
  142. {
  143. int _zw = ImageBasis->height;
  144. ImageBasis->height = ImageBasis->width;
  145. ImageBasis->width = _zw;
  146. }
  147. if (initialmirror){
  148. ESP_LOGD(TAG, "do mirror");
  149. rt.Mirror();
  150. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
  151. }
  152. if ((initalrotate != 0) || initialflip)
  153. {
  154. if (use_antialiasing)
  155. rt.RotateAntiAliasing(initalrotate);
  156. else
  157. rt.Rotate(initalrotate);
  158. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  159. }
  160. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  161. {
  162. SaveReferenceAlignmentValues();
  163. }
  164. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  165. if (SaveAllFiles)
  166. {
  167. if (initialflip)
  168. {
  169. int _zw = ImageTMP->width;
  170. ImageTMP->width = ImageTMP->height;
  171. ImageTMP->height = _zw;
  172. }
  173. DrawRef(ImageTMP);
  174. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  175. }
  176. if (ImageTMP) // nuss gelöscht werden, um Speicherplatz für das Laden von tflite zu haben
  177. {
  178. delete ImageTMP;
  179. ImageTMP = NULL;
  180. }
  181. LoadReferenceAlignmentValues();
  182. return true;
  183. }
  184. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  185. {
  186. FILE* pFile;
  187. std::string zwtime, zwvalue;
  188. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  189. if (strlen(zwtime.c_str()) == 0)
  190. {
  191. time_t rawtime;
  192. struct tm* timeinfo;
  193. char buffer[80];
  194. time(&rawtime);
  195. timeinfo = localtime(&rawtime);
  196. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  197. zwtime = std::string(buffer);
  198. }
  199. fputs(zwtime.c_str(), pFile);
  200. fputs("\n", pFile);
  201. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  202. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
  203. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
  204. fputs(zwvalue.c_str(), pFile);
  205. fputs("\n", pFile);
  206. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  207. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
  208. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
  209. fputs(zwvalue.c_str(), pFile);
  210. fputs("\n", pFile);
  211. fclose(pFile);
  212. }
  213. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  214. {
  215. FILE* pFile;
  216. char zw[1024];
  217. string zwvalue;
  218. std::vector<string> zerlegt;
  219. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues01");
  220. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  221. if (pFile == NULL)
  222. return false;
  223. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues01");
  224. fgets(zw, 1024, pFile);
  225. ESP_LOGD(TAG, "%s", zw);
  226. // zwvalue = "LoadReferenceAlignmentValues Time: " + std::string(zw);
  227. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zwvalue);
  228. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues02");
  229. fgets(zw, 1024, pFile);
  230. zerlegt = ZerlegeZeile(std::string(zw), " \t");
  231. if (zerlegt.size() < 6)
  232. {
  233. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "Exit 01");
  234. fclose(pFile);
  235. return false;
  236. }
  237. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues03");
  238. References[0].fastalg_x = stoi(zerlegt[0]);
  239. References[0].fastalg_y = stoi(zerlegt[1]);
  240. References[0].fastalg_SAD = stof(zerlegt[2]);
  241. References[0].fastalg_min = stoi(zerlegt[3]);
  242. References[0].fastalg_max = stoi(zerlegt[4]);
  243. References[0].fastalg_avg = stof(zerlegt[5]);
  244. fgets(zw, 1024, pFile);
  245. zerlegt = ZerlegeZeile(std::string(zw));
  246. if (zerlegt.size() < 6)
  247. {
  248. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "Exit 02");
  249. fclose(pFile);
  250. return false;
  251. }
  252. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues03");
  253. References[1].fastalg_x = stoi(zerlegt[0]);
  254. References[1].fastalg_y = stoi(zerlegt[1]);
  255. References[1].fastalg_SAD = stof(zerlegt[2]);
  256. References[1].fastalg_min = stoi(zerlegt[3]);
  257. References[1].fastalg_max = stoi(zerlegt[4]);
  258. References[1].fastalg_avg = stof(zerlegt[5]);
  259. fclose(pFile);
  260. #ifdef DEBUG_DETAIL_ON
  261. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  262. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  263. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  264. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  265. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  266. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  267. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  268. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  269. #endif
  270. return true;
  271. }
  272. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  273. {
  274. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  275. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  276. }