CFindTemplate.cpp 5.5 KB

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