ClassFlowAlignment.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. bool AlignmentExtendedDebugging = true;
  10. // #define DEBUG_DETAIL_ON
  11. void ClassFlowAlignment::SetInitialParameter(void)
  12. {
  13. initalrotate = 0;
  14. anz_ref = 0;
  15. initialmirror = false;
  16. use_antialiasing = false;
  17. initialflip = false;
  18. SaveAllFiles = false;
  19. namerawimage = "/sdcard/img_tmp/raw.jpg";
  20. FileStoreRefAlignment = "/sdcard/config/align.txt";
  21. ListFlowControll = NULL;
  22. AlignAndCutImage = NULL;
  23. ImageBasis = NULL;
  24. ImageTMP = NULL;
  25. previousElement = NULL;
  26. disabled = false;
  27. SAD_criteria = 0.05;
  28. }
  29. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow*>* lfc)
  30. {
  31. SetInitialParameter();
  32. ListFlowControll = lfc;
  33. for (int i = 0; i < ListFlowControll->size(); ++i)
  34. {
  35. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  36. {
  37. ImageBasis = ((ClassFlowMakeImage*) (*ListFlowControll)[i])->rawImage;
  38. }
  39. }
  40. if (!ImageBasis) // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
  41. {
  42. if (AlignmentExtendedDebugging) ESP_LOGD(TAG, "CImageBasis had to be created");
  43. ImageBasis = new CImageBasis(namerawimage);
  44. }
  45. }
  46. bool ClassFlowAlignment::ReadParameter(FILE* pfile, string& aktparamgraph)
  47. {
  48. std::vector<string> splitted;
  49. int suchex = 40;
  50. int suchey = 40;
  51. int alg_algo = 0;
  52. aktparamgraph = trim(aktparamgraph);
  53. if (aktparamgraph.size() == 0)
  54. if (!this->GetNextParagraph(pfile, aktparamgraph))
  55. return false;
  56. if (aktparamgraph.compare("[Alignment]") != 0) //Paragraph does not fit MakeImage
  57. return false;
  58. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  59. {
  60. splitted = ZerlegeZeile(aktparamgraph);
  61. if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1))
  62. {
  63. if (toUpper(splitted[1]) == "TRUE")
  64. initialflip = true;
  65. }
  66. if ((toUpper(splitted[0]) == "INITIALMIRROR") && (splitted.size() > 1))
  67. {
  68. if (toUpper(splitted[1]) == "TRUE")
  69. initialmirror = true;
  70. }
  71. if (((toUpper(splitted[0]) == "INITALROTATE") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
  72. {
  73. this->initalrotate = std::stod(splitted[1]);
  74. }
  75. if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1))
  76. {
  77. suchex = std::stod(splitted[1]);
  78. }
  79. if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1))
  80. {
  81. suchey = std::stod(splitted[1]);
  82. }
  83. if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1))
  84. {
  85. if (toUpper(splitted[1]) == "TRUE")
  86. use_antialiasing = true;
  87. }
  88. if ((splitted.size() == 3) && (anz_ref < 2))
  89. {
  90. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  91. References[anz_ref].target_x = std::stod(splitted[1]);
  92. References[anz_ref].target_y = std::stod(splitted[2]);
  93. anz_ref++;
  94. }
  95. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  96. {
  97. if (toUpper(splitted[1]) == "TRUE")
  98. SaveAllFiles = true;
  99. }
  100. if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1))
  101. {
  102. #ifdef DEBUG_DETAIL_ON
  103. std::string zw2 = "Alignment mode selected: " + splitted[1];
  104. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  105. #endif
  106. if (toUpper(splitted[1]) == "HIGHACCURACY")
  107. alg_algo = 1;
  108. if (toUpper(splitted[1]) == "FAST")
  109. alg_algo = 2;
  110. }
  111. }
  112. for (int i = 0; i < anz_ref; ++i)
  113. {
  114. References[i].search_x = suchex;
  115. References[i].search_y = suchey;
  116. References[i].fastalg_SAD_criteria = SAD_criteria;
  117. References[i].alignment_algo = alg_algo;
  118. #ifdef DEBUG_DETAIL_ON
  119. std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
  120. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  121. #endif
  122. }
  123. LoadReferenceAlignmentValues();
  124. return true;
  125. }
  126. string ClassFlowAlignment::getHTMLSingleStep(string host)
  127. {
  128. string result;
  129. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  130. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  131. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  132. return result;
  133. }
  134. bool ClassFlowAlignment::doFlow(string time)
  135. {
  136. if (!ImageTMP)
  137. ImageTMP = new CImageBasis(ImageBasis, 5);
  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. // must be deleted to have memory space for loading tflite
  177. delete ImageTMP;
  178. ImageTMP = NULL;
  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> splitted;
  217. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  218. if (pFile == NULL)
  219. return false;
  220. fgets(zw, 1024, pFile);
  221. ESP_LOGD(TAG, "%s", zw);
  222. fgets(zw, 1024, pFile);
  223. splitted = ZerlegeZeile(std::string(zw), " \t");
  224. if (splitted.size() < 6)
  225. {
  226. fclose(pFile);
  227. return false;
  228. }
  229. References[0].fastalg_x = stoi(splitted[0]);
  230. References[0].fastalg_y = stoi(splitted[1]);
  231. References[0].fastalg_SAD = stof(splitted[2]);
  232. References[0].fastalg_min = stoi(splitted[3]);
  233. References[0].fastalg_max = stoi(splitted[4]);
  234. References[0].fastalg_avg = stof(splitted[5]);
  235. fgets(zw, 1024, pFile);
  236. splitted = ZerlegeZeile(std::string(zw));
  237. if (splitted.size() < 6)
  238. {
  239. fclose(pFile);
  240. return false;
  241. }
  242. References[1].fastalg_x = stoi(splitted[0]);
  243. References[1].fastalg_y = stoi(splitted[1]);
  244. References[1].fastalg_SAD = stof(splitted[2]);
  245. References[1].fastalg_min = stoi(splitted[3]);
  246. References[1].fastalg_max = stoi(splitted[4]);
  247. References[1].fastalg_avg = stof(splitted[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. }