CFindTemplate.cpp 5.6 KB

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