CFindTemplate.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "CFindTemplate.h"
  2. #include "ClassLogFile.h"
  3. #include "Helper.h"
  4. #include "../../include/defines.h"
  5. #include <esp_log.h>
  6. static const char* TAG = "C FIND TEMPL";
  7. // #define DEBUG_DETAIL_ON
  8. bool CFindTemplate::FindTemplate(RefInfo *_ref)
  9. {
  10. uint8_t* rgb_template;
  11. if (file_size(_ref->image_file.c_str()) == 0) {
  12. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _ref->image_file + " is empty!");
  13. return false;
  14. }
  15. rgb_template = stbi_load(_ref->image_file.c_str(), &tpl_width, &tpl_height, &tpl_bpp, channels);
  16. if (rgb_template == NULL) {
  17. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to load " + _ref->image_file + "! Is it corrupted?");
  18. return false;
  19. }
  20. // ESP_LOGD(TAG, "FindTemplate 01");
  21. int ow, ow_start, ow_stop;
  22. int oh, oh_start, oh_stop;
  23. if (_ref->search_x == 0)
  24. {
  25. _ref->search_x = width;
  26. _ref->found_x = 0;
  27. }
  28. if (_ref->search_y == 0)
  29. {
  30. _ref->search_y = height;
  31. _ref->found_y = 0;
  32. }
  33. ow_start = _ref->target_x - _ref->search_x;
  34. ow_start = std::max(ow_start, 0);
  35. ow_stop = _ref->target_x + _ref->search_x;
  36. if ((ow_stop + tpl_width) > width)
  37. ow_stop = width - tpl_width;
  38. ow = ow_stop - ow_start + 1;
  39. oh_start = _ref->target_y - _ref->search_y;
  40. oh_start = std::max(oh_start, 0);
  41. oh_stop = _ref->target_y + _ref->search_y;
  42. if ((oh_stop + tpl_height) > height)
  43. oh_stop = height - tpl_height;
  44. oh = oh_stop - oh_start + 1;
  45. float avg, SAD;
  46. int min, max;
  47. bool isSimilar = false;
  48. // ESP_LOGD(TAG, "FindTemplate 02");
  49. if ((_ref->alignment_algo == 2) && (_ref->fastalg_x > -1) && (_ref->fastalg_y > -1)) // für Testzwecke immer Berechnen
  50. {
  51. isSimilar = CalculateSimularities(rgb_template, _ref->fastalg_x, _ref->fastalg_y, ow, oh, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
  52. /*#ifdef DEBUG_DETAIL_ON
  53. std::string zw = "\t" + _ref->image_file + "\tt1_x_y:\t" + std::to_string(_ref->fastalg_x) + "\t" + std::to_string(_ref->fastalg_y);
  54. zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(min) + "\t" + std::to_string(avg) + "\t" + std::to_string(max) + "\t"+ std::to_string(SAD);
  55. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  56. #endif*/
  57. }
  58. // ESP_LOGD(TAG, "FindTemplate 03");
  59. if (isSimilar)
  60. {
  61. #ifdef DEBUG_DETAIL_ON
  62. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Use FastAlignment sucessfull");
  63. #endif
  64. _ref->found_x = _ref->fastalg_x;
  65. _ref->found_y = _ref->fastalg_y;
  66. stbi_image_free(rgb_template);
  67. return true;
  68. }
  69. // ESP_LOGD(TAG, "FindTemplate 04");
  70. double aktSAD;
  71. double minSAD = pow(tpl_width * tpl_height * 255, 2);
  72. RGBImageLock();
  73. // ESP_LOGD(TAG, "FindTemplate 05");
  74. int xouter, youter, tpl_x, tpl_y, _ch;
  75. int _anzchannels = channels;
  76. if (_ref->alignment_algo == 0) // 0 = "Default" (nur R-Kanal)
  77. _anzchannels = 1;
  78. for (xouter = ow_start; xouter <= ow_stop; xouter++)
  79. for (youter = oh_start; youter <= oh_stop; ++youter)
  80. {
  81. aktSAD = 0;
  82. for (tpl_x = 0; tpl_x < tpl_width; tpl_x++)
  83. for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
  84. {
  85. stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
  86. stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
  87. for (_ch = 0; _ch < _anzchannels; ++_ch)
  88. {
  89. aktSAD += pow(p_tpl[_ch] - p_org[_ch], 2);
  90. }
  91. }
  92. if (aktSAD < minSAD)
  93. {
  94. minSAD = aktSAD;
  95. _ref->found_x = xouter;
  96. _ref->found_y = youter;
  97. }
  98. }
  99. // ESP_LOGD(TAG, "FindTemplate 06");
  100. if (_ref->alignment_algo == 2)
  101. CalculateSimularities(rgb_template, _ref->found_x, _ref->found_y, ow, oh, min, avg, max, SAD, _ref->fastalg_SAD, _ref->fastalg_SAD_criteria);
  102. // ESP_LOGD(TAG, "FindTemplate 07");
  103. _ref->fastalg_x = _ref->found_x;
  104. _ref->fastalg_y = _ref->found_y;
  105. _ref->fastalg_min = min;
  106. _ref->fastalg_avg = avg;
  107. _ref->fastalg_max = max;
  108. _ref->fastalg_SAD = SAD;
  109. /*#ifdef DEBUG_DETAIL_ON
  110. std::string zw = "\t" + _ref->image_file + "\tt1_x_y:\t" + std::to_string(_ref->fastalg_x) + "\t" + std::to_string(_ref->fastalg_y);
  111. zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(min) + "\t" + std::to_string(avg) + "\t" + std::to_string(max) + "\t"+ std::to_string(SAD);
  112. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  113. #endif*/
  114. RGBImageRelease();
  115. stbi_image_free(rgb_template);
  116. // ESP_LOGD(TAG, "FindTemplate 08");
  117. return false;
  118. }
  119. bool CFindTemplate::CalculateSimularities(uint8_t* _rgb_tmpl, int _startx, int _starty, int _sizex, int _sizey, int &min, float &avg, int &max, float &SAD, float _SADold, float _SADcrit)
  120. {
  121. int dif;
  122. int minDif = 255;
  123. int maxDif = -255;
  124. double avgDifSum = 0;
  125. long int anz = 0;
  126. double aktSAD = 0;
  127. int xouter, youter, _ch;
  128. for (xouter = 0; xouter <= _sizex; xouter++)
  129. for (youter = 0; youter <= _sizey; ++youter)
  130. {
  131. stbi_uc* p_org = rgb_image + (channels * ((youter + _starty) * width + (xouter + _startx)));
  132. stbi_uc* p_tpl = _rgb_tmpl + (channels * (youter * tpl_width + xouter));
  133. for (_ch = 0; _ch < channels; ++_ch)
  134. {
  135. dif = p_tpl[_ch] - p_org[_ch];
  136. aktSAD += pow(p_tpl[_ch] - p_org[_ch], 2);
  137. if (dif < minDif) minDif = dif;
  138. if (dif > maxDif) maxDif = dif;
  139. avgDifSum += dif;
  140. anz++;
  141. }
  142. }
  143. avg = avgDifSum / anz;
  144. min = minDif;
  145. max = maxDif;
  146. SAD = sqrt(aktSAD) / anz;
  147. float _SADdif = abs(SAD - _SADold);
  148. ESP_LOGD(TAG, "Anzahl %ld, avgDifSum %fd, avg %f, SAD_neu: %fd, _SAD_old: %f, _SAD_crit:%f", anz, avgDifSum, avg, SAD, _SADold, _SADdif);
  149. if (_SADdif <= _SADcrit)
  150. return true;
  151. return false;
  152. }