CAlignAndCutImage.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "CAlignAndCutImage.h"
  2. #include "CRotateImage.h"
  3. #include "ClassLogFile.h"
  4. #define _USE_MATH_DEFINES
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <esp_log.h>
  8. static const char* TAG = "c_align_and_cut_image";
  9. //#define GET_MEMORY malloc
  10. #define GET_MEMORY(X) heap_caps_malloc(X, MALLOC_CAP_SPIRAM)
  11. // #define DEBUG_DETAIL_ON
  12. CAlignAndCutImage::CAlignAndCutImage(CImageBasis *_org, CImageBasis *_temp)
  13. {
  14. rgb_image = _org->rgb_image;
  15. channels = _org->channels;
  16. width = _org->width;
  17. height = _org->height;
  18. bpp = _org->bpp;
  19. externalImage = true;
  20. islocked = false;
  21. ImageTMP = _temp;
  22. }
  23. void CAlignAndCutImage::GetRefSize(int *ref_dx, int *ref_dy)
  24. {
  25. ref_dx[0] = t0_dx;
  26. ref_dy[0] = t0_dy;
  27. ref_dx[1] = t1_dx;
  28. ref_dy[1] = t1_dy;
  29. }
  30. bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
  31. {
  32. int dx, dy;
  33. int r0_x, r0_y, r1_x, r1_y;
  34. bool isSimilar1, isSimilar2;
  35. CFindTemplate* ft = new CFindTemplate(rgb_image, channels, width, height, bpp);
  36. r0_x = _temp1->target_x;
  37. r0_y = _temp1->target_y;
  38. ESP_LOGD(TAG, "Vor ft->FindTemplate(_temp1); %s", _temp1->image_file.c_str());
  39. isSimilar1 = ft->FindTemplate(_temp1);
  40. _temp1->width = ft->tpl_width;
  41. _temp1->height = ft->tpl_height;
  42. r1_x = _temp2->target_x;
  43. r1_y = _temp2->target_y;
  44. ESP_LOGD(TAG, "Vor ft->FindTemplate(_temp2); %s", _temp2->image_file.c_str());
  45. isSimilar2 = ft->FindTemplate(_temp2);
  46. _temp2->width = ft->tpl_width;
  47. _temp2->height = ft->tpl_height;
  48. delete ft;
  49. dx = _temp1->target_x - _temp1->found_x;
  50. dy = _temp1->target_y - _temp1->found_y;
  51. r0_x += dx;
  52. r0_y += dy;
  53. r1_x += dx;
  54. r1_y += dy;
  55. float w_org, w_ist, d_winkel;
  56. w_org = atan2(_temp2->found_y - _temp1->found_y, _temp2->found_x - _temp1->found_x);
  57. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  58. d_winkel = (w_ist - w_org) * 180 / M_PI;
  59. #ifdef DEBUG_DETAIL_ON
  60. std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
  61. zw = zw + "\tt1_x_y:\t" + std::to_string(_temp1->found_x) + "\t" + std::to_string(_temp1->found_y);
  62. zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(_temp1->fastalg_min) + "\t" + std::to_string(_temp1->fastalg_avg) + "\t" + std::to_string(_temp1->fastalg_max) + "\t"+ std::to_string(_temp1->fastalg_SAD);
  63. zw = zw + "\tt2_x_y:\t" + std::to_string(_temp2->found_x) + "\t" + std::to_string(_temp2->found_y);
  64. zw = zw + "\tpara2_found_min_avg_max:\t" + std::to_string(_temp2->fastalg_min) + "\t" + std::to_string(_temp2->fastalg_avg) + "\t" + std::to_string(_temp2->fastalg_max) + "\t"+ std::to_string(_temp2->fastalg_SAD);
  65. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  66. #endif
  67. CRotateImage rt(this, ImageTMP);
  68. rt.Translate(dx, dy);
  69. rt.Rotate(d_winkel, _temp1->target_x, _temp1->target_y);
  70. ESP_LOGD(TAG, "Alignment: dx %d - dy %d - rot %f", dx, dy, d_winkel);
  71. return (isSimilar1 && isSimilar2);
  72. }
  73. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  74. {
  75. int x2, y2;
  76. x2 = x1 + dx;
  77. y2 = y1 + dy;
  78. x2 = std::min(x2, width - 1);
  79. y2 = std::min(y2, height - 1);
  80. dx = x2 - x1;
  81. dy = y2 - y1;
  82. int memsize = dx * dy * channels;
  83. uint8_t* odata = (unsigned char*) GET_MEMORY(memsize);
  84. stbi_uc* p_target;
  85. stbi_uc* p_source;
  86. RGBImageLock();
  87. for (int x = x1; x < x2; ++x)
  88. for (int y = y1; y < y2; ++y)
  89. {
  90. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  91. p_source = rgb_image + (channels * (y * width + x));
  92. for (int _channels = 0; _channels < channels; ++_channels)
  93. p_target[_channels] = p_source[_channels];
  94. }
  95. // stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 0);
  96. stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
  97. RGBImageRelease();
  98. stbi_image_free(odata);
  99. }
  100. void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
  101. {
  102. int x2, y2;
  103. x2 = x1 + dx;
  104. y2 = y1 + dy;
  105. x2 = std::min(x2, width - 1);
  106. y2 = std::min(y2, height - 1);
  107. dx = x2 - x1;
  108. dy = y2 - y1;
  109. if ((_target->height != dy) || (_target->width != dx) || (_target->channels != channels))
  110. {
  111. ESP_LOGD(TAG, "CAlignAndCutImage::CutAndSave - Image size does not match!");
  112. return;
  113. }
  114. uint8_t* odata = _target->RGBImageLock();
  115. RGBImageLock();
  116. stbi_uc* p_target;
  117. stbi_uc* p_source;
  118. for (int x = x1; x < x2; ++x)
  119. for (int y = y1; y < y2; ++y)
  120. {
  121. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  122. p_source = rgb_image + (channels * (y * width + x));
  123. for (int _channels = 0; _channels < channels; ++_channels)
  124. p_target[_channels] = p_source[_channels];
  125. }
  126. RGBImageRelease();
  127. _target->RGBImageRelease();
  128. }
  129. CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  130. {
  131. int x2, y2;
  132. x2 = x1 + dx;
  133. y2 = y1 + dy;
  134. x2 = std::min(x2, width - 1);
  135. y2 = std::min(y2, height - 1);
  136. dx = x2 - x1;
  137. dy = y2 - y1;
  138. int memsize = dx * dy * channels;
  139. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  140. stbi_uc* p_target;
  141. stbi_uc* p_source;
  142. RGBImageLock();
  143. for (int x = x1; x < x2; ++x)
  144. for (int y = y1; y < y2; ++y)
  145. {
  146. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  147. p_source = rgb_image + (channels * (y * width + x));
  148. for (int _channels = 0; _channels < channels; ++_channels)
  149. p_target[_channels] = p_source[_channels];
  150. }
  151. CImageBasis* rs = new CImageBasis(odata, channels, dx, dy, bpp);
  152. RGBImageRelease();
  153. rs->SetIndepended();
  154. return rs;
  155. }