CAlignAndCutImage.cpp 5.7 KB

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