CFindTemplate.cpp 14 KB

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