CFindTemplate.cp__p 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "CFindTemplate.h"
  2. #include "Helper.h"
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>
  5. #include <algorithm>
  6. #define _ESP32_PSRAM
  7. using namespace std;
  8. #define GET_MEMORY malloc
  9. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  10. {
  11. stbi_uc* p_source;
  12. p_source = this->rgb_image + (this->channels * (y * this->width + x));
  13. return p_source[channels];
  14. }
  15. void CResizeImage::Resize(int _new_dx, int _new_dy)
  16. {
  17. int memsize = _new_dx * _new_dy * this->channels;
  18. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  19. stbir_resize_uint8(this->rgb_image, this->width, this->height, 0, odata, _new_dx, _new_dy, 0, this->channels);
  20. stbi_image_free(this->rgb_image);
  21. this->rgb_image = (unsigned char*)GET_MEMORY(memsize);
  22. this->memCopy(odata, this->rgb_image, memsize);
  23. this->width = _new_dx;
  24. this->height = _new_dy;
  25. stbi_image_free(odata);
  26. }
  27. void CRotate::Rotate(float _angle, int _centerx, int _centery)
  28. {
  29. float m[2][3];
  30. float x_center = _centerx;
  31. float y_center = _centery;
  32. _angle = _angle / 180 * M_PI;
  33. m[0][0] = cos(_angle);
  34. m[0][1] = sin(_angle);
  35. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  36. m[1][0] = -m[0][1];
  37. m[1][1] = m[0][0];
  38. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  39. int memsize = this->width * this->height * this->channels;
  40. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  41. int x_source, y_source;
  42. stbi_uc* p_target;
  43. stbi_uc* p_source;
  44. for (int x = 0; x < this->width; ++x)
  45. for (int y = 0; y < this->height; ++y)
  46. {
  47. p_target = odata + (this->channels * (y * this->width + x));
  48. x_source = int(m[0][0] * x + m[0][1] * y);
  49. y_source = int(m[1][0] * x + m[1][1] * y);
  50. x_source += int(m[0][2]);
  51. y_source += int(m[1][2]);
  52. if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
  53. {
  54. p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
  55. for (int channels = 0; channels < this->channels; ++channels)
  56. p_target[channels] = p_source[channels];
  57. }
  58. else
  59. {
  60. for (int channels = 0; channels < this->channels; ++channels)
  61. p_target[channels] = 255;
  62. }
  63. }
  64. // memcpy(this->rgb_image, odata, memsize);
  65. this->memCopy(odata, this->rgb_image, memsize);
  66. stbi_image_free(odata);
  67. }
  68. void CRotate::Rotate(float _angle)
  69. {
  70. this->Rotate(_angle, this->width / 2, this->height / 2);
  71. }
  72. void CRotate::Translate(int _dx, int _dy)
  73. {
  74. int memsize = this->width * this->height * this->channels;
  75. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  76. int x_source, y_source;
  77. stbi_uc* p_target;
  78. stbi_uc* p_source;
  79. for (int x = 0; x < this->width; ++x)
  80. for (int y = 0; y < this->height; ++y)
  81. {
  82. p_target = odata + (this->channels * (y * this->width + x));
  83. x_source = x - _dx;
  84. y_source = y - _dy;
  85. if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
  86. {
  87. p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
  88. for (int channels = 0; channels < this->channels; ++channels)
  89. p_target[channels] = p_source[channels];
  90. }
  91. else
  92. {
  93. for (int channels = 0; channels < this->channels; ++channels)
  94. p_target[channels] = 255;
  95. }
  96. }
  97. // memcpy(this->rgb_image, odata, memsize);
  98. this->memCopy(odata, this->rgb_image, memsize);
  99. stbi_image_free(odata);
  100. }
  101. CFindTemplate::CFindTemplate(std::string _image)
  102. {
  103. this->channels = 1;
  104. this->rgb_image = stbi_load(_image.c_str(), &(this->width), &(this->height), &(this->bpp), this->channels);
  105. }
  106. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y)
  107. {
  108. this->FindTemplate(_template, found_x, found_y, 0, 0);
  109. }
  110. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy)
  111. {
  112. int tpl_width, tpl_height, tpl_bpp;
  113. uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, this->channels);
  114. int ow, ow_start, ow_stop;
  115. int oh, oh_start, oh_stop;
  116. if (_dx == 0)
  117. {
  118. _dx = this->width;
  119. *found_x = 0;
  120. }
  121. if (_dy == 0)
  122. {
  123. _dy = this->height;
  124. *found_y = 0;
  125. }
  126. ow_start = *found_x - _dx;
  127. ow_start = std::max(ow_start, 0);
  128. ow_stop = *found_x + _dx;
  129. if ((ow_stop + tpl_width) > this->width)
  130. ow_stop = this->width - tpl_width;
  131. ow = ow_stop - ow_start + 1;
  132. oh_start = *found_y - _dy;
  133. oh_start = std::max(oh_start, 0);
  134. oh_stop = *found_y + _dy;
  135. if ((oh_stop + tpl_height) > this->height)
  136. oh_stop = this->height - tpl_height;
  137. oh = oh_stop - oh_start + 1;
  138. uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * this->channels);
  139. double aktSAD;
  140. double minSAD = pow(tpl_width * tpl_height * 255, 2);
  141. for (int xouter = ow_start; xouter <= ow_stop; xouter++)
  142. for (int youter = oh_start; youter <= oh_stop; ++youter)
  143. {
  144. aktSAD = 0;
  145. for (int tpl_x = 0; tpl_x < tpl_width; tpl_x++)
  146. for (int tpl_y = 0; tpl_y < tpl_height; tpl_y++)
  147. {
  148. stbi_uc* p_org = this->rgb_image + (this->channels * ((youter + tpl_y) * this->width + (xouter + tpl_x)));
  149. stbi_uc* p_tpl = rgb_template + (this->channels * (tpl_y * tpl_width + tpl_x));
  150. aktSAD += pow(p_tpl[0] - p_org[0], 2);
  151. }
  152. stbi_uc* p_out = odata + (this->channels * ((youter - oh_start) * ow + (xouter - ow_start)));
  153. p_out[0] = int(sqrt(aktSAD / (tpl_width * tpl_height)));
  154. if (aktSAD < minSAD)
  155. {
  156. minSAD = aktSAD;
  157. *found_x = xouter;
  158. *found_y = youter;
  159. }
  160. }
  161. stbi_image_free(odata);
  162. stbi_image_free(rgb_template);
  163. }
  164. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout)
  165. {
  166. this->FindTemplate(_template, found_x, found_y);
  167. this->SaveToFile(_imageout);
  168. }
  169. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout)
  170. {
  171. this->FindTemplate(_template, found_x, found_y, _dx, _dy);
  172. this->SaveToFile(_imageout);
  173. }
  174. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  175. {
  176. #ifdef _ESP32_PSRAM
  177. for (int i = 0; i < _size; ++i)
  178. *(_target + i) = *(_source + i);
  179. #else
  180. memcpy(_target, _source, _size);
  181. #endif
  182. }
  183. CImageBasis::CImageBasis()
  184. {
  185. this->externalImage = false;
  186. }
  187. CImageBasis::CImageBasis(std::string _image)
  188. {
  189. // printf("Start CImageBasis\n");
  190. channels = 3;
  191. externalImage = false;
  192. filename = _image;
  193. // printf("CImageBasis before load\n");
  194. // printf(_image.c_str()); printf("\n");
  195. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  196. if (!rgb_image)
  197. {
  198. printf("Datei konnte nicht geoeffnet werden\n");
  199. return;
  200. }
  201. // printf("CImageBasis after load\n");
  202. // printf("w %d, h %d, b %d, c %d\n", width, height, bpp, channels);
  203. }
  204. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  205. {
  206. rgb_image = _rgb_image;
  207. channels = _channels;
  208. width = _width;
  209. height = _height;
  210. bpp = _bpp;
  211. externalImage = true;
  212. }
  213. CImageBasis::~CImageBasis()
  214. {
  215. if (!externalImage)
  216. stbi_image_free(rgb_image);
  217. }
  218. void CImageBasis::SaveToFile(std::string _imageout)
  219. {
  220. string typ = getFileType(_imageout);
  221. if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
  222. {
  223. stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
  224. }
  225. if ((typ == "bmp") || (typ == "BMP"))
  226. {
  227. stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
  228. }
  229. // stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
  230. // stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
  231. }
  232. void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax = 40, int deltay = 40)
  233. {
  234. int dx, dy;
  235. int r0_x, r0_y, r1_x, r1_y;
  236. CFindTemplate* ft = new CFindTemplate(this->filename);
  237. r0_x = ref0_x;
  238. r0_y = ref0_y;
  239. ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
  240. r1_x = ref1_x;
  241. r1_y = ref1_y;
  242. ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
  243. delete ft;
  244. dx = ref0_x - r0_x;
  245. dy = ref0_y - r0_y;
  246. r0_x += dx;
  247. r0_y += dy;
  248. r1_x += dx;
  249. r1_y += dy;
  250. float w_org, w_ist, d_winkel;
  251. w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
  252. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  253. d_winkel = -(w_org - w_ist) * 180 / M_PI;
  254. CRotate rt(this->rgb_image, this->channels, this->width, this->height, this->bpp);
  255. rt.Translate(dx, dy);
  256. rt.Rotate(d_winkel, ref0_x, ref0_y);
  257. printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
  258. }
  259. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  260. {
  261. int x2, y2;
  262. x2 = x1 + dx;
  263. y2 = y1 + dy;
  264. x2 = min(x2, this->width - 1);
  265. y2 = min(y2, this->height - 1);
  266. dx = x2 - x1;
  267. dy = y2 - y1;
  268. int memsize = dx * dy * this->channels;
  269. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  270. int x_source, y_source;
  271. stbi_uc* p_target;
  272. stbi_uc* p_source;
  273. for (int x = x1; x < x2; ++x)
  274. for (int y = y1; y < y2; ++y)
  275. {
  276. p_target = odata + (this->channels * ((y - y1) * dx + (x - x1)));
  277. p_source = this->rgb_image + (this->channels * (y * this->width + x));
  278. for (int channels = 0; channels < this->channels; ++channels)
  279. p_target[channels] = p_source[channels];
  280. }
  281. // stbi_write_jpg(_template1.c_str(), dx, dy, this->channels, odata, 0);
  282. stbi_write_bmp(_template1.c_str(), dx, dy, this->channels, odata);
  283. stbi_image_free(odata);
  284. }