ClassFlowAlignment.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "ClassFlowAlignment.h"
  2. #include "ClassFlowTakeImage.h"
  3. #include "ClassFlow.h"
  4. #include "MainFlowControl.h"
  5. #include "CRotateImage.h"
  6. #include "esp_log.h"
  7. #include "ClassLogFile.h"
  8. #include "psram.h"
  9. #include "../../include/defines.h"
  10. #include <new>
  11. static const char *TAG = "ALIGN";
  12. // #define DEBUG_DETAIL_ON
  13. void ClassFlowAlignment::SetInitialParameter(void)
  14. {
  15. initialrotate = 0;
  16. anz_ref = 0;
  17. use_antialiasing = false;
  18. initialflip = false;
  19. SaveAllFiles = false;
  20. namerawimage = "/sdcard/img_tmp/raw.jpg";
  21. FileStoreRefAlignment = "/sdcard/config/align.txt";
  22. ListFlowControll = NULL;
  23. AlignAndCutImage = NULL;
  24. ImageBasis = NULL;
  25. ownsImageBasis = false;
  26. ImageTMP = NULL;
  27. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  28. AlgROI = new (std::nothrow) ImageData;
  29. #endif
  30. previousElement = NULL;
  31. disabled = false;
  32. SAD_criteria = 0.05;
  33. }
  34. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow *> *lfc)
  35. {
  36. SetInitialParameter();
  37. ListFlowControll = lfc;
  38. for (int i = 0; i < ListFlowControll->size(); ++i) {
  39. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0) {
  40. ImageBasis = ((ClassFlowTakeImage *)(*ListFlowControll)[i])->rawImage;
  41. }
  42. }
  43. // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
  44. if (!ImageBasis) {
  45. ESP_LOGD(TAG, "CImageBasis had to be created");
  46. ImageBasis = new (std::nothrow) CImageBasis("ImageBasis", namerawimage);
  47. ownsImageBasis = (ImageBasis != NULL);
  48. if (ImageBasis != NULL && !ImageBasis->ImageOkay()) { delete ImageBasis; ImageBasis = NULL; ownsImageBasis = false; }
  49. }
  50. }
  51. ClassFlowAlignment::~ClassFlowAlignment()
  52. {
  53. delete AlignAndCutImage;
  54. AlignAndCutImage = NULL;
  55. delete ImageTMP;
  56. ImageTMP = NULL;
  57. if (ownsImageBasis) {
  58. delete ImageBasis;
  59. ImageBasis = NULL;
  60. ownsImageBasis = false;
  61. }
  62. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  63. delete AlgROI;
  64. AlgROI = NULL;
  65. #endif
  66. }
  67. bool ClassFlowAlignment::ReadParameter(FILE *pfile, string &aktparamgraph)
  68. {
  69. std::vector<string> splitted;
  70. int suchex = 40;
  71. int suchey = 40;
  72. int alg_algo = 0; // default=0; 1 =HIGHACCURACY; 2= FAST; 3= OFF //add disable aligment algo |01.2023
  73. aktparamgraph = trim(aktparamgraph);
  74. if (aktparamgraph.size() == 0)
  75. {
  76. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  77. return false;
  78. }
  79. }
  80. if (aktparamgraph.compare("[Alignment]") != 0)
  81. {
  82. // Paragraph does not fit Alignment
  83. return false;
  84. }
  85. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  86. {
  87. splitted = ZerlegeZeile(aktparamgraph);
  88. if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1)) {
  89. initialflip = alphanumericToBoolean(splitted[1]);
  90. }
  91. else if (((toUpper(splitted[0]) == "initialrotate") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1)) {
  92. if (isStringNumeric(splitted[1])) {
  93. this->initialrotate = std::stod(splitted[1]);
  94. }
  95. }
  96. else if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1)) {
  97. if (isStringNumeric(splitted[1])) {
  98. suchex = std::stod(splitted[1]);
  99. }
  100. }
  101. else if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1)) {
  102. if (isStringNumeric(splitted[1])) {
  103. suchey = std::stod(splitted[1]);
  104. }
  105. }
  106. else if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1)) {
  107. use_antialiasing = alphanumericToBoolean(splitted[1]);
  108. }
  109. else if ((splitted.size() == 3) && (anz_ref < 2)) {
  110. if ((isStringNumeric(splitted[1])) && (isStringNumeric(splitted[2])))
  111. {
  112. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  113. References[anz_ref].target_x = std::stod(splitted[1]);
  114. References[anz_ref].target_y = std::stod(splitted[2]);
  115. anz_ref++;
  116. }
  117. else
  118. {
  119. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  120. References[anz_ref].target_x = 10;
  121. References[anz_ref].target_y = 10;
  122. anz_ref++;
  123. }
  124. }
  125. else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1)) {
  126. SaveAllFiles = alphanumericToBoolean(splitted[1]);
  127. }
  128. else if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1)) {
  129. #ifdef DEBUG_DETAIL_ON
  130. std::string zw2 = "Alignment mode selected: " + splitted[1];
  131. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  132. #endif
  133. if (toUpper(splitted[1]) == "HIGHACCURACY") {
  134. alg_algo = 1;
  135. }
  136. if (toUpper(splitted[1]) == "FAST") {
  137. alg_algo = 2;
  138. }
  139. if (toUpper(splitted[1]) == "OFF") {
  140. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  141. alg_algo = 3;
  142. }
  143. }
  144. }
  145. for (int i = 0; i < anz_ref; ++i) {
  146. References[i].search_x = suchex;
  147. References[i].search_y = suchey;
  148. References[i].fastalg_SAD_criteria = SAD_criteria;
  149. References[i].alignment_algo = alg_algo;
  150. #ifdef DEBUG_DETAIL_ON
  151. std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
  152. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  153. #endif
  154. }
  155. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  156. if (References[0].alignment_algo != 3) {
  157. return LoadReferenceAlignmentValues();
  158. }
  159. return true;
  160. }
  161. string ClassFlowAlignment::getHTMLSingleStep(string host)
  162. {
  163. string result;
  164. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  165. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  166. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  167. return result;
  168. }
  169. bool ClassFlowAlignment::doFlow(string time)
  170. {
  171. if (ImageBasis == NULL || !ImageBasis->ImageOkay()) {
  172. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Alignment has no valid source image");
  173. return false;
  174. }
  175. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  176. // Reserve the optional JPEG output before tmpImage to reduce PSRAM fragmentation.
  177. if (AlgROI == NULL) AlgROI = new (std::nothrow) ImageData;
  178. if (AlgROI != NULL) {
  179. ImageBasis->writeToMemoryAsJPG(AlgROI, 90);
  180. } else {
  181. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "AlgROI PSRAM allocation failed; continuing without in-memory ROI JPEG");
  182. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  183. }
  184. #endif
  185. if (ImageTMP == NULL) {
  186. ImageTMP = new (std::nothrow) CImageBasis("tmpImage", ImageBasis);
  187. if (ImageTMP == NULL || !ImageTMP->ImageOkay()) {
  188. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tmpImage -> Exec this round aborted!");
  189. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  190. delete ImageTMP;
  191. ImageTMP = NULL;
  192. return false;
  193. }
  194. }
  195. delete AlignAndCutImage;
  196. AlignAndCutImage = new (std::nothrow) CAlignAndCutImage("AlignAndCutImage", ImageBasis, ImageTMP);
  197. if (AlignAndCutImage == NULL) {
  198. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  199. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  200. delete ImageTMP;
  201. ImageTMP = NULL;
  202. return false;
  203. }
  204. CRotateImage rt("rawImage", AlignAndCutImage, ImageTMP, initialflip);
  205. if (initialflip) {
  206. uint8_t* sourceImage = ImageBasis->RGBImageLock();
  207. if (sourceImage == NULL) {
  208. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to lock source image for initial flip");
  209. delete AlignAndCutImage;
  210. AlignAndCutImage = NULL;
  211. delete ImageTMP;
  212. ImageTMP = NULL;
  213. return false;
  214. }
  215. uint8_t* temporaryImage = ImageTMP->RGBImageLock();
  216. if (temporaryImage == NULL) {
  217. ImageBasis->RGBImageRelease();
  218. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to lock temporary image for initial flip");
  219. delete AlignAndCutImage;
  220. AlignAndCutImage = NULL;
  221. delete ImageTMP;
  222. ImageTMP = NULL;
  223. return false;
  224. }
  225. int _zw = ImageBasis->height;
  226. ImageBasis->height = ImageBasis->width;
  227. ImageBasis->width = _zw;
  228. _zw = ImageTMP->width;
  229. ImageTMP->width = ImageTMP->height;
  230. ImageTMP->height = _zw;
  231. ImageTMP->RGBImageRelease();
  232. ImageBasis->RGBImageRelease();
  233. }
  234. if ((initialrotate != 0) || initialflip) {
  235. if (use_antialiasing) {
  236. rt.RotateAntiAliasing(initialrotate);
  237. }
  238. else {
  239. rt.Rotate(initialrotate);
  240. }
  241. if (SaveAllFiles) {
  242. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  243. }
  244. }
  245. // no align algo if set to 3 = off //add disable aligment algo |01.2023
  246. if (References[0].alignment_algo != 3) {
  247. if (!AlignAndCutImage->Align(&References[0], &References[1])) {
  248. SaveReferenceAlignmentValues();
  249. }
  250. } // no align
  251. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  252. if (AlgROI) {
  253. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  254. if (References[0].alignment_algo != 3) {
  255. DrawRef(ImageTMP);
  256. }
  257. flowctrl.DigitDrawROI(ImageTMP);
  258. flowctrl.AnalogDrawROI(ImageTMP);
  259. ImageTMP->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
  260. }
  261. #endif
  262. if (SaveAllFiles) {
  263. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  264. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  265. }
  266. // must be deleted to have memory space for loading tflite
  267. delete ImageTMP;
  268. ImageTMP = NULL;
  269. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  270. if (References[0].alignment_algo != 3) {
  271. return LoadReferenceAlignmentValues();
  272. }
  273. return true;
  274. }
  275. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  276. {
  277. FILE *pFile;
  278. std::string zwtime, zwvalue;
  279. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  280. if (pFile == NULL) {
  281. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SaveReferenceAlignmentValues: Failed to open " + FileStoreRefAlignment);
  282. return;
  283. }
  284. if (strlen(zwtime.c_str()) == 0) {
  285. time_t rawtime;
  286. struct tm *timeinfo;
  287. char buffer[80];
  288. time(&rawtime);
  289. timeinfo = localtime(&rawtime);
  290. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  291. zwtime = std::string(buffer);
  292. }
  293. fputs(zwtime.c_str(), pFile);
  294. fputs("\n", pFile);
  295. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  296. zwvalue = zwvalue + "\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  297. zwvalue = zwvalue + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  298. fputs(zwvalue.c_str(), pFile);
  299. fputs("\n", pFile);
  300. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  301. zwvalue = zwvalue + "\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  302. zwvalue = zwvalue + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  303. fputs(zwvalue.c_str(), pFile);
  304. fputs("\n", pFile);
  305. fclose(pFile);
  306. }
  307. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  308. {
  309. FILE *pFile;
  310. char zw[1024];
  311. string zwvalue;
  312. std::vector<string> splitted;
  313. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  314. if (pFile == NULL) {
  315. return false;
  316. }
  317. fgets(zw, 1024, pFile);
  318. ESP_LOGD(TAG, "%s", zw);
  319. fgets(zw, 1024, pFile);
  320. splitted = ZerlegeZeile(std::string(zw), " \t");
  321. if (splitted.size() < 6) {
  322. fclose(pFile);
  323. return false;
  324. }
  325. References[0].fastalg_x = stoi(splitted[0]);
  326. References[0].fastalg_y = stoi(splitted[1]);
  327. References[0].fastalg_SAD = stof(splitted[2]);
  328. References[0].fastalg_min = stoi(splitted[3]);
  329. References[0].fastalg_max = stoi(splitted[4]);
  330. References[0].fastalg_avg = stof(splitted[5]);
  331. fgets(zw, 1024, pFile);
  332. splitted = ZerlegeZeile(std::string(zw));
  333. if (splitted.size() < 6) {
  334. fclose(pFile);
  335. return false;
  336. }
  337. References[1].fastalg_x = stoi(splitted[0]);
  338. References[1].fastalg_y = stoi(splitted[1]);
  339. References[1].fastalg_SAD = stof(splitted[2]);
  340. References[1].fastalg_min = stoi(splitted[3]);
  341. References[1].fastalg_max = stoi(splitted[4]);
  342. References[1].fastalg_avg = stof(splitted[5]);
  343. fclose(pFile);
  344. /*#ifdef DEBUG_DETAIL_ON
  345. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  346. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  347. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  348. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  349. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  350. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  351. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  352. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  353. #endif*/
  354. return true;
  355. }
  356. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  357. {
  358. if (_zw->ImageOkay()) {
  359. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  360. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  361. }
  362. }