ImageLoc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #ifndef __IMAGELOC_H_
  3. #define __IMAGELOC_H_
  4. /*
  5. 常见的图像算法,例如图像查找,颜色序列匹配(多点着色)
  6. 由于ocr与图像查找类似,故也在类ImageLoc中实现
  7. */
  8. #include <vector>
  9. #include "optype.h"
  10. #include <string>
  11. //#include "Dict.h"
  12. #include "color.h"
  13. #include "ThreadPool.h"
  14. #include "Image.hpp"
  15. inline int HEX2INT(wchar_t c) {
  16. if (L'0' <= c && c <= L'9')
  17. return c - L'0';
  18. if (L'A' <= c && c <= L'Z')
  19. return c - L'A' + 10;
  20. if (L'a' <= c && c <= L'z')
  21. return c - L'a' + 10;
  22. return 0;
  23. }
  24. #define SET_BIT(x, idx) (x |= 1u << (idx))
  25. #define GET_BIT(x, idx) ((x >> (idx)) & 1u)
  26. using img_names = std::vector<TString>;
  27. //检查是否为透明图
  28. int check_transparent(Image* img);
  29. //获取匹配点
  30. void get_match_points(const Image& img, vector<int>& points);
  31. //generate next index for kmp
  32. void gen_next(const Image& img, vector<int>& next);
  33. //sum of all pixels
  34. int inline sum(uchar* begin, uchar* end) {
  35. int s = 0;
  36. while (begin != end)
  37. s += *begin++;
  38. return s;
  39. }
  40. void extractConnectivity(const ImageBin& src, int threshold, std::vector<ImageBin>& out);
  41. struct gray_diff_t {
  42. gray_diff_t(color_df_t const& cd) :gray(cd.color.toGray()), diff(cd.df.toGray()) {
  43. }
  44. unsigned char gray;
  45. unsigned char diff;
  46. };
  47. /*
  48. 此类用于实现一些图像功能,如图像定位,简单ocr等
  49. */
  50. class ImageBase
  51. {
  52. public:
  53. const static int _max_return_obj_ct = 1800;
  54. using vcolor_diff_t = vector<color_df_t>;//rgb color-diff
  55. using vgray_diff_t = vector<gray_diff_t>;//gray
  56. ImageBase();
  57. ~ImageBase();
  58. //brief:输入图像,建立图形矩阵,在图像操作前调用
  59. //image_data: 4子节对齐的像素指针
  60. //widht: 图像宽度
  61. //hegith: h
  62. //x1,y1,x2,y2 拷贝区域
  63. //type: 输入类型,type=0表示正常输入,为-1时表示倒置输入
  64. //long input_image(byte* psrc, int cols, int height,long x1,long y1,long x2,long y2, int type = 0);
  65. void set_offset(int x1, int y1);
  66. long is_valid(long x, long y)
  67. {
  68. return x >= 0 && y >= 0 && x < _src.width&& y < _src.height;
  69. }
  70. long GetPixel(long x, long y, color_t& cr);
  71. long CmpColor(color_t color, std::vector<color_df_t>& colors, double sim);
  72. long FindColor(std::vector<color_df_t>& colors, int dir, long& x, long& y);
  73. long FindColorEx(std::vector<color_df_t>& colors, TString& retstr);
  74. long FindMultiColor(std::vector<color_df_t>& first_color, std::vector<pt_cr_df_t>& offset_color, double sim, long dir, long& x, long& y);
  75. long FindMultiColorEx(std::vector<color_df_t>& first_color, std::vector<pt_cr_df_t>& offset_color, double sim, long dir, TString& retstr);
  76. long FindPic(std::vector<Image*>& pics, color_t dfcolor, double sim, long& x, long& y);
  77. long FindPicTh(std::vector<Image*>& pics, color_t dfcolor, double sim, long& x, long& y);
  78. long FindPicEx(std::vector<Image*>& pics, color_t dfcolor, double sim, vpoint_desc_t& vpd);
  79. long FindPicExTh(std::vector<Image*>& pics, color_t dfcolor, double sim, vpoint_desc_t& vpd);
  80. long FindColorBlock(double sim, long count, long height, long width, long& x, long& y);
  81. long FindColorBlockEx(double sim, long count, long height, long width, TString& retstr);
  82. //描述:查找目标图像中的直线
  83. //输入:精度
  84. //输出:outStr:直线描述[法线角度,直线到原点的距离];ret:该直线上的点的数量
  85. long FindLine(double sim, TString& outStr);
  86. private:
  87. //rgb像素匹配
  88. template<bool nodfcolor>
  89. long simple_match(long x, long y, Image* timg, color_t dfcolor, int tnrom, double sim);
  90. //透明图匹配
  91. template<bool nodfcolor>
  92. long trans_match(long x, long y, Image* timg, color_t dfcolor, vector<uint>points, int max_error);
  93. //灰度匹配
  94. long real_match(long x, long y, ImageBin* timg, int tnorm, double sim);
  95. //记录和
  96. void record_sum(const ImageBin& gray);
  97. //[x1,x2),[y1,y2)
  98. int region_sum(int x1, int y1, int x2, int y2);
  99. int get_bk_color(inputbin bin);
  100. //垂直方向投影,投到x轴
  101. void binshadowx(const rect_t& rc, std::vector<rect_t>& out_put);
  102. //水平方向投影,投到(y)轴
  103. void binshadowy(const rect_t& rc, std::vector<rect_t>& out_put);
  104. public:
  105. /*
  106. if(abs(cr-src)<=df) pixel=1;
  107. else pixel=0;
  108. */
  109. void bgr2binary(vector<color_df_t>& colors);
  110. //二值化 auto
  111. void bgr2binarybk(const vector<color_df_t>& bk_colors);
  112. //图像裁剪
  113. void bin_image_cut(int min_word_h, const rect_t& inrc, rect_t& outrc);
  114. void get_rois(int min_word_h, std::vector<rect_t>& vroi);
  115. public:
  116. Image _src;
  117. ImageBin _gray;
  118. ImageBin _record;
  119. ImageBin _binary;
  120. Image _sum;
  121. private:
  122. //起始点
  123. int _x1, _y1;
  124. //偏移
  125. int _dx, _dy;
  126. ThreadPool m_threadPool;
  127. };
  128. #endif