optimal_angle.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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)
  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. //
  54. //int COptimalAngle::append(
  55. // ImgInfo* imginfo,
  56. // PositionInfo& posinfo
  57. // )
  58. //{
  59. // //clear opencv windows
  60. // if(m_cparam.image_show){destroyAllWindows();}
  61. //
  62. // clock_t t;
  63. // clock_t t0 = clock();
  64. // if(m_imgIndex==0){
  65. // //new patch
  66. // m_patch_id = getImgId(img_type::oa);
  67. // }
  68. // m_imgId = getImgIdOa(m_patch_id,m_imgIndex);
  69. // m_imgIndex+=1;
  70. //
  71. // if(m_pLogger){
  72. // m_pLogger->INFO(m_imgId + " append image begin.");
  73. // }
  74. // if(!isvalid(imginfo)){
  75. // throw_msg(m_imgId+" invalid input image");
  76. // }
  77. // if(m_width==0 || m_height==0){
  78. // m_width = imginfo->width;
  79. // m_height = imginfo->height;
  80. // }
  81. // int angle = imginfo->angle;
  82. // if(m_pLogger){
  83. // stringstream buff;
  84. // buff<<m_imgId<<" append image, angle="<<angle
  85. // <<"\twidth="<<imginfo->width
  86. // <<"\theight="<<imginfo->height;
  87. // m_pLogger->INFO(buff.str());
  88. // }
  89. // Mat img = imginfo2mat(imginfo);
  90. //
  91. // //imshow_wait("src", img);
  92. // if(m_cparam.self_camera){
  93. // image_set_bottom(img,20,8);
  94. // if(m_pLogger){
  95. // m_pLogger->DEBUG(m_imgId+" oa_append image set bottom with pixel value 20.");
  96. // }
  97. // }
  98. // if(m_cparam.oa_y_flip){
  99. // flip(img,img,0);
  100. // if(m_pLogger){
  101. // m_pLogger->DEBUG(m_imgId+" oa_append image y fliped.");
  102. // }
  103. // }
  104. // if(m_ppImgSaver && *m_ppImgSaver){
  105. // (*m_ppImgSaver)->saveImage(img, m_imgId);
  106. // if(m_pLogger){
  107. // m_pLogger->DEBUG(m_imgId+" oa_append after image save.");
  108. // }
  109. // }
  110. // if(m_cparam.image_show){
  111. // Mat tmp_b = img.clone();
  112. // 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);
  113. // 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);
  114. // imshow_wait("oa_sub_image_range", tmp_b);
  115. // }
  116. //
  117. // Rect rect_rs(Point(0,0),Point(img.cols,m_cparam.oa_clip_y_min));
  118. // //Rect rect_base(Point(0,m_cparam.oa_clip_y_max),Point(img.cols,img.rows));
  119. //
  120. // Mat rs_img = img(rect_rs);
  121. // //Mat bs_img = img(rect_base);
  122. //
  123. //
  124. // // 1 rootstock image processing
  125. // // 1.1 calculate the image, get binary image and get object width
  126. // int min_idx, max_idx;
  127. // vector<int> hist_col;
  128. // int width = imgproc(rs_img, hist_col, min_idx, max_idx);
  129. // m_an2width.insert(make_pair<int,int>(angle, width));
  130. // posinfo.rs_oa_width=width;
  131. //
  132. // if(m_cparam.image_show){
  133. // Mat hist_col_img;
  134. // hist2image(hist_col,hist_col_img,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
  135. // imshow_wait("oa_hist_col", hist_col_img);
  136. // }
  137. //
  138. // if(m_pLogger){
  139. // stringstream buff;
  140. // buff<<m_imgId<<" append image, rootstock width="<<width;
  141. // m_pLogger->INFO(buff.str());
  142. // }
  143. //
  144. // if(!m_cparam.image_show){
  145. // t = clock();
  146. // if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
  147. // throw_msg(m_imgId+" time out");
  148. // }
  149. // }
  150. //
  151. // //1.2 茎粗计算
  152. // //1.2.1 茎x方向位置:x方向位置范围
  153. // int r0,r1;
  154. // r0=r1=-1;
  155. // std::vector<int>hist_row_leaf;
  156. // mat_histogram(m_binImg,hist_row_leaf,1);
  157. // for(size_t i=0;i<hist_row_leaf.size();++i){
  158. // if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value && r0<0){r0=i;}
  159. // if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value){r1=i;}
  160. // }
  161. // if(r0<0 || r1<0){throw_msg(string(m_imgId+" invalid image, with no object in binary image"));}
  162. //
  163. // vector<int> hist_col_yfork;
  164. // mat_histogram_yfork(m_binImg,hist_col_yfork,r0,r1);
  165. // if(m_cparam.image_show){
  166. // Mat tmp;
  167. // hist2image(hist_col_yfork,tmp,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
  168. // imshow_wait("oa_hist_col_yfork", tmp);
  169. // }
  170. //
  171. // int x0,x1,stem_x0, stem_x1;
  172. // int centx = (int)((min_idx+ max_idx)/2.0);
  173. // get_stem_x_range_oa(
  174. // hist_col_yfork,
  175. // m_cparam.oa_col_th_ratio,
  176. // m_cparam.oa_stem_x_padding,
  177. // centx,
  178. // width,
  179. // x0,
  180. // x1,
  181. // stem_x0,
  182. // stem_x1);
  183. //
  184. // if(m_pLogger){
  185. // stringstream buff;
  186. // buff<<m_imgId<<" append image, x0="<<x0
  187. // <<"\tx1="<<x1
  188. // <<"\tstem_x0="<<stem_x0
  189. // <<"\tstem_x1="<<stem_x1;
  190. // m_pLogger->INFO(buff.str());
  191. // }
  192. //
  193. // if(m_cparam.image_show){
  194. // Mat tmp_b = m_binImg.clone();
  195. // cv::line(tmp_b,Point(x0,0), Point(x0,tmp_b.rows),Scalar(200),3);
  196. // cv::line(tmp_b,Point(x1,0), Point(x1,tmp_b.rows),Scalar(200),3);
  197. // imshow_wait("oa_x_range", tmp_b);
  198. // }
  199. //
  200. // if(!m_cparam.image_show){
  201. // t = clock();
  202. // if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
  203. // throw_msg(m_imgId+" time out");
  204. // }
  205. // }
  206. //
  207. // vector<int> hist_row;
  208. // mat_histogram(m_binImg,hist_row,1,-1,-1,x0,x1+1);
  209. //
  210. // if(m_cparam.image_show){
  211. // Mat hist_row_img;
  212. // hist2image(hist_row,hist_row_img, 0,m_cparam.image_col_grid,m_cparam.image_row_grid);
  213. // imshow_wait("oa_hist_row", hist_row_img);
  214. // }
  215. // // 1.2.2 茎分叉点检测,y方向检测
  216. // int stem_fork_y=-1,stem_end_y=-1, stem_dia=-1;
  217. // get_stem_y_fork(
  218. // hist_row,
  219. // m_cparam.oa_row_th_ratio,
  220. // m_cparam.oa_stem_dia_min,
  221. // m_cparam.oa_stem_fork_y_min,
  222. // m_cparam.oa_stem_dia_mp,
  223. // stem_fork_y,
  224. // stem_end_y,
  225. // stem_dia);
  226. // if(m_pLogger){
  227. // stringstream buff;
  228. // buff<<m_imgId<<" append image, stem_fork_y="<<stem_fork_y
  229. // <<"\tstem_end_y="<<stem_end_y
  230. // <<"\tstem_dia="<<stem_dia;
  231. // m_pLogger->INFO(buff.str());
  232. // }
  233. //
  234. //
  235. // if(m_cparam.image_show){
  236. // Mat tmp_bin = m_binImg.clone();
  237. // cv::line(tmp_bin,Point(min_idx,0), Point(min_idx,tmp_bin.rows),Scalar(200),3);
  238. // cv::line(tmp_bin,Point(max_idx,0), Point(max_idx,tmp_bin.rows),Scalar(200),3);
  239. // cv::line(tmp_bin,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
  240. // cv::line(tmp_bin,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
  241. // imshow_wait("oa_result", tmp_bin);
  242. // }
  243. //
  244. // m_fork_ys.push_back(stem_fork_y);
  245. // m_end_ys.push_back(stem_end_y);
  246. // m_widths.push_back(width);
  247. //
  248. //
  249. // //2 基质图像处理
  250. // //int min_idx_base, max_idx_base;
  251. // //vector<int> hist_col_base;
  252. // //int width_base = imgprocBase(bs_img, hist_col_base, min_idx_base, max_idx_base);
  253. // //m_an2widthBase.insert(make_pair<int,int>(angle, width_base));
  254. // //posinfo.rs_oa_width_base = width_base;
  255. //
  256. // //if(m_pLogger){
  257. // // stringstream buff;
  258. // // buff<<m_imgId<<" append image, base width="<<width_base;
  259. // // m_pLogger->INFO(buff.str());
  260. // //
  261. //
  262. // // 3 返回结果
  263. // double fork_y = (double)stem_fork_y;
  264. // double end_y = (double)stem_end_y;
  265. //
  266. // fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
  267. // end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
  268. // posinfo.rs_oa_stem_y_fork=fork_y;
  269. // posinfo.rs_oa_clamp_y_end=end_y;
  270. // if(m_pLogger){
  271. // stringstream buff;
  272. // buff<<m_imgId<<" append image, stem_fork_y(mm)="<<fork_y
  273. // <<"\tstem_end_y(mm)="<<end_y;
  274. // m_pLogger->INFO(buff.str());
  275. // }
  276. // //4 返回图像
  277. // if(m_cparam.image_return){
  278. // this->clear_imginfo();
  279. // cv::line(m_binImg,Point(min_idx,0), Point(min_idx,rs_img.rows),Scalar(200),3);
  280. // cv::line(m_binImg,Point(max_idx,0), Point(max_idx,rs_img.rows),Scalar(200),3);
  281. //
  282. // cv::line(m_binImg,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
  283. // cv::line(m_binImg,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
  284. //
  285. // clear_imginfo();
  286. // m_pImginfo = mat2imginfo(m_binImg);
  287. // //m_pImginfoBase = mat2imginfo(m_binImgBase);
  288. // posinfo.pp_images[0]=m_pImginfo;
  289. // //posinfo.pp_images[1]=m_pImginfoBase;
  290. //
  291. // if(m_ppImgSaver && *m_ppImgSaver){
  292. // (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  293. // }
  294. // }
  295. // if(m_pLogger){
  296. // m_pLogger->INFO(m_imgId + " append image finished.");
  297. // }
  298. // return 0;
  299. //}
  300. //
  301. //double COptimalAngle::infer(PositionInfo& posinfo){
  302. //
  303. // if(m_pLogger){
  304. // m_pLogger->INFO(m_patch_id + " optimal angle infer begin.");
  305. // }
  306. // double oa = this->angle_fit(this->m_an2width);
  307. // //double oa_base = 0.0;
  308. // //try{
  309. // // oa_base = this->angle_fit_base(this->m_an2widthBase);
  310. // //}
  311. // //catch(...){
  312. // // if(m_pLogger){
  313. // // m_pLogger->WARNING("angle_fit_base() error");
  314. // // }
  315. // //}
  316. // posinfo.rs_oa = oa;
  317. // //posinfo.rs_oa_base=oa_base;
  318. //
  319. // if(m_fork_ys.size()==0 ||m_end_ys.size()==0){
  320. // throw_msg(m_patch_id+ " empty fork_ys or end_ys, NEED append image");
  321. // }
  322. // vector<size_t>des_idx = sort_indexes_e(m_widths,false);
  323. // int e_idx = (int)((float)m_end_ys.size() * 0.5);
  324. // sort(m_end_ys.begin(), m_end_ys.end());
  325. // double fork_y = ((m_fork_ys[des_idx[0]] + m_fork_ys[des_idx[1]])/2.0);
  326. // double end_y = m_end_ys[e_idx];
  327. // if(m_pLogger){
  328. // stringstream buff;
  329. // buff<<m_patch_id<<" angle fit result, stem_fork_y(pixel)="<<(int)(fork_y)
  330. // <<"\tstem_end_y(pixel)="<<(int)(end_y);
  331. // m_pLogger->INFO(buff.str());
  332. // }
  333. //
  334. // fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
  335. // end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
  336. // posinfo.rs_oa_stem_y_fork=fork_y;
  337. // posinfo.rs_oa_clamp_y_end=end_y;
  338. // if(m_pLogger){
  339. // stringstream buff;
  340. // buff<<m_patch_id<<" angle fit result, stem_fork_y(mm)="<<fork_y
  341. // <<"\tstem_end_y(mm)="<<end_y;
  342. // m_pLogger->INFO(buff.str());
  343. //
  344. // m_pLogger->INFO(m_patch_id + " optimal angle infer finished.");
  345. // }
  346. // return oa;
  347. //}
  348. //
  349. //int COptimalAngle::imgproc(
  350. // Mat& img,
  351. // vector<int>& hist,
  352. // int& min_idx,
  353. // int& max_idx
  354. // )
  355. //{
  356. // //int morph_size = 1;
  357. // //int min_hist_value = 10;
  358. // hist.clear();
  359. //
  360. // Mat b_img;
  361. // double th = threshold(
  362. // img,
  363. // b_img,
  364. // 255,
  365. // 255,
  366. // THRESH_OTSU);//+THRESH_BINARY_INV
  367. //
  368. // Mat kernel = getStructuringElement(
  369. // MORPH_RECT,
  370. // Size( 2*m_cparam.oa_morph_radius+1, 2*m_cparam.oa_morph_radius+1),
  371. // Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
  372. // );
  373. //
  374. // morphologyEx(
  375. // b_img,
  376. // m_binImg,
  377. // MORPH_CLOSE,
  378. // kernel,
  379. // Point(-1,-1),
  380. // m_cparam.oa_morph_iteration);
  381. //
  382. //
  383. //
  384. // mat_histogram(m_binImg, hist, 0);
  385. //
  386. // /*int min_idx, max_idx;*/
  387. // min_idx = hist.size();
  388. // max_idx = 0;
  389. // vector<int>::const_iterator it0 = hist.begin();
  390. // for(vector<int>::const_iterator it = hist.begin();it!=hist.end();++it){
  391. // if(*it>=m_cparam.oa_min_hist_value){
  392. // int idx = it - it0;
  393. // if(idx<min_idx){min_idx = idx;}
  394. // if(idx > max_idx) {max_idx = idx;}
  395. // }
  396. // }
  397. // if(max_idx<=min_idx){
  398. // throw_msg(m_imgId+" invalid binary image, not exist valid objects");
  399. // }
  400. // if(m_cparam.image_show){
  401. // Mat tmp = m_binImg.clone();
  402. // cv::line(tmp,Point(min_idx,0), Point(min_idx,tmp.rows),Scalar(156),3);
  403. // cv::line(tmp,Point(max_idx,0), Point(max_idx,tmp.rows),Scalar(156),3);
  404. // imshow_wait("oa_bin", tmp);
  405. // }
  406. // int width = max_idx-min_idx+1;
  407. // return width;
  408. //}
  409. //
  410. //int COptimalAngle::imgprocBase(
  411. // Mat& img,
  412. // vector<int>& hist,
  413. // int& min_idx,
  414. // int& max_idx
  415. // )
  416. //{
  417. // //int morph_size = 1;
  418. // //int min_hist_value = 10;
  419. // hist.clear();
  420. //
  421. // Mat b_img;
  422. // double th = threshold(
  423. // img,
  424. // b_img,
  425. // 255,
  426. // 255,
  427. // THRESH_OTSU+THRESH_BINARY_INV);
  428. //
  429. // Mat kernel = getStructuringElement(
  430. // MORPH_RECT,
  431. // Size( 2*m_cparam.oa_morph_radius_base+1, 2*m_cparam.oa_morph_radius_base+1),
  432. // Point( m_cparam.oa_morph_radius_base, m_cparam.oa_morph_radius_base)
  433. // );
  434. //
  435. // morphologyEx(
  436. // b_img,
  437. // m_binImgBase,
  438. // MORPH_OPEN,
  439. // kernel,
  440. // Point(-1,-1),
  441. // m_cparam.oa_morph_iteration_base);
  442. //
  443. // if(m_cparam.image_show){
  444. // destroyAllWindows();
  445. // imshow_wait("oa_bin_base", m_binImgBase);
  446. // }
  447. //
  448. // mat_histogram(m_binImgBase, hist, 0);
  449. //
  450. // /*int min_idx, max_idx;*/
  451. // min_idx = hist.size();
  452. // max_idx = 0;
  453. // vector<int>::const_iterator it0 = hist.begin();
  454. // for(vector<int>::const_iterator it = hist.begin()+5;it!=hist.end();++it){
  455. // if(*it>=m_cparam.oa_min_hist_value_base){
  456. // int idx = it - it0;
  457. // if(idx<min_idx){min_idx = idx;}
  458. // if(idx > max_idx) {max_idx = idx;}
  459. // }
  460. // }
  461. // if(max_idx<=min_idx){
  462. // throw_msg(m_imgId+" invalid binary image, not exist valid objects");
  463. // }
  464. // int width = max_idx-min_idx+1;
  465. // return width;
  466. //}
  467. ////angle_fit()方法
  468. ////append()加载的图片,满足下述条件:0-180度采样,其中0和180度都有数值
  469. //double COptimalAngle::angle_fit(map<int,int>& an2width,bool is_base)
  470. //{
  471. // if(an2width.size()<3){
  472. // throw_msg(m_patch_id+" not enough valid images, NEED append image");
  473. // }
  474. // double oa = 0.0;
  475. // std::vector<double> x;//angle
  476. // std::vector<double> y;//width
  477. //
  478. // map<int,int>::iterator it = an2width.begin();
  479. // for(it=an2width.begin(); it!=an2width.end(); ++it)
  480. // {
  481. // x.push_back((double)(it->first));
  482. // y.push_back((double)(it->second));
  483. // }
  484. // //mean value of first and latest y
  485. // double y_mu = 0.5*(y[0]+ y[y.size()-1]);
  486. // y[0] = y[y.size()-1] = y_mu;
  487. // size_t ap_times = x.size()-1;
  488. // for (size_t i=0; i<ap_times;++i)
  489. // {
  490. // x.push_back(x[i+1]+180.0);
  491. // y.push_back(y[i+1]);
  492. // }
  493. // vector<double>::iterator smallest = min_element(begin(y), end(y));
  494. // size_t min_idx, max_idx;
  495. // min_idx = distance(y.begin(), smallest);
  496. //
  497. // for(size_t i = min_idx+1;i<y.size();++i){
  498. // if(fabs(y[i]-*smallest)<0.1){
  499. // max_idx = i;
  500. // break;
  501. // }
  502. // }
  503. //
  504. // // slice x and y the convex segment
  505. // vector<double>xx;
  506. // vector<double>yy;
  507. // for(size_t i=min_idx;i<=max_idx;++i){
  508. // xx.push_back(x[i]);
  509. // yy.push_back(y[i]);
  510. // }
  511. //
  512. // // fit quadratic function with opencv
  513. // Mat A = cv::Mat::zeros(cv::Size(3,xx.size()), CV_64FC1);
  514. // for(int i=0; i<xx.size();++i){
  515. // A.at<double>(i,0) = 1;
  516. // A.at<double>(i,1) = xx[i];
  517. // A.at<double>(i,2) = xx[i] * xx[i];
  518. // }
  519. //
  520. // Mat b = cv::Mat::zeros(cv::Size(1,yy.size()), CV_64FC1);
  521. // for(int i = 0; i<yy.size(); ++i){
  522. // b.at<double>(i,0) = yy[i];
  523. // }
  524. // Mat c, d;
  525. // c = A.t() * A;
  526. // d = A.t() * b;
  527. // Mat result = cv::Mat::zeros(cv::Size(1,3),CV_64FC1);
  528. // cv::solve(c,d,result);
  529. //
  530. //
  531. // double a0 = result.at<double>(0, 0);
  532. // double a1 = result.at<double>(1, 0);
  533. // double a2 = result.at<double>(2, 0);
  534. // if(fabs(a2)<1.0e-6){
  535. // throw_msg(m_patch_id+" a2 = 0 in solver");
  536. // }
  537. // oa = -a1 / a2 / 2;
  538. // if(oa >180.0){oa -= 180.0;}
  539. // return oa;
  540. //}
  541. //double COptimalAngle::angle_fit_base(map<int, int>&){
  542. // return 0.0;
  543. //}
  544. //
  545. /////////////////////////////////////////////////////////////////////////////////
  546. //COptimalAnglePart::COptimalAnglePart(ConfigParam&cp, CGcvLogger* pLog)
  547. // :COptimalAngle(cp,pLog)
  548. //{
  549. //}
  550. //COptimalAnglePart::~COptimalAnglePart()
  551. //{
  552. // COptimalAngle::clear_imginfo();
  553. //}
  554. //// append()加载的图片,满足不是0-180度采样,但保证部分采样中一定存在最大值,
  555. //// 直接利用这些数值进行二次函数的拟合
  556. //double COptimalAnglePart::angle_fit(map<int,int>& an2width,bool is_base)
  557. //{
  558. //
  559. // double oa = 0.0;
  560. // std::vector<double> xx;//angle
  561. // std::vector<double> yy;//width
  562. //
  563. // map<int,int>::iterator it = an2width.begin();
  564. // for(it=an2width.begin(); it!=an2width.end(); ++it)
  565. // {
  566. // xx.push_back((double)(it->first));
  567. // yy.push_back((double)(it->second));
  568. // }
  569. // //sort
  570. // for(size_t i =0;i<xx.size()-1;++i){
  571. // for(size_t j = i+1; j<xx.size();++j){
  572. // if(xx[j]<xx[i]){
  573. // double tmp = xx[i];
  574. // xx[i]=xx[j];
  575. // xx[j]=tmp;
  576. // tmp = yy[i];
  577. // yy[i]=yy[j];
  578. // yy[j]=tmp;
  579. // }
  580. // }
  581. // }
  582. //
  583. // if(m_pLogger){
  584. // stringstream buff;
  585. // buff<<m_patch_id;
  586. // if(is_base){
  587. // buff<<" rootstock_base ";
  588. // }
  589. // else{
  590. // buff<<" rootstock_leaf ";
  591. // }
  592. // buff<<"opt-angle, (xi,yi): ";
  593. // for(size_t i=0;i<xx.size();++i){
  594. // buff<<"("<<xx[i]<<","<<yy[i]<<"), ";
  595. // }
  596. // m_pLogger->INFO(buff.str());
  597. // }
  598. //
  599. // if(an2width.size()<3){
  600. // throw_msg(m_patch_id+" not enough valid images, NEED append image");
  601. // }
  602. // // angle field check
  603. // if((xx.back()-xx.front())<90){
  604. // if(m_pLogger){
  605. // stringstream buff;
  606. // buff<<m_patch_id<<" x_end="<<xx.back()<<" x_front="<<xx.front();
  607. // buff<<" not enough angle field, small than 90 degree.";
  608. // m_pLogger->ERRORINFO(buff.str());
  609. // }
  610. // throw_msg(m_patch_id+" not enough angle field, small than 90 degree");
  611. // }
  612. // oa = opt_angle_part_impl(xx,yy,is_base);
  613. // if(m_pLogger){
  614. // stringstream buff;
  615. // buff<<m_patch_id;
  616. // if(is_base){
  617. // buff<<" rootstock_base ";
  618. // }
  619. // else{
  620. // buff<<" rootstock_leaf ";
  621. // }
  622. // buff<<" opt-angle: "<<oa;
  623. // m_pLogger->INFO(buff.str());
  624. // }
  625. // return oa;
  626. //}
  627. //double COptimalAnglePart::angle_fit_base(map<int, int>& an2width){
  628. //
  629. // double oa = 0.0;
  630. // //find the maximun angle
  631. // oa = angle_fit(an2width,true);
  632. //
  633. // //get the minimum angle
  634. // if(oa>45.0){oa -= 45.0;}
  635. // else{oa +=45.0;}
  636. // return oa;
  637. //}
  638. //void COptimalAnglePart::zero_cross_detect(
  639. // vector<int>&d_sign, //input
  640. // bool& is_local_max, // whether exists local max
  641. // int& local_limit_idx // local limit index of original vector
  642. //)
  643. //{
  644. // int min_zc_idx = -1;
  645. // int max_zc_idx = -1;
  646. // bool b_local_min =false;
  647. // bool b_local_max =false;
  648. // bool is_all_down=true;
  649. // bool is_all_up = true;
  650. // for (size_t i=0; i<d_sign.size();++i){
  651. // if (d_sign[i] >0){is_all_down=false;}
  652. // if (d_sign[i] <0){is_all_up=false;}
  653. // if (i == d_sign.size()-1){continue;}
  654. //
  655. // if (d_sign[i] >=0 && d_sign[i+1] <=0){
  656. // //is_local_max=true;
  657. // max_zc_idx = i+1;
  658. // b_local_max=true;
  659. //
  660. // }
  661. // if (d_sign[i] <=0 && d_sign[i+1] >=0){
  662. // //is_local_max=false;
  663. // min_zc_idx=i+1;
  664. // b_local_min=true;
  665. // }
  666. // }
  667. // if(b_local_min){
  668. // is_local_max=false;
  669. // local_limit_idx = min_zc_idx;
  670. // return;
  671. // }
  672. // if(b_local_max){
  673. // is_local_max=true;
  674. // local_limit_idx = max_zc_idx;
  675. // return;
  676. // }
  677. // if (is_all_down){
  678. // is_local_max=true;
  679. // local_limit_idx=0;
  680. // return;
  681. // }
  682. // if( is_all_up){
  683. // is_local_max=true;
  684. // local_limit_idx=d_sign.size();
  685. // return;
  686. // }
  687. // is_local_max=true;
  688. // local_limit_idx=-1;
  689. // return;
  690. //}
  691. //double COptimalAnglePart::limit_min_angle(
  692. // vector<double>&x, //sorted, ascending
  693. // vector<double>&y,
  694. // size_t min_idx
  695. // )
  696. //{
  697. // size_t i = min_idx;
  698. // if(fabs(x[i+1]-x[i])<1.0e-6 || fabs(x[i]-x[i-1])<1.0e-6){
  699. // throw_msg(m_patch_id+" limit_min_angle, diff_x < 1.0e-6");
  700. // }
  701. // double k_r = (y[i+1]-y[i])/(x[i+1]-x[i]);
  702. // double k_l = (y[i]-y[i-1])/(x[i]-x[i-1]);
  703. // double oa = 0.0;
  704. // double b_r = 0.0;
  705. // double b_l = 0.0;
  706. // double cross_x = 0;
  707. // if (fabs(k_r) > fabs(k_l)){
  708. // // right line
  709. // b_r = y[i]-k_r*x[i];
  710. // b_l = y[i-1] + k_r*x[i-1];
  711. // cross_x = line_cross(k_r,b_r,-k_r,b_l);
  712. // oa = cross_x;
  713. // }
  714. // else{
  715. // //# left line
  716. // b_l = y[i]-k_l*x[i];
  717. // b_r = y[i+1] + k_l*x[i+1];
  718. // cross_x = line_cross(k_l,b_l,-k_l,b_r);
  719. // oa = cross_x;
  720. // }
  721. // return oa;
  722. //}
  723. //
  724. //double COptimalAnglePart::line_cross(double k1,double b1,double k2,double b2)
  725. //{
  726. // double x = (b2-b1) / (k1-k2);
  727. // return x;
  728. //}
  729. //
  730. //double COptimalAnglePart::opt_angle_part_impl(
  731. // vector<double>&x, //sorted, ascending
  732. // vector<double>&y,
  733. // bool is_base)
  734. //{
  735. // // sign of difference of y
  736. // vector<int> dy_sign;
  737. // for(size_t i=1; i<y.size();++i){
  738. // double dd = y[i] - y[i-1];
  739. // if(dd>0){
  740. // dy_sign.push_back(1);
  741. // }
  742. // else{
  743. // if(dd<0){dy_sign.push_back(-1);}
  744. // else{dy_sign.push_back(0);}
  745. // }
  746. // }
  747. // //local limit search
  748. // bool is_local_max = false;
  749. // int local_limit_idx = -1;
  750. // zero_cross_detect(dy_sign,is_local_max,local_limit_idx);
  751. // if(local_limit_idx<0){
  752. // throw_msg(m_patch_id+" not found local limit");
  753. // }
  754. // // optimal angle calculation
  755. // vector<double> xx;
  756. // vector<double> yy;
  757. // double oa=0.0;
  758. // if(is_local_max){
  759. // //存在局部最大
  760. // if( local_limit_idx == 0 || local_limit_idx==y.size()-1){
  761. // //#单调
  762. // oa = x[local_limit_idx];
  763. // //if (local_limit_idx == 0){
  764. // // for(size_t i=0;i<3;++i){
  765. // /// xx.push_back(x[i]);
  766. // // yy.push_back(y[i]);
  767. // // }
  768. // //}
  769. // //else {
  770. // // for(size_t i=y.size()-3;i<y.size();++i){
  771. // // xx.push_back(x[i]);
  772. // // yy.push_back(y[i]);
  773. // // }
  774. // //}
  775. // //oa = qua_fitting(xx,yy);
  776. // if(m_pLogger){
  777. // m_pLogger->INFO(m_patch_id+ " angle fit -- signle --- max_angle");
  778. // }
  779. //
  780. // }
  781. // else{
  782. // // 极大值
  783. // for(size_t i=local_limit_idx-1;i<local_limit_idx+2;++i){
  784. // xx.push_back(x[i]);
  785. // yy.push_back(-y[i]);
  786. // }
  787. // oa = limit_min_angle(xx,yy,1);
  788. // if(m_pLogger){
  789. // m_pLogger->INFO(m_patch_id+" angle fit -- local max --- 3p insert");
  790. // }
  791. // }
  792. // }
  793. // else{
  794. // // 极小值
  795. // oa = limit_min_angle(x,y,local_limit_idx);
  796. // if(is_base){
  797. // //for base, offset 45 degree
  798. // if(oa >45){oa = oa-45.0;}
  799. // else{oa = oa+45.0;}
  800. // }
  801. // else{
  802. // // for rootstock, offset 90 degree
  803. // if(oa >90){oa = oa-90.0;}
  804. // else{oa = oa+90.0;}
  805. // }
  806. // if(m_pLogger){
  807. // m_pLogger->INFO(m_patch_id+" angle fit -- local min --- 3p insert");
  808. // }
  809. // }
  810. // return oa;
  811. //}
  812. CCotyledonAngle::CCotyledonAngle(ConfigParam&cp, CGcvLogger* pLog)
  813. :m_cparam(cp),
  814. m_pLogger(pLog),
  815. m_imgId(""),
  816. m_ppImgSaver(0),
  817. m_height(0),
  818. m_width(0)
  819. {
  820. }
  821. CCotyledonAngle::~CCotyledonAngle(void)
  822. {
  823. }
  824. void CCotyledonAngle::clear_imginfo(){
  825. if (m_pImginfoOpen){
  826. imginfo_release(&m_pImginfoOpen);
  827. m_pImginfoOpen=0;
  828. }
  829. if (m_pImginfoAngle){
  830. imginfo_release(&m_pImginfoAngle);
  831. m_pImginfoAngle=0;
  832. }
  833. }
  834. int CCotyledonAngle::angle_recognize(
  835. ImgInfo* imginfo,
  836. PositionInfo& posinfo
  837. )
  838. {
  839. //clear opencv windows
  840. if(m_cparam.image_show){destroyAllWindows();}
  841. clock_t t;
  842. clock_t t0 = clock();
  843. m_imgId = getImgId(img_type::oa);
  844. if(m_pLogger){
  845. m_pLogger->INFO(m_imgId + " cotyledon angle recognize begin.");
  846. }
  847. if(!isvalid(imginfo)){
  848. throw_msg(m_imgId+" invalid input image");
  849. }
  850. if(m_width==0 || m_height==0){
  851. m_width = imginfo->width;
  852. m_height = imginfo->height;
  853. }
  854. if(m_pLogger){
  855. stringstream buff;
  856. buff<<m_imgId<<" load image\twidth="<<imginfo->width
  857. <<"\theight="<<imginfo->height;
  858. m_pLogger->INFO(buff.str());
  859. }
  860. Mat img = imginfo2mat(imginfo);
  861. //for DEBUG
  862. //Mat img = *(Mat*)(imginfo);
  863. //m_width = img.cols/2;
  864. //m_height = img.rows/2;
  865. //cv::resize(img, img, cv::Size(m_width,m_height ));
  866. //end DEBUG
  867. //imshow_wait("src", img);
  868. if(m_cparam.self_camera){
  869. image_set_bottom(img,20,8);
  870. if(m_pLogger){
  871. m_pLogger->DEBUG(m_imgId+" oa_recognize image set bottom with pixel value 20.");
  872. }
  873. }
  874. if(m_cparam.oa_y_flip){
  875. flip(img,img,0);
  876. if(m_pLogger){
  877. m_pLogger->DEBUG(m_imgId+" oa_recognize image y fliped.");
  878. }
  879. }
  880. if(m_ppImgSaver && *m_ppImgSaver){
  881. (*m_ppImgSaver)->saveImage(img, m_imgId);
  882. if(m_pLogger){
  883. m_pLogger->DEBUG(m_imgId+" oa_recognize after image save.");
  884. }
  885. }
  886. if(m_cparam.image_show){
  887. Mat tmp_b = img.clone();
  888. imshow_wait("oa_image", tmp_b);
  889. }
  890. //1图像分割
  891. this->img_segment(img);
  892. if(m_cparam.image_show){
  893. imshow_wait("oa_image_bin", m_binImg);
  894. }
  895. Mat img_median, img_base, img_open, img_xor;
  896. cv::medianBlur(m_binImg,img_median, 5);
  897. Mat kernel = getStructuringElement(
  898. MORPH_ELLIPSE,
  899. Size( 2*m_cparam.oa_morph_radius + 1, 2*m_cparam.oa_morph_radius+1),
  900. Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
  901. );
  902. morphologyEx(
  903. img_median,
  904. img_base,
  905. MORPH_CLOSE,
  906. kernel,
  907. Point(-1,-1),
  908. 1);
  909. if(m_cparam.image_show){
  910. imshow_wait("img_base", img_base);
  911. }
  912. //2 找到2片叶子,计算各自重心,主方向,面积,横纵比,椭圆度,选择最优,计算需要旋转角度
  913. int iter_step = 3;
  914. int iter_open = 10;
  915. double leaf_area_th = (double)m_cparam.oa_min_leaf_area;
  916. vector<vector<Point>> contours;
  917. vector<Vec4i> hierarchy;
  918. vector<t_leaf> leafs;
  919. while( iter_open >0 && iter_open < 50){
  920. morphologyEx(
  921. img_base,
  922. img_open,
  923. MORPH_OPEN,
  924. kernel,
  925. Point(-1,-1),
  926. iter_open);
  927. if(m_cparam.image_show){
  928. imshow_wait("img_base", img_base);
  929. imshow_wait("img_open", img_open);
  930. }
  931. m_openImg = img_open.clone();
  932. contours.clear();
  933. hierarchy.clear();
  934. leafs.clear();
  935. findContours(img_open,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE);
  936. for(int i=0;i<contours.size();++i){
  937. double area = contourArea(contours[i]);
  938. if (area < leaf_area_th){continue;}
  939. RotatedRect minrect_ellips = fitEllipse(contours[i]);
  940. Moments moment = moments(contours[i]);
  941. double centx = moment.m10 / moment.m00;
  942. double centy = moment.m01 / moment.m00;
  943. t_leaf leaf;
  944. leaf.area = area;
  945. leaf.centx = centx;
  946. leaf.centy = centy;
  947. leaf.minrect_ellips = minrect_ellips;
  948. leaf.contours = contours[i];
  949. leafs.push_back(leaf);
  950. }
  951. if(leafs.size()<2){
  952. iter_open += iter_step;
  953. }
  954. if(leafs.size()>2){
  955. iter_open -= iter_step;
  956. }
  957. if(leafs.size()==2){
  958. break;
  959. }
  960. }
  961. if(leafs.size()!=2){
  962. throw_msg(m_imgId+" leafs detect failed");
  963. }
  964. //select one leaf for angle detection
  965. double min_err = 1.0e16;
  966. int min_err_idx = -1;
  967. for(size_t i =0; i<leafs.size();++i){
  968. double err = fit_error_ellipse(leafs[i].minrect_ellips, leafs[i].contours);
  969. if(err<min_err){
  970. min_err = err;
  971. min_err_idx = i;
  972. }
  973. }
  974. if(min_err_idx<0){
  975. throw_msg(m_imgId+" no normal leaf");
  976. }
  977. //计算旋转角度
  978. double all_area = leafs[0].area+leafs[1].area;
  979. double r0 = leafs[0].area/all_area;
  980. double all_centx = leafs[0].centx * r0 + leafs[1].centx * (1-r0);
  981. double all_centy = leafs[0].centy * r0 + leafs[1].centy * (1 - r0);
  982. float tar_angle1 = 90 + leafs[min_err_idx].minrect_ellips.angle;//长轴与x轴的夹角(图像坐标系下),90-270间
  983. float tar_angle2 = leafs[min_err_idx].minrect_ellips.angle - 90;//-90 ~ 90
  984. if(tar_angle2<0){
  985. tar_angle2+=360;
  986. }
  987. //叶尖方向(相对于两个叶片中心)
  988. float leaf_apex_angle = atan2(leafs[min_err_idx].centy - all_centy, leafs[min_err_idx].centx - all_centx) * 180 / CV_PI;
  989. if(leaf_apex_angle<0){
  990. leaf_apex_angle += 360;
  991. }
  992. float diff1 = fabs(leaf_apex_angle - tar_angle1);
  993. if(diff1>180){
  994. diff1 = 360 - diff1;
  995. }
  996. float diff2 = fabs(leaf_apex_angle - tar_angle2);
  997. if(diff2>180){
  998. diff2 = 360 - diff2;
  999. }
  1000. float rot_angle = tar_angle1;
  1001. if(diff2 < diff1){
  1002. rot_angle = tar_angle2;
  1003. }
  1004. rot_angle -= 180;//需要旋转的角度
  1005. //draw image
  1006. Mat ellipse_img = m_openImg.clone();
  1007. for(size_t j=0; j<leafs.size(); ++j){
  1008. stringstream buff;
  1009. buff<<"wa="<<(float)((int)(leafs[j].minrect_ellips.angle*10.0)/10.0);
  1010. Scalar color = Scalar(100);
  1011. if(j == min_err_idx){
  1012. color[0] = 180;
  1013. buff<<",rot="<<(float)((int)(rot_angle*10.0)/10.0);
  1014. }
  1015. Point2f vertices[4];
  1016. leafs[j].minrect_ellips.points(vertices);
  1017. for (int i = 0; i < 4; i++)
  1018. line(ellipse_img, vertices[i], vertices[(i+1)%4], color);
  1019. putText(ellipse_img,buff.str(),leafs[j].minrect_ellips.center, 0,0.5,color);
  1020. }
  1021. if(m_cparam.image_show){
  1022. imshow_wait("ellipse_img", ellipse_img);
  1023. }
  1024. // 3 返回结果
  1025. posinfo.rs_oa=(double)rot_angle;
  1026. if(m_pLogger){
  1027. stringstream buff;
  1028. buff<<m_imgId<<" detect rotat angle "<<posinfo.rs_oa;
  1029. m_pLogger->INFO(buff.str());
  1030. }
  1031. //4 返回图像
  1032. if(m_cparam.image_return){
  1033. this->clear_imginfo();
  1034. m_pImginfoOpen = mat2imginfo(m_openImg);
  1035. m_pImginfoAngle = mat2imginfo(ellipse_img);
  1036. posinfo.pp_images[0]=m_pImginfoOpen;
  1037. posinfo.pp_images[1]=m_pImginfoAngle;
  1038. if(m_ppImgSaver && *m_ppImgSaver){
  1039. (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  1040. (*m_ppImgSaver)->saveImage(ellipse_img, m_imgId+"_rst_1");
  1041. if(!m_grayImg.empty()){
  1042. (*m_ppImgSaver)->saveImage(m_grayImg, m_imgId+"_rst_2");
  1043. }
  1044. }
  1045. }
  1046. if(m_pLogger){
  1047. m_pLogger->INFO(m_imgId + " oa_recognize image finished.");
  1048. }
  1049. return 0;
  1050. }
  1051. double CCotyledonAngle::fit_error_ellipse(
  1052. RotatedRect& ellipse_rect,
  1053. vector<Point>& pts)
  1054. {
  1055. /*
  1056. ref:https://blog.csdn.net/fangyan90617/article/details/89486331
  1057. ellipse common function: A * x^2 + B * x*y + C * y^2 + D * x + E * y + F = 0
  1058. reformed function:
  1059. A * x'^2 + B * x'*y' + C * y'^2 + F = 0
  1060. x' = x - x0
  1061. y' = y - y0
  1062. from ellipse_rect, calculate A,B,C,F
  1063. */
  1064. if(pts.size()==0){
  1065. return 1.0e6;
  1066. }
  1067. float A, B,C, F;
  1068. get_ellipse_param(ellipse_rect, A, B,C, F);
  1069. double mean_err = 0.0;
  1070. double x,y,err;
  1071. for(size_t i=0; i<pts.size(); ++i){
  1072. x = (double)pts[i].x - ellipse_rect.center.x;
  1073. y = (double)pts[i].y - ellipse_rect.center.y;
  1074. err = A * x * x + B * x * y + C * y * y + F;
  1075. mean_err += err*err;
  1076. }
  1077. mean_err /= (double)pts.size();
  1078. mean_err = sqrt(mean_err);
  1079. return mean_err;
  1080. }
  1081. void CCotyledonAngle::get_ellipse_param(
  1082. RotatedRect&ellipse_rect,
  1083. float&A,
  1084. float&B,
  1085. float&C,
  1086. float&F)
  1087. {
  1088. float theta = ellipse_rect.angle * CV_PI / 180.0;
  1089. float a = ellipse_rect.size.width / 2.0;
  1090. float b = ellipse_rect.size.height / 2.0;
  1091. A = a * a * sin(theta) * sin(theta) + b * b * cos(theta) * cos(theta);
  1092. B = (-2.0) * (a * a - b * b) * sin(theta) * cos(theta);
  1093. C = a * a * cos(theta) * cos(theta) + b * b * sin(theta) * sin(theta);
  1094. F = (-1.0) * a * a * b * b;
  1095. }
  1096. void CCotyledonAngle::img_segment(Mat&img)
  1097. {
  1098. if(img.channels()!=1){
  1099. //color image ,bgr, for testing
  1100. /*Mat img_gray,b_img;
  1101. cvtColor(img,img_gray,COLOR_BGR2GRAY);
  1102. double th = threshold(
  1103. img_gray,
  1104. b_img,
  1105. 255,
  1106. 255,
  1107. THRESH_OTSU
  1108. );
  1109. b_img/= 255;*/
  1110. Mat img_hsv, b_img_hsv;
  1111. cvtColor(
  1112. img,
  1113. img_hsv,
  1114. COLOR_BGR2HSV
  1115. );
  1116. inRange(
  1117. img_hsv,
  1118. Scalar(30 , 30, 30),
  1119. Scalar(75, 255, 255),
  1120. b_img_hsv
  1121. );
  1122. /*imshow_wait("oa_image_range", b_img_hsv);
  1123. b_img_hsv/=255;
  1124. b_img = b_img.mul(b_img_hsv); */
  1125. //int morph_size = 1;
  1126. Mat kernel = getStructuringElement(
  1127. MORPH_RECT,
  1128. Size( 2*m_cparam.oa_morph_radius + 1, 2*m_cparam.oa_morph_radius+1),
  1129. Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
  1130. );
  1131. morphologyEx(
  1132. b_img_hsv,
  1133. m_binImg,
  1134. MORPH_CLOSE,
  1135. kernel,
  1136. Point(-1,-1),
  1137. m_cparam.oa_morph_iteration);
  1138. }
  1139. else{
  1140. //from imginfo image, gray image
  1141. //int morph_size = 1;
  1142. Mat b_img;
  1143. m_grayImg = img.clone();
  1144. double th = threshold(img, b_img, 255, 255,THRESH_OTSU);
  1145. Mat kernel = getStructuringElement(
  1146. MORPH_RECT,
  1147. Size( 2*m_cparam.oa_morph_radius + 1, 2*m_cparam.oa_morph_radius+1 ),
  1148. Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
  1149. );
  1150. morphologyEx(
  1151. b_img,
  1152. m_binImg,
  1153. MORPH_OPEN,
  1154. kernel,
  1155. Point(-1,-1),
  1156. m_cparam.oa_morph_iteration
  1157. );
  1158. }
  1159. }
  1160. };