optimal_angle.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. //#include "StdAfx.h"
  2. #include <opencv2\imgproc\imgproc.hpp>
  3. #include "optimal_angle.h"
  4. #include "utils.h"
  5. #include <string.h>
  6. namespace graft_cv{
  7. COptimalAngle::COptimalAngle(ConfigParam&cp, CGcvLogger* pLog/*=0*/)
  8. :m_cparam(cp),
  9. m_pLogger(pLog),
  10. m_pImginfo(0),
  11. m_pImginfoBase(0),
  12. m_height(0),
  13. m_width(0),
  14. m_patch_id(""),
  15. m_imgIndex(0),
  16. m_imgId(""),
  17. m_ppImgSaver(0)
  18. {
  19. }
  20. COptimalAngle::~COptimalAngle(void)
  21. {
  22. this->clear_imginfo();
  23. }
  24. void COptimalAngle::clear_imginfo(){
  25. if (m_pImginfo){
  26. imginfo_release(&m_pImginfo);
  27. m_pImginfo=0;
  28. }
  29. if (m_pImginfoBase){
  30. imginfo_release(&m_pImginfoBase);
  31. m_pImginfoBase=0;
  32. }
  33. }
  34. int COptimalAngle::reset()
  35. {
  36. if(m_pLogger){
  37. m_pLogger->DEBUG("optimal angle reset begin.");
  38. }
  39. m_fork_ys.clear();
  40. m_end_ys.clear();
  41. m_widths.clear();
  42. m_an2width.clear();
  43. m_an2widthBase.clear();
  44. m_width=0;
  45. m_height=0;
  46. m_patch_id="";
  47. m_imgIndex=0;
  48. if(m_pLogger){
  49. m_pLogger->DEBUG("optimal angle reset finished.");
  50. }
  51. return 0;
  52. }
  53. int COptimalAngle::append(
  54. ImgInfo* imginfo,
  55. PositionInfo& posinfo
  56. )
  57. {
  58. //clear opencv windows
  59. if(m_cparam.image_show){destroyAllWindows();}
  60. clock_t t;
  61. clock_t t0 = clock();
  62. if(m_imgIndex==0){
  63. //new patch
  64. m_patch_id = getImgId(img_type::oa);
  65. }
  66. m_imgId = getImgIdOa(m_patch_id,m_imgIndex);
  67. m_imgIndex+=1;
  68. if(m_pLogger){
  69. m_pLogger->INFO(m_imgId + " append image begin.");
  70. }
  71. if(!isvalid(imginfo)){
  72. throw_msg(m_imgId+" invalid input image");
  73. }
  74. if(m_width==0 || m_height==0){
  75. m_width = imginfo->width;
  76. m_height = imginfo->height;
  77. }
  78. int angle = imginfo->angle;
  79. if(m_pLogger){
  80. stringstream buff;
  81. buff<<m_imgId<<" append image, angle="<<angle
  82. <<"\twidth="<<imginfo->width
  83. <<"\theight="<<imginfo->height;
  84. m_pLogger->INFO(buff.str());
  85. }
  86. Mat img = imginfo2mat(imginfo);
  87. //imshow_wait("src", img);
  88. if(m_cparam.self_camera){
  89. image_set_bottom(img,20,8);
  90. if(m_pLogger){
  91. m_pLogger->DEBUG(m_imgId+" oa_append image set bottom with pixel value 20.");
  92. }
  93. }
  94. if(m_cparam.oa_y_flip){
  95. flip(img,img,0);
  96. if(m_pLogger){
  97. m_pLogger->DEBUG(m_imgId+" oa_append image y fliped.");
  98. }
  99. }
  100. if(m_ppImgSaver && *m_ppImgSaver){
  101. (*m_ppImgSaver)->saveImage(img, m_imgId);
  102. if(m_pLogger){
  103. m_pLogger->DEBUG(m_imgId+" oa_append after image save.");
  104. }
  105. }
  106. if(m_cparam.image_show){
  107. Mat tmp_b = img.clone();
  108. cv::line(tmp_b,Point(0,m_cparam.oa_clip_y_min), Point(tmp_b.cols,m_cparam.oa_clip_y_min),Scalar(200),3);
  109. cv::line(tmp_b,Point(0,m_cparam.oa_clip_y_max), Point(tmp_b.cols,m_cparam.oa_clip_y_max),Scalar(200),3);
  110. imshow_wait("oa_sub_image_range", tmp_b);
  111. }
  112. Rect rect_rs(Point(0,0),Point(img.cols,m_cparam.oa_clip_y_min));
  113. //Rect rect_base(Point(0,m_cparam.oa_clip_y_max),Point(img.cols,img.rows));
  114. Mat rs_img = img(rect_rs);
  115. //Mat bs_img = img(rect_base);
  116. // 1 rootstock image processing
  117. // 1.1 calculate the image, get binary image and get object width
  118. int min_idx, max_idx;
  119. vector<int> hist_col;
  120. int width = imgproc(rs_img, hist_col, min_idx, max_idx);
  121. m_an2width.insert(make_pair<int,int>(angle, width));
  122. posinfo.rs_oa_width=width;
  123. if(m_cparam.image_show){
  124. Mat hist_col_img;
  125. hist2image(hist_col,hist_col_img,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
  126. imshow_wait("oa_hist_col", hist_col_img);
  127. }
  128. if(m_pLogger){
  129. stringstream buff;
  130. buff<<m_imgId<<" append image, rootstock width="<<width;
  131. m_pLogger->INFO(buff.str());
  132. }
  133. if(!m_cparam.image_show){
  134. t = clock();
  135. if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
  136. throw_msg(m_imgId+" time out");
  137. }
  138. }
  139. //1.2 茎粗计算
  140. //1.2.1 茎x方向位置:x方向位置范围
  141. int r0,r1;
  142. r0=r1=-1;
  143. std::vector<int>hist_row_leaf;
  144. mat_histogram(m_binImg,hist_row_leaf,1);
  145. for(size_t i=0;i<hist_row_leaf.size();++i){
  146. if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value && r0<0){r0=i;}
  147. if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value){r1=i;}
  148. }
  149. if(r0<0 || r1<0){throw_msg(string(m_imgId+" invalid image, with no object in binary image"));}
  150. vector<int> hist_col_yfork;
  151. mat_histogram_yfork(m_binImg,hist_col_yfork,r0,r1);
  152. if(m_cparam.image_show){
  153. Mat tmp;
  154. hist2image(hist_col_yfork,tmp,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
  155. imshow_wait("oa_hist_col_yfork", tmp);
  156. }
  157. int x0,x1,stem_x0, stem_x1;
  158. int centx = (int)((min_idx+ max_idx)/2.0);
  159. get_stem_x_range_oa(
  160. hist_col_yfork,
  161. m_cparam.oa_col_th_ratio,
  162. m_cparam.oa_stem_x_padding,
  163. centx,
  164. width,
  165. x0,
  166. x1,
  167. stem_x0,
  168. stem_x1);
  169. if(m_pLogger){
  170. stringstream buff;
  171. buff<<m_imgId<<" append image, x0="<<x0
  172. <<"\tx1="<<x1
  173. <<"\tstem_x0="<<stem_x0
  174. <<"\tstem_x1="<<stem_x1;
  175. m_pLogger->INFO(buff.str());
  176. }
  177. if(m_cparam.image_show){
  178. Mat tmp_b = m_binImg.clone();
  179. cv::line(tmp_b,Point(x0,0), Point(x0,tmp_b.rows),Scalar(200),3);
  180. cv::line(tmp_b,Point(x1,0), Point(x1,tmp_b.rows),Scalar(200),3);
  181. imshow_wait("oa_x_range", tmp_b);
  182. }
  183. if(!m_cparam.image_show){
  184. t = clock();
  185. if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
  186. throw_msg(m_imgId+" time out");
  187. }
  188. }
  189. vector<int> hist_row;
  190. mat_histogram(m_binImg,hist_row,1,-1,-1,x0,x1+1);
  191. if(m_cparam.image_show){
  192. Mat hist_row_img;
  193. hist2image(hist_row,hist_row_img, 0,m_cparam.image_col_grid,m_cparam.image_row_grid);
  194. imshow_wait("oa_hist_row", hist_row_img);
  195. }
  196. // 1.2.2 茎分叉点检测,y方向检测
  197. int stem_fork_y=-1,stem_end_y=-1, stem_dia=-1;
  198. get_stem_y_fork(
  199. hist_row,
  200. m_cparam.oa_row_th_ratio,
  201. m_cparam.oa_stem_dia_min,
  202. m_cparam.oa_stem_fork_y_min,
  203. m_cparam.oa_stem_dia_mp,
  204. stem_fork_y,
  205. stem_end_y,
  206. stem_dia);
  207. if(m_pLogger){
  208. stringstream buff;
  209. buff<<m_imgId<<" append image, stem_fork_y="<<stem_fork_y
  210. <<"\tstem_end_y="<<stem_end_y
  211. <<"\tstem_dia="<<stem_dia;
  212. m_pLogger->INFO(buff.str());
  213. }
  214. if(m_cparam.image_show){
  215. Mat tmp_bin = m_binImg.clone();
  216. cv::line(tmp_bin,Point(min_idx,0), Point(min_idx,tmp_bin.rows),Scalar(200),3);
  217. cv::line(tmp_bin,Point(max_idx,0), Point(max_idx,tmp_bin.rows),Scalar(200),3);
  218. cv::line(tmp_bin,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
  219. cv::line(tmp_bin,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
  220. imshow_wait("oa_result", tmp_bin);
  221. }
  222. m_fork_ys.push_back(stem_fork_y);
  223. m_end_ys.push_back(stem_end_y);
  224. m_widths.push_back(width);
  225. //2 基质图像处理
  226. /*int min_idx_base, max_idx_base;
  227. vector<int> hist_col_base;
  228. int width_base = imgprocBase(bs_img, hist_col_base, min_idx_base, max_idx_base);
  229. m_an2widthBase.insert(make_pair<int,int>(angle, width_base));
  230. posinfo.rs_oa_width_base = width_base;
  231. if(m_pLogger){
  232. stringstream buff;
  233. buff<<m_imgId<<" append image, base width="<<width_base;
  234. m_pLogger->INFO(buff.str());
  235. }*/
  236. // 3 返回结果
  237. double fork_y = (double)stem_fork_y;
  238. double end_y = (double)stem_end_y;
  239. fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
  240. end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
  241. posinfo.rs_oa_stem_y_fork=fork_y;
  242. posinfo.rs_oa_clamp_y_end=end_y;
  243. if(m_pLogger){
  244. stringstream buff;
  245. buff<<m_imgId<<" append image, stem_fork_y(mm)="<<fork_y
  246. <<"\tstem_end_y(mm)="<<end_y;
  247. m_pLogger->INFO(buff.str());
  248. }
  249. //4 返回图像
  250. if(m_cparam.image_return){
  251. this->clear_imginfo();
  252. cv::line(m_binImg,Point(min_idx,0), Point(min_idx,rs_img.rows),Scalar(200),3);
  253. cv::line(m_binImg,Point(max_idx,0), Point(max_idx,rs_img.rows),Scalar(200),3);
  254. cv::line(m_binImg,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
  255. cv::line(m_binImg,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
  256. clear_imginfo();
  257. m_pImginfo = mat2imginfo(m_binImg);
  258. //m_pImginfoBase = mat2imginfo(m_binImgBase);
  259. posinfo.pp_images[0]=m_pImginfo;
  260. //posinfo.pp_images[1]=m_pImginfoBase;
  261. if(m_ppImgSaver && *m_ppImgSaver){
  262. (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  263. }
  264. }
  265. if(m_pLogger){
  266. m_pLogger->INFO(m_imgId + " append image finished.");
  267. }
  268. return 0;
  269. }
  270. double COptimalAngle::infer(PositionInfo& posinfo){
  271. if(m_pLogger){
  272. m_pLogger->INFO(m_patch_id + " optimal angle infer begin.");
  273. }
  274. double oa = this->angle_fit(this->m_an2width);
  275. /*double oa_base = 0.0;
  276. try{
  277. oa_base = this->angle_fit_base(this->m_an2widthBase);
  278. }
  279. catch(...){
  280. if(m_pLogger){
  281. m_pLogger->WARNING("angle_fit_base() error");
  282. }
  283. }*/
  284. posinfo.rs_oa = oa;
  285. //posinfo.rs_oa_base=oa_base;
  286. if(m_fork_ys.size()==0 ||m_end_ys.size()==0){
  287. throw_msg(m_patch_id+ " empty fork_ys or end_ys, NEED append image");
  288. }
  289. vector<size_t>des_idx = sort_indexes_e(m_widths,false);
  290. int e_idx = (int)((float)m_end_ys.size() * 0.5);
  291. sort(m_end_ys.begin(), m_end_ys.end());
  292. double fork_y = ((m_fork_ys[des_idx[0]] + m_fork_ys[des_idx[1]])/2.0);
  293. double end_y = m_end_ys[e_idx];
  294. if(m_pLogger){
  295. stringstream buff;
  296. buff<<m_patch_id<<" angle fit result, stem_fork_y(pixel)="<<(int)(fork_y)
  297. <<"\tstem_end_y(pixel)="<<(int)(end_y);
  298. m_pLogger->INFO(buff.str());
  299. }
  300. fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
  301. end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
  302. posinfo.rs_oa_stem_y_fork=fork_y;
  303. posinfo.rs_oa_clamp_y_end=end_y;
  304. if(m_pLogger){
  305. stringstream buff;
  306. buff<<m_patch_id<<" angle fit result, stem_fork_y(mm)="<<fork_y
  307. <<"\tstem_end_y(mm)="<<end_y;
  308. m_pLogger->INFO(buff.str());
  309. m_pLogger->INFO(m_patch_id + " optimal angle infer finished.");
  310. }
  311. return oa;
  312. }
  313. int COptimalAngle::imgproc(
  314. Mat& img,
  315. vector<int>& hist,
  316. int& min_idx,
  317. int& max_idx
  318. )
  319. {
  320. //int morph_size = 1;
  321. //int min_hist_value = 10;
  322. hist.clear();
  323. Mat b_img;
  324. double th = threshold(
  325. img,
  326. b_img,
  327. 255,
  328. 255,
  329. THRESH_OTSU);//+THRESH_BINARY_INV
  330. Mat kernel = getStructuringElement(
  331. MORPH_RECT,
  332. Size( 2*m_cparam.oa_morph_radius+1, 2*m_cparam.oa_morph_radius+1),
  333. Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
  334. );
  335. morphologyEx(
  336. b_img,
  337. m_binImg,
  338. MORPH_CLOSE,
  339. kernel,
  340. Point(-1,-1),
  341. m_cparam.oa_morph_iteration);
  342. mat_histogram(m_binImg, hist, 0);
  343. /*int min_idx, max_idx;*/
  344. min_idx = hist.size();
  345. max_idx = 0;
  346. vector<int>::const_iterator it0 = hist.begin();
  347. for(vector<int>::const_iterator it = hist.begin();it!=hist.end();++it){
  348. if(*it>=m_cparam.oa_min_hist_value){
  349. int idx = it - it0;
  350. if(idx<min_idx){min_idx = idx;}
  351. if(idx > max_idx) {max_idx = idx;}
  352. }
  353. }
  354. if(max_idx<=min_idx){
  355. throw_msg(m_imgId+" invalid binary image, not exist valid objects");
  356. }
  357. if(m_cparam.image_show){
  358. Mat tmp = m_binImg.clone();
  359. cv::line(tmp,Point(min_idx,0), Point(min_idx,tmp.rows),Scalar(156),3);
  360. cv::line(tmp,Point(max_idx,0), Point(max_idx,tmp.rows),Scalar(156),3);
  361. imshow_wait("oa_bin", tmp);
  362. }
  363. int width = max_idx-min_idx+1;
  364. return width;
  365. }
  366. int COptimalAngle::imgprocBase(
  367. Mat& img,
  368. vector<int>& hist,
  369. int& min_idx,
  370. int& max_idx
  371. )
  372. {
  373. //int morph_size = 1;
  374. //int min_hist_value = 10;
  375. hist.clear();
  376. Mat b_img;
  377. double th = threshold(
  378. img,
  379. b_img,
  380. 255,
  381. 255,
  382. THRESH_OTSU+THRESH_BINARY_INV);
  383. Mat kernel = getStructuringElement(
  384. MORPH_RECT,
  385. Size( 2*m_cparam.oa_morph_radius_base+1, 2*m_cparam.oa_morph_radius_base+1),
  386. Point( m_cparam.oa_morph_radius_base, m_cparam.oa_morph_radius_base)
  387. );
  388. morphologyEx(
  389. b_img,
  390. m_binImgBase,
  391. MORPH_OPEN,
  392. kernel,
  393. Point(-1,-1),
  394. m_cparam.oa_morph_iteration_base);
  395. if(m_cparam.image_show){
  396. destroyAllWindows();
  397. imshow_wait("oa_bin_base", m_binImgBase);
  398. }
  399. mat_histogram(m_binImgBase, hist, 0);
  400. /*int min_idx, max_idx;*/
  401. min_idx = hist.size();
  402. max_idx = 0;
  403. vector<int>::const_iterator it0 = hist.begin();
  404. for(vector<int>::const_iterator it = hist.begin()+5;it!=hist.end();++it){
  405. if(*it>=m_cparam.oa_min_hist_value_base){
  406. int idx = it - it0;
  407. if(idx<min_idx){min_idx = idx;}
  408. if(idx > max_idx) {max_idx = idx;}
  409. }
  410. }
  411. if(max_idx<=min_idx){
  412. throw_msg(m_imgId+" invalid binary image, not exist valid objects");
  413. }
  414. int width = max_idx-min_idx+1;
  415. return width;
  416. }
  417. //angle_fit()方法
  418. //append()加载的图片,满足下述条件:0-180度采样,其中0和180度都有数值
  419. double COptimalAngle::angle_fit(map<int,int>& an2width,bool is_base/*=false*/)
  420. {
  421. if(an2width.size()<3){
  422. throw_msg(m_patch_id+" not enough valid images, NEED append image");
  423. }
  424. double oa = 0.0;
  425. std::vector<double> x;//angle
  426. std::vector<double> y;//width
  427. map<int,int>::iterator it = an2width.begin();
  428. for(it=an2width.begin(); it!=an2width.end(); ++it)
  429. {
  430. x.push_back((double)(it->first));
  431. y.push_back((double)(it->second));
  432. }
  433. //mean value of first and latest y
  434. double y_mu = 0.5*(y[0]+ y[y.size()-1]);
  435. y[0] = y[y.size()-1] = y_mu;
  436. size_t ap_times = x.size()-1;
  437. for (size_t i=0; i<ap_times;++i)
  438. {
  439. x.push_back(x[i+1]+180.0);
  440. y.push_back(y[i+1]);
  441. }
  442. vector<double>::iterator smallest = min_element(begin(y), end(y));
  443. size_t min_idx, max_idx;
  444. min_idx = distance(y.begin(), smallest);
  445. for(size_t i = min_idx+1;i<y.size();++i){
  446. if(fabs(y[i]-*smallest)<0.1){
  447. max_idx = i;
  448. break;
  449. }
  450. }
  451. // slice x and y the convex segment
  452. vector<double>xx;
  453. vector<double>yy;
  454. for(size_t i=min_idx;i<=max_idx;++i){
  455. xx.push_back(x[i]);
  456. yy.push_back(y[i]);
  457. }
  458. // fit quadratic function with opencv
  459. Mat A = cv::Mat::zeros(cv::Size(3,xx.size()), CV_64FC1);
  460. for(int i=0; i<xx.size();++i){
  461. A.at<double>(i,0) = 1;
  462. A.at<double>(i,1) = xx[i];
  463. A.at<double>(i,2) = xx[i] * xx[i];
  464. }
  465. Mat b = cv::Mat::zeros(cv::Size(1,yy.size()), CV_64FC1);
  466. for(int i = 0; i<yy.size(); ++i){
  467. b.at<double>(i,0) = yy[i];
  468. }
  469. Mat c, d;
  470. c = A.t() * A;
  471. d = A.t() * b;
  472. Mat result = cv::Mat::zeros(cv::Size(1,3),CV_64FC1);
  473. cv::solve(c,d,result);
  474. double a0 = result.at<double>(0, 0);
  475. double a1 = result.at<double>(1, 0);
  476. double a2 = result.at<double>(2, 0);
  477. if(fabs(a2)<1.0e-6){
  478. throw_msg(m_patch_id+" a2 = 0 in solver");
  479. }
  480. oa = -a1 / a2 / 2;
  481. if(oa >180.0){oa -= 180.0;}
  482. return oa;
  483. }
  484. double COptimalAngle::angle_fit_base(map<int, int>&){
  485. return 0.0;
  486. }
  487. ///////////////////////////////////////////////////////////////////////////////
  488. COptimalAnglePart::COptimalAnglePart(ConfigParam&cp, CGcvLogger* pLog/*=0*/)
  489. :COptimalAngle(cp,pLog)
  490. {
  491. }
  492. COptimalAnglePart::~COptimalAnglePart()
  493. {
  494. COptimalAngle::clear_imginfo();
  495. }
  496. // append()加载的图片,满足不是0-180度采样,但保证部分采样中一定存在最大值,
  497. // 直接利用这些数值进行二次函数的拟合
  498. double COptimalAnglePart::angle_fit(map<int,int>& an2width,bool is_base/*=false*/)
  499. {
  500. double oa = 0.0;
  501. std::vector<double> xx;//angle
  502. std::vector<double> yy;//width
  503. map<int,int>::iterator it = an2width.begin();
  504. for(it=an2width.begin(); it!=an2width.end(); ++it)
  505. {
  506. xx.push_back((double)(it->first));
  507. yy.push_back((double)(it->second));
  508. }
  509. //sort
  510. for(size_t i =0;i<xx.size()-1;++i){
  511. for(size_t j = i+1; j<xx.size();++j){
  512. if(xx[j]<xx[i]){
  513. double tmp = xx[i];
  514. xx[i]=xx[j];
  515. xx[j]=tmp;
  516. tmp = yy[i];
  517. yy[i]=yy[j];
  518. yy[j]=tmp;
  519. }
  520. }
  521. }
  522. if(m_pLogger){
  523. stringstream buff;
  524. buff<<m_patch_id;
  525. if(is_base){
  526. buff<<" rootstock_base ";
  527. }
  528. else{
  529. buff<<" rootstock_leaf ";
  530. }
  531. buff<<"opt-angle, (xi,yi): ";
  532. for(size_t i=0;i<xx.size();++i){
  533. buff<<"("<<xx[i]<<","<<yy[i]<<"), ";
  534. }
  535. m_pLogger->INFO(buff.str());
  536. }
  537. if(an2width.size()<3){
  538. throw_msg(m_patch_id+" not enough valid images, NEED append image");
  539. }
  540. // angle field check
  541. if((xx.back()-xx.front())<90){
  542. if(m_pLogger){
  543. stringstream buff;
  544. buff<<m_patch_id<<" x_end="<<xx.back()<<" x_front="<<xx.front();
  545. buff<<" not enough angle field, small than 90 degree.";
  546. m_pLogger->ERRORINFO(buff.str());
  547. }
  548. throw_msg(m_patch_id+" not enough angle field, small than 90 degree");
  549. }
  550. oa = opt_angle_part_impl(xx,yy,is_base);
  551. if(m_pLogger){
  552. stringstream buff;
  553. buff<<m_patch_id;
  554. if(is_base){
  555. buff<<" rootstock_base ";
  556. }
  557. else{
  558. buff<<" rootstock_leaf ";
  559. }
  560. buff<<" opt-angle: "<<oa;
  561. m_pLogger->INFO(buff.str());
  562. }
  563. return oa;
  564. }
  565. double COptimalAnglePart::angle_fit_base(map<int, int>& an2width){
  566. double oa = 0.0;
  567. //find the maximun angle
  568. oa = angle_fit(an2width,true);
  569. //get the minimum angle
  570. if(oa>45.0){oa -= 45.0;}
  571. else{oa +=45.0;}
  572. return oa;
  573. }
  574. void COptimalAnglePart::zero_cross_detect(
  575. vector<int>&d_sign, //input
  576. bool& is_local_max, // whether exists local max
  577. int& local_limit_idx // local limit index of original vector
  578. )
  579. {
  580. int min_zc_idx = -1;
  581. int max_zc_idx = -1;
  582. bool b_local_min =false;
  583. bool b_local_max =false;
  584. bool is_all_down=true;
  585. bool is_all_up = true;
  586. for (size_t i=0; i<d_sign.size();++i){
  587. if (d_sign[i] >0){is_all_down=false;}
  588. if (d_sign[i] <0){is_all_up=false;}
  589. if (i == d_sign.size()-1){continue;}
  590. if (d_sign[i] >=0 && d_sign[i+1] <=0){
  591. //is_local_max=true;
  592. max_zc_idx = i+1;
  593. b_local_max=true;
  594. }
  595. if (d_sign[i] <=0 && d_sign[i+1] >=0){
  596. //is_local_max=false;
  597. min_zc_idx=i+1;
  598. b_local_min=true;
  599. }
  600. }
  601. if(b_local_min){
  602. is_local_max=false;
  603. local_limit_idx = min_zc_idx;
  604. return;
  605. }
  606. if(b_local_max){
  607. is_local_max=true;
  608. local_limit_idx = max_zc_idx;
  609. return;
  610. }
  611. if (is_all_down){
  612. is_local_max=true;
  613. local_limit_idx=0;
  614. return;
  615. }
  616. if( is_all_up){
  617. is_local_max=true;
  618. local_limit_idx=d_sign.size();
  619. return;
  620. }
  621. is_local_max=true;
  622. local_limit_idx=-1;
  623. return;
  624. }
  625. double COptimalAnglePart::limit_min_angle(
  626. vector<double>&x, //sorted, ascending
  627. vector<double>&y,
  628. size_t min_idx
  629. )
  630. {
  631. size_t i = min_idx;
  632. if(fabs(x[i+1]-x[i])<1.0e-6 || fabs(x[i]-x[i-1])<1.0e-6){
  633. throw_msg(m_patch_id+" limit_min_angle, diff_x < 1.0e-6");
  634. }
  635. double k_r = (y[i+1]-y[i])/(x[i+1]-x[i]);
  636. double k_l = (y[i]-y[i-1])/(x[i]-x[i-1]);
  637. double oa = 0.0;
  638. double b_r = 0.0;
  639. double b_l = 0.0;
  640. double cross_x = 0;
  641. if (fabs(k_r) > fabs(k_l)){
  642. // right line
  643. b_r = y[i]-k_r*x[i];
  644. b_l = y[i-1] + k_r*x[i-1];
  645. cross_x = line_cross(k_r,b_r,-k_r,b_l);
  646. oa = cross_x;
  647. }
  648. else{
  649. //# left line
  650. b_l = y[i]-k_l*x[i];
  651. b_r = y[i+1] + k_l*x[i+1];
  652. cross_x = line_cross(k_l,b_l,-k_l,b_r);
  653. oa = cross_x;
  654. }
  655. return oa;
  656. }
  657. double COptimalAnglePart::line_cross(double k1,double b1,double k2,double b2)
  658. {
  659. double x = (b2-b1) / (k1-k2);
  660. return x;
  661. }
  662. double COptimalAnglePart::opt_angle_part_impl(
  663. vector<double>&x, //sorted, ascending
  664. vector<double>&y,
  665. bool is_base/*=false*/)
  666. {
  667. // sign of difference of y
  668. vector<int> dy_sign;
  669. for(size_t i=1; i<y.size();++i){
  670. double dd = y[i] - y[i-1];
  671. if(dd>0){
  672. dy_sign.push_back(1);
  673. }
  674. else{
  675. if(dd<0){dy_sign.push_back(-1);}
  676. else{dy_sign.push_back(0);}
  677. }
  678. }
  679. //local limit search
  680. bool is_local_max = false;
  681. int local_limit_idx = -1;
  682. zero_cross_detect(dy_sign,is_local_max,local_limit_idx);
  683. if(local_limit_idx<0){
  684. throw_msg(m_patch_id+" not found local limit");
  685. }
  686. // optimal angle calculation
  687. vector<double> xx;
  688. vector<double> yy;
  689. double oa=0.0;
  690. if(is_local_max){
  691. //存在局部最大
  692. if( local_limit_idx == 0 || local_limit_idx==y.size()-1){
  693. //#单调
  694. oa = x[local_limit_idx];
  695. /*if (local_limit_idx == 0){
  696. for(size_t i=0;i<3;++i){
  697. xx.push_back(x[i]);
  698. yy.push_back(y[i]);
  699. }
  700. }
  701. else {
  702. for(size_t i=y.size()-3;i<y.size();++i){
  703. xx.push_back(x[i]);
  704. yy.push_back(y[i]);
  705. }
  706. }
  707. oa = qua_fitting(xx,yy); */
  708. if(m_pLogger){
  709. m_pLogger->INFO(m_patch_id+ " angle fit -- signle --- max_angle");
  710. }
  711. }
  712. else{
  713. // 极大值
  714. for(size_t i=local_limit_idx-1;i<local_limit_idx+2;++i){
  715. xx.push_back(x[i]);
  716. yy.push_back(-y[i]);
  717. }
  718. oa = limit_min_angle(xx,yy,1);
  719. if(m_pLogger){
  720. m_pLogger->INFO(m_patch_id+" angle fit -- local max --- 3p insert");
  721. }
  722. }
  723. }
  724. else{
  725. // 极小值
  726. oa = limit_min_angle(x,y,local_limit_idx);
  727. if(is_base){
  728. //for base, offset 45 degree
  729. if(oa >45){oa = oa-45.0;}
  730. else{oa = oa+45.0;}
  731. }
  732. else{
  733. // for rootstock, offset 90 degree
  734. if(oa >90){oa = oa-90.0;}
  735. else{oa = oa+90.0;}
  736. }
  737. if(m_pLogger){
  738. m_pLogger->INFO(m_patch_id+" angle fit -- local min --- 3p insert");
  739. }
  740. }
  741. return oa;
  742. }
  743. };