CAlignAndCutImage.cpp 5.7 KB

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