CAlignAndCutImage.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "CAlignAndCutImage.h"
  2. #include "CRotateImage.h"
  3. #include "ClassLogFile.h"
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <esp_log.h>
  7. #include "psram.h"
  8. #include "../../include/defines.h"
  9. #include "Helper.h"
  10. static const char* TAG = "c_align_and_cut_image";
  11. CAlignAndCutImage::CAlignAndCutImage(std::string _name, CImageBasis *_org, CImageBasis *_temp) : CImageBasis(_name)
  12. {
  13. name = _name;
  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("align", rgb_image, channels, width, height, bpp);
  36. r0_x = _temp1->target_x;
  37. r0_y = _temp1->target_y;
  38. ESP_LOGD(TAG, "Before 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, "Before 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((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
  57. w_ist = atan2((float)(r1_y - r0_y), (float)(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("Align", 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*) malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  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. #ifdef STBI_ONLY_JPEG
  96. stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 100);
  97. #else
  98. stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
  99. #endif
  100. RGBImageRelease();
  101. free_psram_heap(std::string(TAG) + "->odata", odata);
  102. }
  103. void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
  104. {
  105. int x2, y2;
  106. x2 = x1 + dx;
  107. y2 = y1 + dy;
  108. x2 = std::min(x2, width - 1);
  109. y2 = std::min(y2, height - 1);
  110. dx = x2 - x1;
  111. dy = y2 - y1;
  112. if ((_target->height != dy) || (_target->width != dx) || (_target->channels != channels))
  113. {
  114. ESP_LOGD(TAG, "CAlignAndCutImage::CutAndSave - Image size does not match!");
  115. return;
  116. }
  117. uint8_t* odata = _target->RGBImageLock();
  118. RGBImageLock();
  119. stbi_uc* p_target;
  120. stbi_uc* p_source;
  121. for (int x = x1; x < x2; ++x)
  122. for (int y = y1; y < y2; ++y)
  123. {
  124. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  125. p_source = rgb_image + (channels * (y * width + x));
  126. for (int _channels = 0; _channels < channels; ++_channels)
  127. p_target[_channels] = p_source[_channels];
  128. }
  129. RGBImageRelease();
  130. _target->RGBImageRelease();
  131. }
  132. CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  133. {
  134. int x2, y2;
  135. x2 = x1 + dx;
  136. y2 = y1 + dy;
  137. x2 = std::min(x2, width - 1);
  138. y2 = std::min(y2, height - 1);
  139. dx = x2 - x1;
  140. dy = y2 - y1;
  141. int memsize = dx * dy * channels;
  142. uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  143. stbi_uc* p_target;
  144. stbi_uc* p_source;
  145. RGBImageLock();
  146. for (int x = x1; x < x2; ++x)
  147. for (int y = y1; y < y2; ++y)
  148. {
  149. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  150. p_source = rgb_image + (channels * (y * width + x));
  151. for (int _channels = 0; _channels < channels; ++_channels)
  152. p_target[_channels] = p_source[_channels];
  153. }
  154. CImageBasis* rs = new CImageBasis("CutAndSave", odata, channels, dx, dy, bpp);
  155. RGBImageRelease();
  156. rs->SetIndepended();
  157. return rs;
  158. }