ClassFlowAlignment.cpp 11 KB

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