ClassFlowAlignment.cpp 11 KB

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