CFindTemplate.cpp 14 KB

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