CAlignAndCutImage.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "CAlignAndCutImage.h"
  2. #include "CRotateImage.h"
  3. #include "CFindTemplate.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. void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax, int deltay, std::string imageROI)
  28. {
  29. int dx, dy;
  30. int r0_x, r0_y, r1_x, r1_y;
  31. // CFindTemplate* ft = new CFindTemplate(filename);
  32. CFindTemplate* ft = new CFindTemplate(rgb_image, channels, width, height, bpp);
  33. r0_x = ref0_x;
  34. r0_y = ref0_y;
  35. ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
  36. t0_dx = ft->tpl_width;
  37. t0_dy = ft->tpl_height;
  38. r1_x = ref1_x;
  39. r1_y = ref1_y;
  40. ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
  41. t1_dx = ft->tpl_width;
  42. t1_dy = ft->tpl_height;
  43. delete ft;
  44. dx = ref0_x - r0_x;
  45. dy = ref0_y - r0_y;
  46. r0_x += dx;
  47. r0_y += dy;
  48. r1_x += dx;
  49. r1_y += dy;
  50. float w_org, w_ist, d_winkel;
  51. w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
  52. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  53. d_winkel = (w_org - w_ist) * 180 / M_PI;
  54. if (imageROI.length() > 0)
  55. {
  56. CImageBasis* imgzw = new CImageBasis(this);
  57. imgzw->drawRect(r0_x, r0_y, t0_dx, t0_dy, 255, 0, 0, 2);
  58. imgzw->drawRect(r1_x, r1_y, t1_dx, t1_dy, 255, 0, 0, 2);
  59. imgzw->SaveToFile(imageROI);
  60. printf("Alignment: alignment ROI created: %s\n", imageROI.c_str());
  61. delete imgzw;
  62. }
  63. std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
  64. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  65. CRotateImage rt(this, ImageTMP);
  66. rt.Translate(dx, dy);
  67. rt.Rotate(d_winkel, ref0_x, ref0_y);
  68. printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
  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 - Bildgröße passt nicht !!!!!!!!!");
  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. }