ClassFlowAlignment.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "ClassFlowAlignment.h"
  2. #include "ClassFlowMakeImage.h"
  3. #include "ClassFlow.h"
  4. #include "CRotateImage.h"
  5. #include "ClassLogFile.h"
  6. bool AlignmentExtendedDebugging = true;
  7. // #define DEBUG_DETAIL_ON
  8. void ClassFlowAlignment::SetInitialParameter(void)
  9. {
  10. initalrotate = 0;
  11. anz_ref = 0;
  12. initialmirror = false;
  13. SaveAllFiles = false;
  14. namerawimage = "/sdcard/img_tmp/raw.jpg";
  15. FileStoreRefAlignment = "/sdcard/config/align.txt";
  16. ListFlowControll = NULL;
  17. AlignAndCutImage = NULL;
  18. ImageBasis = NULL;
  19. ImageTMP = NULL;
  20. previousElement = NULL;
  21. disabled = false;
  22. SAD_criteria = 0.05;
  23. }
  24. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow*>* lfc)
  25. {
  26. SetInitialParameter();
  27. ListFlowControll = lfc;
  28. for (int i = 0; i < ListFlowControll->size(); ++i)
  29. {
  30. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  31. {
  32. ImageBasis = ((ClassFlowMakeImage*) (*ListFlowControll)[i])->rawImage;
  33. }
  34. }
  35. if (!ImageBasis) // die Funktion Bilder aufnehmen existiert nicht --> muss erst erzeugt werden NUR ZU TESTZWECKEN
  36. {
  37. if (AlignmentExtendedDebugging) printf("CImageBasis musste erzeugt werden\n");
  38. ImageBasis = new CImageBasis(namerawimage);
  39. }
  40. }
  41. bool ClassFlowAlignment::ReadParameter(FILE* pfile, string& aktparamgraph)
  42. {
  43. std::vector<string> zerlegt;
  44. int suchex = 40;
  45. int suchey = 40;
  46. int alg_algo = 0;
  47. aktparamgraph = trim(aktparamgraph);
  48. if (aktparamgraph.size() == 0)
  49. if (!this->GetNextParagraph(pfile, aktparamgraph))
  50. return false;
  51. if (aktparamgraph.compare("[Alignment]") != 0) // Paragraph passt nich zu MakeImage
  52. return false;
  53. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  54. {
  55. zerlegt = ZerlegeZeile(aktparamgraph);
  56. if ((toUpper(zerlegt[0]) == "INITIALMIRROR") && (zerlegt.size() > 1))
  57. {
  58. if (toUpper(zerlegt[1]) == "TRUE")
  59. initialmirror = true;
  60. }
  61. if (((toUpper(zerlegt[0]) == "INITALROTATE") || (toUpper(zerlegt[0]) == "INITIALROTATE")) && (zerlegt.size() > 1))
  62. {
  63. this->initalrotate = std::stod(zerlegt[1]);
  64. }
  65. if ((toUpper(zerlegt[0]) == "SEARCHFIELDX") && (zerlegt.size() > 1))
  66. {
  67. suchex = std::stod(zerlegt[1]);
  68. }
  69. if ((toUpper(zerlegt[0]) == "SEARCHFIELDY") && (zerlegt.size() > 1))
  70. {
  71. suchey = std::stod(zerlegt[1]);
  72. }
  73. if ((zerlegt.size() == 3) && (anz_ref < 2))
  74. {
  75. References[anz_ref].image_file = FormatFileName("/sdcard" + zerlegt[0]);
  76. References[anz_ref].target_x = std::stod(zerlegt[1]);
  77. References[anz_ref].target_y = std::stod(zerlegt[2]);
  78. anz_ref++;
  79. }
  80. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  81. {
  82. if (toUpper(zerlegt[1]) == "TRUE")
  83. SaveAllFiles = true;
  84. }
  85. if ((toUpper(zerlegt[0]) == "ALIGNMENTALGO") && (zerlegt.size() > 1))
  86. {
  87. #ifdef DEBUG_DETAIL_ON
  88. std::string zw2 = "Alignmentmodus gewählt: " + zerlegt[1];
  89. LogFile.WriteToFile(zw2);
  90. #endif
  91. if (toUpper(zerlegt[1]) == "HIGHACCURACY")
  92. alg_algo = 1;
  93. if (toUpper(zerlegt[1]) == "FAST")
  94. alg_algo = 2;
  95. }
  96. }
  97. for (int i = 0; i < anz_ref; ++i)
  98. {
  99. References[i].search_x = suchex;
  100. References[i].search_y = suchey;
  101. References[i].fastalg_SAD_criteria = SAD_criteria;
  102. References[i].alignment_algo = alg_algo;
  103. #ifdef DEBUG_DETAIL_ON
  104. std::string zw2 = "Alignmentmodus geschrieben: " + std::to_string(alg_algo);
  105. LogFile.WriteToFile(zw2);
  106. #endif
  107. }
  108. LoadReferenceAlignmentValues();
  109. return true;
  110. }
  111. string ClassFlowAlignment::getHTMLSingleStep(string host)
  112. {
  113. string result;
  114. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  115. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  116. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  117. return result;
  118. }
  119. bool ClassFlowAlignment::doFlow(string time)
  120. {
  121. if (!ImageTMP)
  122. ImageTMP = new CImageBasis(ImageBasis, 5);
  123. if (AlignAndCutImage)
  124. delete AlignAndCutImage;
  125. AlignAndCutImage = new CAlignAndCutImage(ImageBasis, ImageTMP);
  126. CRotateImage rt(AlignAndCutImage, ImageTMP);
  127. if (initialmirror){
  128. printf("do mirror\n");
  129. rt.Mirror();
  130. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
  131. }
  132. if (initalrotate != 0)
  133. {
  134. rt.Rotate(initalrotate);
  135. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  136. }
  137. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  138. {
  139. SaveReferenceAlignmentValues();
  140. }
  141. if (SaveAllFiles) AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  142. if (SaveAllFiles)
  143. {
  144. DrawRef(ImageTMP);
  145. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  146. }
  147. if (ImageTMP) // nuss gelöscht werden, um Speicherplatz für das Laden von tflite zu haben
  148. {
  149. delete ImageTMP;
  150. ImageTMP = NULL;
  151. }
  152. LoadReferenceAlignmentValues();
  153. return true;
  154. }
  155. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  156. {
  157. FILE* pFile;
  158. std::string zwtime, zwvalue;
  159. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  160. if (strlen(zwtime.c_str()) == 0)
  161. {
  162. time_t rawtime;
  163. struct tm* timeinfo;
  164. char buffer[80];
  165. time(&rawtime);
  166. timeinfo = localtime(&rawtime);
  167. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  168. zwtime = std::string(buffer);
  169. }
  170. fputs(zwtime.c_str(), pFile);
  171. fputs("\n", pFile);
  172. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  173. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
  174. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
  175. fputs(zwvalue.c_str(), pFile);
  176. fputs("\n", pFile);
  177. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  178. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
  179. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
  180. fputs(zwvalue.c_str(), pFile);
  181. fputs("\n", pFile);
  182. fclose(pFile);
  183. }
  184. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  185. {
  186. FILE* pFile;
  187. char zw[1024];
  188. string zwvalue;
  189. std::vector<string> zerlegt;
  190. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues01");
  191. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  192. if (pFile == NULL)
  193. return false;
  194. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues01");
  195. fgets(zw, 1024, pFile);
  196. printf("%s", zw);
  197. // zwvalue = "LoadReferenceAlignmentValues Time: " + std::string(zw);
  198. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zwvalue);
  199. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues02");
  200. fgets(zw, 1024, pFile);
  201. zerlegt = ZerlegeZeile(std::string(zw), " \t");
  202. if (zerlegt.size() < 6)
  203. {
  204. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "Exit 01");
  205. fclose(pFile);
  206. return false;
  207. }
  208. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues03");
  209. References[0].fastalg_x = stoi(zerlegt[0]);
  210. References[0].fastalg_y = stoi(zerlegt[1]);
  211. References[0].fastalg_SAD = stof(zerlegt[2]);
  212. References[0].fastalg_min = stoi(zerlegt[3]);
  213. References[0].fastalg_max = stoi(zerlegt[4]);
  214. References[0].fastalg_avg = stof(zerlegt[5]);
  215. fgets(zw, 1024, pFile);
  216. zerlegt = ZerlegeZeile(std::string(zw));
  217. if (zerlegt.size() < 6)
  218. {
  219. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "Exit 02");
  220. fclose(pFile);
  221. return false;
  222. }
  223. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", "LoadReferenceAlignmentValues03");
  224. References[1].fastalg_x = stoi(zerlegt[0]);
  225. References[1].fastalg_y = stoi(zerlegt[1]);
  226. References[1].fastalg_SAD = stof(zerlegt[2]);
  227. References[1].fastalg_min = stoi(zerlegt[3]);
  228. References[1].fastalg_max = stoi(zerlegt[4]);
  229. References[1].fastalg_avg = stof(zerlegt[5]);
  230. fclose(pFile);
  231. #ifdef DEBUG_DETAIL_ON
  232. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  233. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  234. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  235. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  236. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  237. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  238. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  239. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  240. #endif
  241. return true;
  242. }
  243. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  244. {
  245. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  246. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  247. }