CAlignAndCutImage.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ImageTMP = _temp;
  18. }
  19. void CAlignAndCutImage::GetRefSize(int *ref_dx, int *ref_dy)
  20. {
  21. ref_dx[0] = t0_dx;
  22. ref_dy[0] = t0_dy;
  23. ref_dx[1] = t1_dx;
  24. ref_dy[1] = t1_dy;
  25. }
  26. 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)
  27. {
  28. int dx, dy;
  29. int r0_x, r0_y, r1_x, r1_y;
  30. // CFindTemplate* ft = new CFindTemplate(filename);
  31. CFindTemplate* ft = new CFindTemplate(rgb_image, channels, width, height, bpp);
  32. r0_x = ref0_x;
  33. r0_y = ref0_y;
  34. ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
  35. t0_dx = ft->tpl_width;
  36. t0_dy = ft->tpl_height;
  37. r1_x = ref1_x;
  38. r1_y = ref1_y;
  39. ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
  40. t1_dx = ft->tpl_width;
  41. t1_dy = ft->tpl_height;
  42. delete ft;
  43. dx = ref0_x - r0_x;
  44. dy = ref0_y - r0_y;
  45. r0_x += dx;
  46. r0_y += dy;
  47. r1_x += dx;
  48. r1_y += dy;
  49. float w_org, w_ist, d_winkel;
  50. w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
  51. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  52. d_winkel = (w_org - w_ist) * 180 / M_PI;
  53. if (imageROI.length() > 0)
  54. {
  55. CImageBasis* imgzw = new CImageBasis(this);
  56. imgzw->drawRect(r0_x, r0_y, t0_dx, t0_dy, 255, 0, 0, 2);
  57. imgzw->drawRect(r1_x, r1_y, t1_dx, t1_dy, 255, 0, 0, 2);
  58. imgzw->SaveToFile(imageROI);
  59. printf("Alignment: alignment ROI created: %s\n", imageROI.c_str());
  60. delete imgzw;
  61. }
  62. std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
  63. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  64. CRotateImage rt(this, ImageTMP);
  65. rt.Translate(dx, dy);
  66. rt.Rotate(d_winkel, ref0_x, ref0_y);
  67. printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
  68. }
  69. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  70. {
  71. int x2, y2;
  72. x2 = x1 + dx;
  73. y2 = y1 + dy;
  74. x2 = std::min(x2, width - 1);
  75. y2 = std::min(y2, height - 1);
  76. dx = x2 - x1;
  77. dy = y2 - y1;
  78. int memsize = dx * dy * channels;
  79. uint8_t* odata = (unsigned char*) GET_MEMORY(memsize);
  80. stbi_uc* p_target;
  81. stbi_uc* p_source;
  82. for (int x = x1; x < x2; ++x)
  83. for (int y = y1; y < y2; ++y)
  84. {
  85. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  86. p_source = rgb_image + (channels * (y * width + x));
  87. for (int _channels = 0; _channels < channels; ++_channels)
  88. p_target[_channels] = p_source[_channels];
  89. }
  90. // stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 0);
  91. stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
  92. stbi_image_free(odata);
  93. }
  94. void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
  95. {
  96. int x2, y2;
  97. x2 = x1 + dx;
  98. y2 = y1 + dy;
  99. x2 = std::min(x2, width - 1);
  100. y2 = std::min(y2, height - 1);
  101. dx = x2 - x1;
  102. dy = y2 - y1;
  103. if ((_target->height != dy) || (_target->width != dx) || (_target->channels != channels))
  104. {
  105. printf("CAlignAndCutImage::CutAndSave - Bildgröße passt nicht !!!!!!!!!");
  106. return;
  107. }
  108. uint8_t* odata = _target->rgb_image;
  109. stbi_uc* p_target;
  110. stbi_uc* p_source;
  111. for (int x = x1; x < x2; ++x)
  112. for (int y = y1; y < y2; ++y)
  113. {
  114. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  115. p_source = rgb_image + (channels * (y * width + x));
  116. for (int _channels = 0; _channels < channels; ++_channels)
  117. p_target[_channels] = p_source[_channels];
  118. }
  119. }
  120. CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  121. {
  122. int x2, y2;
  123. x2 = x1 + dx;
  124. y2 = y1 + dy;
  125. x2 = std::min(x2, width - 1);
  126. y2 = std::min(y2, height - 1);
  127. dx = x2 - x1;
  128. dy = y2 - y1;
  129. int memsize = dx * dy * channels;
  130. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  131. stbi_uc* p_target;
  132. stbi_uc* p_source;
  133. for (int x = x1; x < x2; ++x)
  134. for (int y = y1; y < y2; ++y)
  135. {
  136. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  137. p_source = rgb_image + (channels * (y * width + x));
  138. for (int _channels = 0; _channels < channels; ++_channels)
  139. p_target[_channels] = p_source[_channels];
  140. }
  141. CImageBasis* rs = new CImageBasis(odata, channels, dx, dy, bpp);
  142. rs->SetIndepended();
  143. return rs;
  144. }