optimal_angle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include <opencv.hpp>
  3. #include <map>
  4. #include <opencv2\imgproc\imgproc.hpp>
  5. #include "data_def_api.h"
  6. #include "logger.h"
  7. #include "imstorage_manager.h"
  8. using namespace std;
  9. //using namespace cv;
  10. namespace graft_cv{
  11. // class COptimalAngle
  12. // 1. find the max angle
  13. // 2. find the clamp position
  14. // 条件:图像0度到180度间,并第一帧为0度时刻,最后一帧为180时刻
  15. // 采用的方法:拼接数据(360度范围),通过找到两个最小点,此两点间的数据用
  16. // 二次曲线拟合,得到对称轴x坐标,即为最优角度
  17. //class COptimalAngle
  18. //{
  19. //public:
  20. // COptimalAngle(ConfigParam&, CGcvLogger* pLog=0);
  21. // virtual ~COptimalAngle(void);
  22. // int reset();
  23. // int append(ImgInfo*,
  24. // PositionInfo& posinfo
  25. // );
  26. // double infer(PositionInfo& posinfo);// calculate the optimal angle
  27. //
  28. // //fit optimal angle based m_an2width
  29. // virtual double angle_fit(map<int, int>&,bool is_base=false);
  30. // virtual double angle_fit_base(map<int, int>&);// for base matter
  31. // void set_image_saver(CImStoreManager** ppis)
  32. // {
  33. // m_ppImgSaver=ppis;
  34. // }
  35. //protected:
  36. // //global configure parameters
  37. // ConfigParam& m_cparam;
  38. // CGcvLogger* m_pLogger;
  39. // string m_patch_id;
  40. // CImStoreManager** m_ppImgSaver;
  41. // int m_imgIndex;
  42. // string m_imgId;
  43. //
  44. // // binary image
  45. // Mat m_binImg;
  46. // Mat m_binImgBase;
  47. //
  48. // //返回图片,用于调试
  49. // ImgInfo* m_pImginfo;
  50. // ImgInfo* m_pImginfoBase;
  51. //
  52. // // 角度-叶展宽度(像素)
  53. // map<int, int> m_an2width;
  54. // map<int, int> m_an2widthBase;
  55. // // 图像处理函数
  56. // // 返回min_idx左侧x,max_idx右侧x
  57. // int imgproc(
  58. // Mat&,
  59. // vector<int>& hist,
  60. // int& min_idx,
  61. // int& max_idx);
  62. // int imgprocBase(
  63. // Mat&,
  64. // vector<int>& hist,
  65. // int& min_idx,
  66. // int& max_idx);
  67. //
  68. // // 清理释放m_pImginfo
  69. // void clear_imginfo();
  70. //
  71. // //茎分叉点y值系列
  72. // vector<int> m_fork_ys;
  73. //
  74. // //茎最低点y值系列
  75. // vector<int> m_end_ys;
  76. //
  77. // //叶展宽度系列
  78. // vector<int>m_widths;
  79. //
  80. // //all image size
  81. // int m_width;
  82. // int m_height;
  83. //};
  84. //COptimalAnglePart, 继承自COptimalAngle
  85. //条件:图像0度到90度间,并保证>=90度范围
  86. //方法:0-90度范围必然存在最大值或最小值,通过最大最小值局部数据
  87. // 插值(二次曲线拟合)得到最大角度计算
  88. //class COptimalAnglePart: public COptimalAngle{
  89. //public:
  90. // COptimalAnglePart(ConfigParam&, CGcvLogger*pLog=0);
  91. // virtual ~COptimalAnglePart();
  92. // //fit optimal angle based m_an2width
  93. // double angle_fit(map<int, int>&,bool is_base=false);
  94. // double angle_fit_base(map<int, int>&);// for base matter
  95. //protected:
  96. // //zero_cross_detect()
  97. // //过零点检测,用于确定是否单调(增或减),局部最小,局部最大,
  98. // //并返回局部极值index
  99. // void zero_cross_detect(
  100. // vector<int>&d_sign, //input
  101. // bool& is_local_max, // whether exists local max
  102. // int& local_limit_idx // local limit index of original vector
  103. // );
  104. // double limit_min_angle(
  105. // vector<double>&x, //sorted, ascending
  106. // vector<double>&y,
  107. // size_t min_idx
  108. // );
  109. //
  110. // // 计算y = k1*x + b1和y = k2*x + b2两条直线相交交点,x坐标
  111. // double line_cross(
  112. // double k1,
  113. // double b1,
  114. // double k2,
  115. // double b2);
  116. // //
  117. // double opt_angle_part_impl(
  118. // vector<double>&x, //sorted, ascending
  119. // vector<double>&y,
  120. // bool is_base=false);
  121. //};
  122. // CCotyledonAngle
  123. // 顶部拍照砧木,通过子叶角度识别,提供机构旋转角度
  124. class CCotyledonAngle{
  125. public:
  126. CCotyledonAngle(ConfigParam&, CGcvLogger* pLog=0);
  127. virtual ~CCotyledonAngle();
  128. int angle_recognize(ImgInfo*,
  129. PositionInfo& posinfo
  130. );
  131. void set_image_saver(CImStoreManager** ppis)
  132. {
  133. m_ppImgSaver=ppis;
  134. }
  135. protected:
  136. //global configure parameters
  137. ConfigParam& m_cparam;
  138. CGcvLogger* m_pLogger;
  139. string m_imgId;
  140. CImStoreManager** m_ppImgSaver;
  141. //all image size
  142. cv::Mat m_grayImg;
  143. cv::Mat m_binImg;
  144. int m_width;
  145. int m_height;
  146. cv::Mat m_openImg; // for save
  147. //返回图片,用于调试
  148. ImgInfo* m_pImginfoOpen;//fork-y, right-x
  149. ImgInfo* m_pImginfoAngle;//corners, reference-point, candidate box
  150. //
  151. typedef struct {
  152. double area;
  153. double centx;
  154. double centy;
  155. cv::RotatedRect minrect_ellips;
  156. vector<cv::Point> contours;
  157. } t_leaf;
  158. void img_segment(cv::Mat&img);
  159. double fit_error_ellipse(cv::RotatedRect&ellipse_rect,vector<cv::Point>& pts);
  160. void get_ellipse_param(cv::RotatedRect&ellipse_rect, //input
  161. float&A, float&B, float&C, float&F); //output
  162. void clear_imginfo();
  163. };
  164. };