CFindTemplate.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include "CFindTemplate.h"
  2. #include "Helper.h"
  3. #include "ClassLogFile.h"
  4. #include "esp_system.h"
  5. #define _USE_MATH_DEFINES
  6. #include <math.h>
  7. #include <algorithm>
  8. #define _ESP32_PSRAM
  9. using namespace std;
  10. #define GET_MEMORY malloc
  11. /*
  12. CResizeImage::CResizeImage(std::string _image, int _new_dx, int _new_dy)
  13. {
  14. CImageBasis::CImageBasis(_image);
  15. }
  16. */
  17. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  18. {
  19. stbi_uc* p_source;
  20. p_source = this->rgb_image + (this->channels * (y * this->width + x));
  21. return p_source[ch];
  22. }
  23. void CResizeImage::Resize(int _new_dx, int _new_dy)
  24. {
  25. int memsize = _new_dx * _new_dy * this->channels;
  26. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  27. stbir_resize_uint8(this->rgb_image, this->width, this->height, 0, odata, _new_dx, _new_dy, 0, this->channels);
  28. stbi_image_free(this->rgb_image);
  29. this->rgb_image = (unsigned char*)GET_MEMORY(memsize);
  30. this->memCopy(odata, this->rgb_image, memsize);
  31. this->width = _new_dx;
  32. this->height = _new_dy;
  33. stbi_image_free(odata);
  34. }
  35. void CRotate::Rotate(float _angle, int _centerx, int _centery)
  36. {
  37. float m[2][3];
  38. float x_center = _centerx;
  39. float y_center = _centery;
  40. _angle = _angle / 180 * M_PI;
  41. m[0][0] = cos(_angle);
  42. m[0][1] = sin(_angle);
  43. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  44. m[1][0] = -m[0][1];
  45. m[1][1] = m[0][0];
  46. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  47. int memsize = this->width * this->height * this->channels;
  48. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  49. int x_source, y_source;
  50. stbi_uc* p_target;
  51. stbi_uc* p_source;
  52. for (int x = 0; x < this->width; ++x)
  53. for (int y = 0; y < this->height; ++y)
  54. {
  55. p_target = odata + (this->channels * (y * this->width + x));
  56. x_source = int(m[0][0] * x + m[0][1] * y);
  57. y_source = int(m[1][0] * x + m[1][1] * y);
  58. x_source += int(m[0][2]);
  59. y_source += int(m[1][2]);
  60. if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
  61. {
  62. p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
  63. for (int channels = 0; channels < this->channels; ++channels)
  64. p_target[channels] = p_source[channels];
  65. }
  66. else
  67. {
  68. for (int channels = 0; channels < this->channels; ++channels)
  69. p_target[channels] = 255;
  70. }
  71. }
  72. // memcpy(this->rgb_image, odata, memsize);
  73. this->memCopy(odata, this->rgb_image, memsize);
  74. stbi_image_free(odata);
  75. }
  76. void CRotate::Rotate(float _angle)
  77. {
  78. this->Rotate(_angle, this->width / 2, this->height / 2);
  79. }
  80. void CRotate::Translate(int _dx, int _dy)
  81. {
  82. int memsize = this->width * this->height * this->channels;
  83. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  84. int x_source, y_source;
  85. stbi_uc* p_target;
  86. stbi_uc* p_source;
  87. for (int x = 0; x < this->width; ++x)
  88. for (int y = 0; y < this->height; ++y)
  89. {
  90. p_target = odata + (this->channels * (y * this->width + x));
  91. x_source = x - _dx;
  92. y_source = y - _dy;
  93. if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
  94. {
  95. p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
  96. for (int channels = 0; channels < this->channels; ++channels)
  97. p_target[channels] = p_source[channels];
  98. }
  99. else
  100. {
  101. for (int channels = 0; channels < this->channels; ++channels)
  102. p_target[channels] = 255;
  103. }
  104. }
  105. // memcpy(this->rgb_image, odata, memsize);
  106. this->memCopy(odata, this->rgb_image, memsize);
  107. stbi_image_free(odata);
  108. }
  109. CFindTemplate::CFindTemplate(std::string _image)
  110. {
  111. this->channels = 1;
  112. this->rgb_image = stbi_load(_image.c_str(), &(this->width), &(this->height), &(this->bpp), this->channels);
  113. }
  114. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y)
  115. {
  116. this->FindTemplate(_template, found_x, found_y, 0, 0);
  117. }
  118. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy)
  119. {
  120. uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, this->channels);
  121. int ow, ow_start, ow_stop;
  122. int oh, oh_start, oh_stop;
  123. if (_dx == 0)
  124. {
  125. _dx = this->width;
  126. *found_x = 0;
  127. }
  128. if (_dy == 0)
  129. {
  130. _dy = this->height;
  131. *found_y = 0;
  132. }
  133. ow_start = *found_x - _dx;
  134. ow_start = std::max(ow_start, 0);
  135. ow_stop = *found_x + _dx;
  136. if ((ow_stop + tpl_width) > this->width)
  137. ow_stop = this->width - tpl_width;
  138. ow = ow_stop - ow_start + 1;
  139. oh_start = *found_y - _dy;
  140. oh_start = std::max(oh_start, 0);
  141. oh_stop = *found_y + _dy;
  142. if ((oh_stop + tpl_height) > this->height)
  143. oh_stop = this->height - tpl_height;
  144. oh = oh_stop - oh_start + 1;
  145. uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * this->channels);
  146. double aktSAD;
  147. double minSAD = pow(tpl_width * tpl_height * 255, 2);
  148. for (int xouter = ow_start; xouter <= ow_stop; xouter++)
  149. for (int youter = oh_start; youter <= oh_stop; ++youter)
  150. {
  151. aktSAD = 0;
  152. for (int tpl_x = 0; tpl_x < tpl_width; tpl_x++)
  153. for (int tpl_y = 0; tpl_y < tpl_height; tpl_y++)
  154. {
  155. stbi_uc* p_org = this->rgb_image + (this->channels * ((youter + tpl_y) * this->width + (xouter + tpl_x)));
  156. stbi_uc* p_tpl = rgb_template + (this->channels * (tpl_y * tpl_width + tpl_x));
  157. aktSAD += pow(p_tpl[0] - p_org[0], 2);
  158. }
  159. stbi_uc* p_out = odata + (this->channels * ((youter - oh_start) * ow + (xouter - ow_start)));
  160. p_out[0] = int(sqrt(aktSAD / (tpl_width * tpl_height)));
  161. if (aktSAD < minSAD)
  162. {
  163. minSAD = aktSAD;
  164. *found_x = xouter;
  165. *found_y = youter;
  166. }
  167. }
  168. stbi_write_bmp("sdcard\\find.bmp", ow, oh, this->channels, odata);
  169. stbi_image_free(odata);
  170. stbi_image_free(rgb_template);
  171. }
  172. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout)
  173. {
  174. this->FindTemplate(_template, found_x, found_y);
  175. this->SaveToFile(_imageout);
  176. }
  177. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout)
  178. {
  179. this->FindTemplate(_template, found_x, found_y, _dx, _dy);
  180. this->SaveToFile(_imageout);
  181. }
  182. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  183. {
  184. #ifdef _ESP32_PSRAM
  185. for (int i = 0; i < _size; ++i)
  186. *(_target + i) = *(_source + i);
  187. #else
  188. memcpy(_target, _source, _size);
  189. #endif
  190. }
  191. bool CImageBasis::isInImage(int x, int y)
  192. {
  193. if ((x < 0) || (x > this->width - 1))
  194. return false;
  195. if ((y < 0) || (y > this->height- 1))
  196. return false;
  197. return true;
  198. }
  199. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  200. {
  201. stbi_uc* p_source;
  202. p_source = this->rgb_image + (this->channels * (y * this->width + x));
  203. p_source[0] = r;
  204. if (this-> channels > 2)
  205. {
  206. p_source[1] = g;
  207. p_source[2] = b;
  208. }
  209. }
  210. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  211. {
  212. int zwx1, zwx2, zwy1, zwy2;
  213. int _x, _y, _thick;
  214. zwx1 = x - thickness + 1;
  215. zwx2 = x + dx + thickness - 1;
  216. zwy1 = y;
  217. zwy2 = y;
  218. for (_thick = 0; _thick < thickness; _thick++)
  219. for (_x = zwx1; _x <= zwx2; ++_x)
  220. for (_y = zwy1; _y <= zwy2; _y++)
  221. if (isInImage(_x, _y))
  222. setPixelColor(_x, _y - _thick, r, g, b);
  223. zwx1 = x - thickness + 1;
  224. zwx2 = x + dx + thickness - 1;
  225. zwy1 = y + dy;
  226. zwy2 = y + dy;
  227. for (_thick = 0; _thick < thickness; _thick++)
  228. for (_x = zwx1; _x <= zwx2; ++_x)
  229. for (_y = zwy1; _y <= zwy2; _y++)
  230. if (isInImage(_x, _y))
  231. setPixelColor(_x, _y + _thick, r, g, b);
  232. zwx1 = x;
  233. zwx2 = x;
  234. zwy1 = y;
  235. zwy2 = y + dy;
  236. for (_thick = 0; _thick < thickness; _thick++)
  237. for (_x = zwx1; _x <= zwx2; ++_x)
  238. for (_y = zwy1; _y <= zwy2; _y++)
  239. if (isInImage(_x, _y))
  240. setPixelColor(_x - _thick, _y, r, g, b);
  241. zwx1 = x + dx;
  242. zwx2 = x + dx;
  243. zwy1 = y;
  244. zwy2 = y + dy;
  245. for (_thick = 0; _thick < thickness; _thick++)
  246. for (_x = zwx1; _x <= zwx2; ++_x)
  247. for (_y = zwy1; _y <= zwy2; _y++)
  248. if (isInImage(_x, _y))
  249. setPixelColor(_x + _thick, _y, r, g, b);
  250. }
  251. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  252. {
  253. int _x, _y, _thick;
  254. int _zwy1, _zwy2;
  255. thickness = (thickness-1) / 2;
  256. for (_thick = 0; _thick <= thickness; ++_thick)
  257. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  258. {
  259. if (x2 == x1)
  260. {
  261. _zwy1 = y1;
  262. _zwy2 = y2;
  263. }
  264. else
  265. {
  266. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  267. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  268. }
  269. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  270. if (isInImage(_x, _y))
  271. setPixelColor(_x, _y, r, g, b);
  272. }
  273. }
  274. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  275. {
  276. float deltarad, aktrad;
  277. int _thick, _x, _y;
  278. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  279. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  280. for (_thick = 0; _thick < thickness; ++_thick)
  281. {
  282. _x = sin(aktrad) * (rad + _thick) + x1;
  283. _y = cos(aktrad) * (rad + _thick) + y1;
  284. if (isInImage(_x, _y))
  285. setPixelColor(_x, _y, r, g, b);
  286. }
  287. }
  288. CImageBasis::CImageBasis()
  289. {
  290. this->externalImage = false;
  291. }
  292. CImageBasis::CImageBasis(std::string _image)
  293. {
  294. channels = 3;
  295. externalImage = false;
  296. filename = _image;
  297. long freebefore = esp_get_free_heap_size();
  298. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  299. if (rgb_image == NULL)
  300. LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
  301. // printf("CImageBasis after load\n");
  302. // printf("w %d, h %d, b %d, c %d", this->width, this->height, this->bpp, this->channels);
  303. }
  304. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  305. {
  306. this->rgb_image = _rgb_image;
  307. this->channels = _channels;
  308. this->width = _width;
  309. this->height = _height;
  310. this->bpp = _bpp;
  311. this->externalImage = true;
  312. }
  313. CImageBasis::~CImageBasis()
  314. {
  315. if (!this->externalImage)
  316. stbi_image_free(this->rgb_image);
  317. }
  318. void CImageBasis::SaveToFile(std::string _imageout)
  319. {
  320. string typ = getFileType(_imageout);
  321. if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
  322. {
  323. stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
  324. }
  325. if ((typ == "bmp") || (typ == "BMP"))
  326. {
  327. stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
  328. }
  329. // stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
  330. // stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
  331. }
  332. 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)
  333. {
  334. int dx, dy;
  335. int r0_x, r0_y, r1_x, r1_y;
  336. CFindTemplate* ft = new CFindTemplate(this->filename);
  337. r0_x = ref0_x;
  338. r0_y = ref0_y;
  339. ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
  340. t0_dx = ft->tpl_width;
  341. t0_dy = ft->tpl_height;
  342. r1_x = ref1_x;
  343. r1_y = ref1_y;
  344. ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
  345. t1_dx = ft->tpl_width;
  346. t1_dy = ft->tpl_height;
  347. delete ft;
  348. dx = ref0_x - r0_x;
  349. dy = ref0_y - r0_y;
  350. r0_x += dx;
  351. r0_y += dy;
  352. r1_x += dx;
  353. r1_y += dy;
  354. float w_org, w_ist, d_winkel;
  355. w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
  356. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  357. d_winkel = (w_org - w_ist) * 180 / M_PI;
  358. if (imageROI.length() > 0)
  359. {
  360. CImageBasis* imgzw = new CImageBasis(this->filename);
  361. imgzw->drawRect(r0_x, r0_y, t0_dx, t0_dy, 255, 0, 0, 2);
  362. imgzw->drawRect(r1_x, r1_y, t1_dx, t1_dy, 255, 0, 0, 2);
  363. imgzw->SaveToFile(imageROI);
  364. printf("Alignment: alignment ROI created: %s\n", imageROI.c_str());
  365. delete imgzw;
  366. }
  367. string zw = "\tdx:\t" + to_string(dx) + "\tdy:\t" + to_string(dy) + "\td_winkel:\t" + to_string(d_winkel);
  368. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  369. CRotate rt(this->rgb_image, this->channels, this->width, this->height, this->bpp);
  370. rt.Translate(dx, dy);
  371. rt.Rotate(d_winkel, ref0_x, ref0_y);
  372. printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
  373. }
  374. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  375. {
  376. int x2, y2;
  377. x2 = x1 + dx;
  378. y2 = y1 + dy;
  379. x2 = min(x2, this->width - 1);
  380. y2 = min(y2, this->height - 1);
  381. dx = x2 - x1;
  382. dy = y2 - y1;
  383. int memsize = dx * dy * this->channels;
  384. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  385. int x_source, y_source;
  386. stbi_uc* p_target;
  387. stbi_uc* p_source;
  388. for (int x = x1; x < x2; ++x)
  389. for (int y = y1; y < y2; ++y)
  390. {
  391. p_target = odata + (this->channels * ((y - y1) * dx + (x - x1)));
  392. p_source = this->rgb_image + (this->channels * (y * this->width + x));
  393. for (int channels = 0; channels < this->channels; ++channels)
  394. p_target[channels] = p_source[channels];
  395. }
  396. // stbi_write_jpg(_template1.c_str(), dx, dy, this->channels, odata, 0);
  397. stbi_write_bmp(_template1.c_str(), dx, dy, this->channels, odata);
  398. stbi_image_free(odata);
  399. }