ImageProc.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //#include "stdafx.h"
  2. #include "ImageProc.h"
  3. #include "helpfunc.h"
  4. #include <fstream>
  5. #include <bitset>
  6. #include <algorithm>
  7. #include <sstream>
  8. ImageProc::ImageProc()
  9. {
  10. _enable_cache = 1;
  11. }
  12. ImageProc::~ImageProc()
  13. {
  14. }
  15. long ImageProc::Capture(const TString& file)
  16. {
  17. TString fpath = file;
  18. if (fpath.find(_T('\\')) == -1)
  19. fpath = _curr_path + _T("\\") + fpath;
  20. return _src.write(fpath.c_str());
  21. }
  22. long ImageProc::CmpColor(long x, long y, const TString& scolor, double sim)
  23. {
  24. std::vector<color_df_t> vcolor;
  25. str2colordfs(scolor, vcolor);
  26. return ImageBase::CmpColor(_src.at<color_t>(0, 0), vcolor, sim);
  27. }
  28. long ImageProc::FindColor(const TString& color, double sim, long dir, long& x, long& y)
  29. {
  30. std::vector<color_df_t> colors;
  31. str2colordfs(color, colors);
  32. //setlog("%s cr size=%d",colors[0].color.tostr().data(), colors.size());
  33. //setlog("sim:,dir:%d", dir);
  34. return ImageBase::FindColor(colors, dir, x, y);
  35. }
  36. long ImageProc::FindColoEx(const TString& color, double sim, long dir, TString& retstr)
  37. {
  38. std::vector<color_df_t> colors;
  39. str2colordfs(color, colors);
  40. return ImageBase::FindColorEx(colors, retstr);
  41. }
  42. long ImageProc::FindMultiColor(const TString& first_color, const TString& offset_color, double sim, long dir, long& x, long& y)
  43. {
  44. std::vector<color_df_t> vfirst_color;
  45. str2colordfs(first_color, vfirst_color);
  46. std::vector<TString> vseconds;
  47. split(offset_color, vseconds, _T(","));
  48. std::vector<pt_cr_df_t> voffset_cr;
  49. pt_cr_df_t tp;
  50. for (auto& it : vseconds)
  51. {
  52. size_t id1, id2;
  53. id1 = it.find(_T('|'));
  54. id2 = (id1 == TString::npos ? TString::npos : it.find(L'|', id1));
  55. if (id2 != TString::npos)
  56. {
  57. sscanf(it.c_str(), _T("%d|%d"), &tp.x, &tp.y);
  58. if (id2 + 1 != it.length())
  59. str2colordfs(it.substr(id2 + 1), tp.crdfs);
  60. else
  61. break;
  62. voffset_cr.push_back(tp);
  63. }
  64. }
  65. return ImageBase::FindMultiColor(vfirst_color, voffset_cr, sim, dir, x, y);
  66. }
  67. long ImageProc::FindMultiColorEx(const TString& first_color, const TString& offset_color, double sim, long dir, TString& retstr)
  68. {
  69. std::vector<color_df_t> vfirst_color;
  70. str2colordfs(first_color, vfirst_color);
  71. std::vector<TString> vseconds;
  72. split(offset_color, vseconds, _T(","));
  73. std::vector<pt_cr_df_t> voffset_cr;
  74. pt_cr_df_t tp;
  75. for (auto& it : vseconds)
  76. {
  77. size_t id1, id2;
  78. id1 = it.find(_T('|'));
  79. id2 = (id1 == TString::npos ? TString::npos : it.find(_T('|'), id1));
  80. if (id2 != TString::npos)
  81. {
  82. _stscanf(it.c_str(), _T("%d|%d"), &tp.x, &tp.y);
  83. if (id2 + 1 != it.length())
  84. str2colordfs(it.substr(id2 + 1), tp.crdfs);
  85. else
  86. break;
  87. voffset_cr.push_back(tp);
  88. }
  89. }
  90. return ImageBase::FindMultiColorEx(vfirst_color, voffset_cr, sim, dir, retstr);
  91. }
  92. //图形定位
  93. long ImageProc::FindPic(const TString& files, const TString& delta_colors, double sim, long dir, long& x, long& y)
  94. {
  95. vector<Image*> vpic;
  96. //vector<color_t> vcolor;
  97. color_t dfcolor;
  98. vector<TString> vpic_name;
  99. files2mats(files, vpic, vpic_name);
  100. dfcolor.str2color(delta_colors);
  101. //str2colors(delta_colors, vcolor);
  102. sim = 0.5 + sim / 2;
  103. //long ret = ImageBase::FindPic(vpic, dfcolor, sim, x, y);
  104. long ret = ImageBase::FindPicTh(vpic, dfcolor, sim, x, y);
  105. //清理缓存
  106. if (!_enable_cache)
  107. _pic_cache.clear();
  108. return ret;
  109. }
  110. //
  111. long ImageProc::FindPicEx(const TString& files, const TString& delta_colors, double sim, long dir, TString& retstr, bool returnID)
  112. {
  113. vector<Image*> vpic;
  114. vpoint_desc_t vpd;
  115. //vector<color_t> vcolor;
  116. color_t dfcolor;
  117. vector<TString> vpic_name;
  118. files2mats(files, vpic, vpic_name);
  119. dfcolor.str2color(delta_colors);
  120. sim = 0.5 + sim / 2;
  121. long ret = ImageBase::FindPicExTh(vpic, dfcolor, sim, vpd);
  122. #ifdef UNICODE
  123. std::wstringstream ss(std::wstringstream::in | std::wstringstream::out);
  124. #else
  125. std::stringstream ss(std::stringstream::in | std::stringstream::out);
  126. #endif
  127. if (returnID)
  128. {
  129. for (auto& it : vpd)
  130. {
  131. ss << it.id << _T(",") << it.pos << _T("|");
  132. }
  133. }
  134. else
  135. {
  136. for (auto& it : vpd)
  137. {
  138. ss << vpic_name[it.id] << _T(",") << it.pos << _T("|");
  139. }
  140. }
  141. //清理缓存
  142. if (!_enable_cache)
  143. _pic_cache.clear();
  144. retstr = ss.str();
  145. if (vpd.size())
  146. retstr.pop_back();
  147. return ret;
  148. }
  149. long ImageProc::FindColorBlock(const TString& color, double sim, long count, long height, long width, long& x, long& y)
  150. {
  151. vector<color_df_t> colors;
  152. if (str2colordfs(color, colors) == 0)
  153. {
  154. bgr2binary(colors);
  155. }
  156. else
  157. {
  158. bgr2binarybk(colors);
  159. }
  160. return ImageBase::FindColorBlock(sim, count, height, width, x, y);
  161. }
  162. long ImageProc::FindColorBlockEx(const TString& color, double sim, long count, long height, long width, TString& retstr)
  163. {
  164. vector<color_df_t> colors;
  165. if (str2colordfs(color, colors) == 0)
  166. {
  167. bgr2binary(colors);
  168. }
  169. else
  170. {
  171. bgr2binarybk(colors);
  172. }
  173. return ImageBase::FindColorBlockEx(sim, count, height, width, retstr);
  174. }
  175. TString ImageProc::GetColor(long x, long y)
  176. {
  177. color_t cr;
  178. if (ImageBase::GetPixel(x, y, cr))
  179. {
  180. return cr.tostr();
  181. }
  182. else
  183. {
  184. return _T("");
  185. }
  186. }
  187. int ImageProc::str2colordfs(const TString& color_str, std::vector<color_df_t>& colors)
  188. {
  189. std::vector<TString> vstr, vstr2;
  190. color_df_t cr;
  191. colors.clear();
  192. int ret = 0;
  193. if (color_str.empty())
  194. { //default
  195. return 1;
  196. }
  197. if (color_str[0] == _T('@'))
  198. { //bk color info
  199. ret = 1;
  200. }
  201. split(ret ? color_str.substr(1) : color_str, vstr, _T("|"));
  202. for (auto& it : vstr)
  203. {
  204. split(it, vstr2, _T("-"));
  205. cr.color.str2color(vstr2[0]);
  206. cr.df.str2color(vstr2.size() == 2 ? vstr2[1] : _T("000000"));
  207. colors.push_back(cr);
  208. }
  209. return ret;
  210. }
  211. void ImageProc::str2colors(const TString& color, std::vector<color_t>& vcolor)
  212. {
  213. std::vector<TString> vstr, vstr2;
  214. color_t cr;
  215. vcolor.clear();
  216. split(color, vstr, _T("|"));
  217. for (auto& it : vstr)
  218. {
  219. cr.str2color(it);
  220. vcolor.push_back(cr);
  221. }
  222. }
  223. long ImageProc::LoadPic(const TString& files)
  224. {
  225. //std::vector<TString>vstr, vstr2;
  226. std::vector<TString> vstr;
  227. int loaded = 0;
  228. split(files, vstr, _T("|"));
  229. TString tp;
  230. for (auto& it : vstr)
  231. {
  232. //路径转化
  233. if (!Path2GlobalPath(it, _curr_path, tp))
  234. continue;
  235. //先在缓存中查找
  236. if (!_pic_cache.count(tp))
  237. {
  238. _pic_cache[tp].read(tp.data());
  239. }
  240. //已存在于缓存中的文件也算加载成功
  241. loaded++;
  242. }
  243. return loaded;
  244. }
  245. long ImageProc::FreePic(const TString& files)
  246. {
  247. std::vector<TString> vstr;
  248. int loaded = 0;
  249. split(files, vstr, _T("|"));
  250. TString tp;
  251. for (auto& it : vstr)
  252. {
  253. //看当前目录
  254. auto cache_it = _pic_cache.find(it);
  255. //没查到再看一下资源目录
  256. if (cache_it == _pic_cache.end())
  257. {
  258. cache_it = _pic_cache.find(_curr_path + _T("\\") + it);
  259. }
  260. //查到了就释放
  261. if (cache_it != _pic_cache.end())
  262. {
  263. cache_it->second.release();
  264. _pic_cache.erase(cache_it);
  265. loaded++;
  266. }
  267. }
  268. return loaded;
  269. }
  270. long ImageProc::LoadMemPic(const TString& file_name, void* data, long size)
  271. {
  272. try
  273. {
  274. if (!_pic_cache.count(file_name))
  275. {
  276. _pic_cache[file_name].read(data, size);
  277. }
  278. }
  279. catch (...)
  280. {
  281. return 0;
  282. }
  283. return 1;
  284. }
  285. void ImageProc::files2mats(const TString& files, std::vector<Image*>& vpic, std::vector<TString>& vstr)
  286. {
  287. //std::vector<TString>vstr, vstr2;
  288. Image* pm;
  289. vpic.clear();
  290. split(files, vstr, _T("|"));
  291. TString tp;
  292. for (auto& it : vstr)
  293. {
  294. //先在缓存中查找是否已加载,包括从内存中加载的文件
  295. if (_pic_cache.count(it))
  296. {
  297. pm = &_pic_cache[it];
  298. }
  299. else
  300. {
  301. //路径转化
  302. if (!Path2GlobalPath(it, _curr_path, tp))
  303. continue;
  304. //再检测一次,包括绝对路径的文件
  305. if (_pic_cache.count(tp))
  306. {
  307. pm = &_pic_cache[tp];
  308. }
  309. else
  310. {
  311. _pic_cache[tp].read(tp.data());
  312. pm = &_pic_cache[tp];
  313. }
  314. }
  315. vpic.push_back(pm);
  316. }
  317. }
  318. long ImageProc::FindLine(const TString& color, double sim, TString& retStr)
  319. {
  320. retStr.clear();
  321. vector<color_df_t> colors;
  322. if (str2colordfs(color, colors) == 0)
  323. {
  324. bgr2binary(colors);
  325. }
  326. else
  327. {
  328. bgr2binarybk(colors);
  329. }
  330. if (sim < 0. || sim > 1.)
  331. sim = 1.;
  332. _src.write(_T("_src.bmp"));
  333. _gray.write(_T("gray.bmp"));
  334. _binary.write(_T("_binary.bmp"));
  335. return ImageBase::FindLine(sim, retStr);
  336. }