CFindTemplate.cpp 16 KB

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