cut_point_rs_reid.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. 砧木切割点后识别
  3. */
  4. #include <opencv2\imgproc\imgproc.hpp>
  5. #include <opencv2\features2d\features2d.hpp>
  6. //#include <opencv2\nonfree\features2d.hpp>
  7. #include <math.h>
  8. #include <algorithm>
  9. #include "cut_point_rs_reid.h"
  10. #include "utils.h"
  11. #include "data_def.h"
  12. #include "logger.h"
  13. //using namespace cv;
  14. namespace graft_cv{
  15. CRootStockCutPointReid::CRootStockCutPointReid(ConfigParam&cp,CGcvLogger*pLog/*=0*/)
  16. :m_cparam(cp),
  17. m_pLogger(pLog),
  18. m_pImginfoBinFork(0),
  19. m_pImgCorners(0),
  20. m_pImgCutPoint(0),
  21. m_imgId(""),
  22. m_ppImgSaver(0)
  23. {
  24. }
  25. CRootStockCutPointReid::~CRootStockCutPointReid()
  26. {
  27. this->clear_imginfo();
  28. }
  29. void CRootStockCutPointReid::clear_imginfo(){
  30. if (m_pImginfoBinFork){
  31. imginfo_release(&m_pImginfoBinFork);
  32. m_pImginfoBinFork=0;
  33. }
  34. if (m_pImgCorners){
  35. imginfo_release(&m_pImgCorners);
  36. m_pImgCorners=0;
  37. }
  38. if (m_pImgCutPoint){
  39. imginfo_release(&m_pImgCutPoint);
  40. m_pImgCutPoint=0;
  41. }
  42. }
  43. int CRootStockCutPointReid::cut_point_reid(
  44. ImgInfo* imginfo,
  45. cv::Mat&cimg,
  46. const char * pre_img_id,
  47. PositionInfo& posinfo,
  48. map<string, cv::Mat>& img_cache
  49. )
  50. {
  51. // cimg --- color image, bgr
  52. if(m_pLogger){
  53. m_pLogger->INFO(m_imgId +" rootstock cut_pt reid begin");
  54. }
  55. if(!pre_img_id){
  56. if(m_pLogger){
  57. m_pLogger->ERRORINFO(m_imgId +" pre-image id is NULL");
  58. }
  59. return 1;
  60. }
  61. string pre_imgid(pre_img_id);
  62. map<string, cv::Mat>::iterator iter = img_cache.find(pre_imgid);
  63. if(iter==img_cache.end()){
  64. if(m_pLogger){
  65. m_pLogger->ERRORINFO(m_imgId +" pre-image NOT in cache");
  66. }
  67. return 1;
  68. }
  69. m_preGrayImg = iter->second;
  70. m_imgId = getImgId(img_type::rs_reid);
  71. //1 image segment
  72. clock_t t;
  73. clock_t t0 = clock();
  74. cv::Mat img;
  75. if(imginfo){
  76. if(m_pLogger){
  77. stringstream buff;
  78. buff<<m_imgId<<" rootstock image, width="<<imginfo->width
  79. <<"\theight="<<imginfo->height;
  80. m_pLogger->INFO(buff.str());
  81. }
  82. if(!isvalid(imginfo)){
  83. if(m_pLogger){
  84. m_pLogger->ERRORINFO(m_imgId+" rootstock input image invalid.");
  85. }
  86. throw_msg(m_imgId+" invalid input image");
  87. }
  88. img = imginfo2mat(imginfo);
  89. }
  90. else{
  91. if(m_pLogger){
  92. stringstream buff;
  93. buff<<m_imgId<<"rootstock image, width="<<cimg.cols
  94. <<"\theight="<<cimg.rows;
  95. m_pLogger->INFO(buff.str());
  96. }
  97. if(cimg.empty()){
  98. if(m_pLogger){
  99. m_pLogger->ERRORINFO(m_imgId+" rootstock input image invalid.");
  100. }
  101. throw_msg(m_imgId +" invalid input image");
  102. }
  103. img = cimg;
  104. }
  105. if(m_cparam.self_camera){
  106. image_set_bottom(img,20,8);
  107. if(m_pLogger){
  108. m_pLogger->DEBUG(m_imgId+" image set bottom with pixel value 20.");
  109. }
  110. }
  111. if(m_cparam.rs_y_flip){
  112. flip(img,img,0);
  113. if(m_pLogger){
  114. m_pLogger->DEBUG(m_imgId+" image y fliped.");
  115. }
  116. }
  117. //image saver
  118. if(m_ppImgSaver && *m_ppImgSaver){
  119. (*m_ppImgSaver)->saveImage(img, m_imgId);
  120. }
  121. if(m_pLogger){
  122. m_pLogger->DEBUG(m_imgId+" before image segment.");
  123. }
  124. ///////////////////////////////////////////////////////
  125. // image segment
  126. this->img_preprocess(img);
  127. if(m_pLogger){
  128. m_pLogger->DEBUG(m_imgId+" after image gray.");
  129. }
  130. if(m_cparam.image_show){
  131. cv::destroyAllWindows();
  132. imshow_wait("rs_pre_gray",m_preGrayImg);
  133. imshow_wait("rs_gray",m_grayImg);
  134. }
  135. else{
  136. t = clock();
  137. if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_proc){
  138. if(m_pLogger){
  139. m_pLogger->ERRORINFO(m_imgId+" rootstock reid timeout.");
  140. }
  141. throw_msg(m_imgId+" time out");
  142. }
  143. }
  144. if(m_pLogger){
  145. m_pLogger->DEBUG(m_imgId+" after pre- and cur- gray image show.");
  146. }
  147. //特征提取
  148. //int max_feature = 500;
  149. //cv::OrbFeatureDetector fts_detector(max_feature);
  150. ////SurfFeatureDetector fts_detector(max_feature);
  151. //std::vector<cv::KeyPoint> keypoints_pre, keypoints_cur;
  152. //fts_detector.detect( m_preGrayImg, keypoints_pre );
  153. //fts_detector.detect( m_grayImg, keypoints_cur );
  154. //cv::SurfDescriptorExtractor extractor;
  155. //cv::Mat descriptors_pre, descriptors_cur;
  156. //extractor.compute( m_preGrayImg, keypoints_pre, descriptors_pre );
  157. //extractor.compute( m_grayImg, keypoints_cur, descriptors_cur );
  158. ////-- Step 3: Matching descriptor vectors with a brute force matcher
  159. //cv::BFMatcher matcher(cv::NORM_L2);
  160. //std::vector< cv::DMatch > matches;
  161. //cv::matcher.match( descriptors_pre, descriptors_cur, matches );
  162. //if(m_cparam.image_show){
  163. // //-- Draw matches
  164. // cv::Mat img_matches;
  165. // cv::drawMatches( m_preGrayImg, keypoints_pre, m_grayImg, keypoints_cur, matches, img_matches );
  166. // //-- Show detected matches
  167. // cv::imshow("Matches", img_matches );
  168. // cv::waitKey(-1);
  169. //}
  170. posinfo.rs_reid_upoint_x = 10.0;
  171. posinfo.rs_reid_upoint_y = 10.0;
  172. posinfo.rs_reid_lpoint_x = 20.0;
  173. posinfo.rs_reid_lpoint_y = 20.0;
  174. //if(m_pLogger){
  175. // stringstream buff;
  176. // buff<<m_imgId<<" rootstock image, rs_cut_upoint(mm)("<<rs_cut_upoint_x
  177. // <<","<<rs_cut_upoint_y<<")"
  178. // <<", rs_stem_diameter(mm)="<<rs_stem_diameter
  179. // <<", lower_cut_pt(mm)("<<rs_cut_lpoint_x
  180. // <<","<<rs_cut_lpoint_y<<")";
  181. // m_pLogger->INFO(buff.str());
  182. //}
  183. //// return images: posinfo.pp_images
  184. //if(m_cparam.image_return){
  185. // this->clear_imginfo();
  186. // //0) image id
  187. // strcpy(posinfo.rs_img_id,m_imgId.c_str());
  188. // //1)
  189. // //stem x-range
  190. // line(m_binImg,Point(stem_x0,0),Point(stem_x0,m_binImg.cols-1),Scalar(100),2);
  191. // line(m_binImg,Point(stem_x1,0),Point(stem_x1,m_binImg.cols-1),Scalar(100),2);
  192. // //fork right point
  193. // circle(m_binImg, Point(stem_fork_left_x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0);
  194. // m_pImginfoBinFork=mat2imginfo(m_binImg);
  195. // //3 cut point int gray image
  196. // circle(m_grayImg, Point(stem_fork_left_x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0);
  197. // circle(m_grayImg, Point(stem_fork_right_x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0);//v0.5.9.3 reference point
  198. // circle(m_grayImg, Point(cut_pt.x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0);//v0.5.9.3 reference point
  199. // circle(m_grayImg, Point(cut_pt.x,stem_fork_y),2, Scalar(255,0,255), -1, 8,0);
  200. // circle(m_grayImg, Point(lower_cut_pt.x,lower_cut_pt.y),5, Scalar(200,0,200), -1, 8,0);
  201. // image_draw_line(m_grayImg,cut_pt.x,cut_pt.y,lower_cut_pt.x,lower_cut_pt.y);
  202. //
  203. // m_pImgCutPoint = mat2imginfo(m_grayImg);
  204. // posinfo.pp_images[0]=m_pImginfoBinFork;
  205. // posinfo.pp_images[1]=m_pImgCutPoint;
  206. // if(m_ppImgSaver && *m_ppImgSaver){
  207. // (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  208. // (*m_ppImgSaver)->saveImage(m_grayImg, m_imgId+"_rst_1");
  209. // }
  210. //}
  211. if(m_pLogger){
  212. m_pLogger->INFO(m_imgId +" rootstock cut reid detect finished.");
  213. }
  214. return 0;
  215. };
  216. void CRootStockCutPointReid::img_preprocess(cv::Mat&img)
  217. {
  218. //灰度化
  219. cv::Mat b_img;
  220. if(img.channels()!=1){
  221. //color image ,bgr, for testing
  222. cvtColor(img,m_grayImg, cv::COLOR_BGR2GRAY);
  223. }
  224. else{
  225. m_grayImg = img.clone();
  226. }
  227. /*Mat kernel = getStructuringElement(
  228. MORPH_ELLIPSE,
  229. Size( 2*m_cparam.rs_morph_radius + 1, 2*m_cparam.rs_morph_radius+1 ),
  230. Point( m_cparam.rs_morph_radius, m_cparam.rs_morph_radius )
  231. );
  232. double th = threshold(m_grayImg, b_img, 255, 255,THRESH_OTSU);
  233. morphologyEx(
  234. b_img,
  235. m_binImg,
  236. MORPH_CLOSE,
  237. kernel,
  238. Point(-1,-1),
  239. m_cparam.rs_morph_iteration
  240. );*/
  241. }
  242. ///////////////////////////////////////////////////////////////////////////////////////
  243. ///////////////////////////////////////////////////////////////////////////////////////
  244. CSolaCutPointReid::CSolaCutPointReid(ConfigParam&cp, int stemType, CGcvLogger*pLog)
  245. :m_cparam(cp),
  246. m_pLogger(pLog),
  247. m_stem_type(stemType),
  248. m_imgId(""),
  249. m_ppImgSaver(0),
  250. m_pImginfoBin(0),
  251. m_pImgCutPoint(0)
  252. {
  253. //m_stem_type -- 0-接穗, 1-砧木
  254. }
  255. CSolaCutPointReid::~CSolaCutPointReid()
  256. {}
  257. void CSolaCutPointReid::clear_imginfo() {
  258. if (m_pImginfoBin) {
  259. imginfo_release(&m_pImginfoBin);
  260. m_pImginfoBin = 0;
  261. }
  262. if (m_pImgCutPoint) {
  263. imginfo_release(&m_pImgCutPoint);
  264. m_pImgCutPoint = 0;
  265. }
  266. }
  267. int CSolaCutPointReid::cut_point_reid(
  268. ImgInfo* imginfo,
  269. cv::Mat&cimg,
  270. PositionInfo& posinfo
  271. )
  272. {
  273. if (m_stem_type == 0) {
  274. m_imgId = getImgId(img_type::sola_sc_reid);
  275. }
  276. else {
  277. m_imgId = getImgId(img_type::sola_rs_reid);
  278. }
  279. //1 image segment
  280. clock_t t;
  281. clock_t t0 = clock();
  282. cv::Mat img;
  283. if (imginfo) {
  284. if (m_pLogger) {
  285. stringstream buff;
  286. buff << m_imgId<<" "<<get_stem_type_name() << " image, width=" << imginfo->width
  287. << "\theight=" << imginfo->height;
  288. m_pLogger->INFO(buff.str());
  289. }
  290. if (!isvalid(imginfo)) {
  291. if (m_pLogger) {
  292. m_pLogger->ERRORINFO(m_imgId +string(" ")+get_stem_type_name() + " input image invalid.");
  293. }
  294. throw_msg(m_imgId + string(" ") + get_stem_type_name() + " invalid input image");
  295. }
  296. img = imginfo2mat(imginfo);
  297. }
  298. else {
  299. if (m_pLogger) {
  300. stringstream buff;
  301. buff << m_imgId << " " << get_stem_type_name() << " image, width=" << cimg.cols
  302. << "\theight=" << cimg.rows;
  303. m_pLogger->INFO(buff.str());
  304. }
  305. if (cimg.empty()) {
  306. if (m_pLogger) {
  307. m_pLogger->ERRORINFO(m_imgId +string(" ")+ get_stem_type_name() + " input image invalid.");
  308. }
  309. throw_msg(m_imgId + string(" ") +get_stem_type_name() + " invalid input image");
  310. }
  311. img = cimg;
  312. }
  313. /*if (m_cparam.self_camera) {
  314. image_set_bottom(img, 20, 8);
  315. if (m_pLogger) {
  316. m_pLogger->DEBUG(m_imgId + " image set bottom with pixel value 20.");
  317. }
  318. }*/
  319. if (m_cparam.rs_y_flip) {
  320. flip(img, img, 0);
  321. if (m_pLogger) {
  322. m_pLogger->DEBUG(m_imgId + string(" ") + get_stem_type_name() + " image y fliped.");
  323. }
  324. }
  325. //image saver
  326. if (m_ppImgSaver && *m_ppImgSaver) {
  327. (*m_ppImgSaver)->saveImage(img, m_imgId);
  328. }
  329. if (m_pLogger) {
  330. m_pLogger->DEBUG(m_imgId + string(" ") + get_stem_type_name() + " before image segment.");
  331. }
  332. ///////////////////////////////////////////////////////
  333. // image segment
  334. this->img_preprocess(img);
  335. if (m_pLogger) {
  336. m_pLogger->DEBUG(m_imgId + " after image gray.");
  337. }
  338. if (m_cparam.image_show) {
  339. cv::destroyAllWindows();
  340. imshow_wait("gray", m_grayImg);
  341. imshow_wait("binary", m_binImg);
  342. }
  343. else {
  344. t = clock();
  345. if (1000.0*((float)(t - t0)) / CLOCKS_PER_SEC>(float)m_cparam.timeout_proc) {
  346. if (m_pLogger) {
  347. m_pLogger->ERRORINFO(m_imgId + " sola cut points reid timeout.");
  348. }
  349. throw_msg(m_imgId + " time out");
  350. }
  351. }
  352. if (m_pLogger) {
  353. m_pLogger->DEBUG(m_imgId + " after gray image show.");
  354. }
  355. // find object
  356. vector<vector<cv::Point>> contours;
  357. vector<cv::Vec4i> hierarchy;
  358. contours.clear();
  359. hierarchy.clear();
  360. int obj_area_th = 100;//m_cparam
  361. double max_area = 0;
  362. int max_idx = -1;
  363. findContours(m_binImg, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
  364. for (int i = 0; i<contours.size(); ++i) {
  365. double area = contourArea(contours[i]);
  366. if (area < obj_area_th) { continue; }
  367. if(area<max_area) { continue; }
  368. max_area = area;
  369. max_idx = i;
  370. }
  371. if (max_idx < 0 || contours[max_idx].size()==0) {
  372. throw_msg(m_imgId + " no valid object");
  373. }
  374. // 找到切口处y值
  375. int minx, maxx, miny, maxy;
  376. int cut_miny, cut_midy, cut_maxy;
  377. find_cut_ys(contours[max_idx],
  378. minx, maxx, miny, maxy,
  379. cut_miny, cut_midy, cut_maxy);
  380. int box_width, box_height;
  381. box_width = abs(maxx - minx);
  382. box_height = abs(maxy - miny);
  383. if (box_width < 5) {
  384. throw_msg(m_imgId + " no valid object");
  385. }
  386. float hw_ratio = (float)(box_height) / (float)(box_width);
  387. if (hw_ratio < 1.0) {
  388. throw_msg(m_imgId + " no valid object");
  389. }
  390. float area_th = (float)(m_grayImg.rows*m_grayImg.cols) / 5.0;
  391. if (max_area > area_th) {
  392. throw_msg(m_imgId + " no valid object");
  393. }
  394. // 夹爪基面的y
  395. int base_y = maxy; // 砧木情况
  396. if (m_stem_type == 0) {// 接穗情况
  397. base_y = miny;
  398. }
  399. if (m_cparam.image_show) {
  400. cv::Mat tmp = m_grayImg.clone();
  401. cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200));
  402. cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200));
  403. cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200));
  404. cv::line(tmp, cv::Point(minx, base_y), cv::Point(maxx, base_y), cv::Scalar(255));
  405. imshow_wait("gray_lines", tmp);
  406. }
  407. if (m_stem_type == 0) {//接穗,坐标左上角位0点
  408. posinfo.sc_reid_sola_upoint_x = base_y; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿)
  409. posinfo.sc_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1 ;
  410. posinfo.sc_reid_sola_cpoint_x = minx;
  411. posinfo.sc_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1;
  412. posinfo.sc_reid_sola_lpoint_x = minx;
  413. posinfo.sc_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1;
  414. }
  415. else {//砧木,坐标左下角位0点
  416. posinfo.rs_reid_sola_upoint_x = m_grayImg.rows - base_y - 1; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿)
  417. posinfo.rs_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1;
  418. posinfo.rs_reid_sola_cpoint_x = minx;
  419. posinfo.rs_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1;
  420. posinfo.rs_reid_sola_lpoint_x = minx;
  421. posinfo.rs_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1;
  422. }
  423. if(m_pLogger){
  424. stringstream buff;
  425. buff<<m_imgId<<" sola rootstock(scion) image, rs_cut_upoint("<< posinfo.rs_reid_sola_upoint_x
  426. <<","<< posinfo.rs_reid_sola_upoint_y <<")"
  427. <<", rs_cut_cpoint=("<< posinfo.rs_reid_sola_cpoint_x
  428. << "," << posinfo.rs_reid_sola_cpoint_y << ")"
  429. <<", rs_cut_lpoint(mm)("<< posinfo.rs_reid_sola_lpoint_x
  430. <<","<< posinfo.rs_reid_sola_lpoint_y <<")";
  431. m_pLogger->INFO(buff.str());
  432. }
  433. // return images: posinfo.pp_images
  434. if(m_cparam.image_return){
  435. this->clear_imginfo();
  436. //0) image id
  437. strcpy(posinfo.rs_img_id,m_imgId.c_str());
  438. //1) bin image
  439. m_pImginfoBin=mat2imginfo(m_binImg);
  440. //2 cut point int gray image
  441. cv::Mat tmp = m_grayImg.clone();
  442. cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200));
  443. cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200));
  444. cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200));
  445. m_pImgCutPoint = mat2imginfo(tmp);
  446. posinfo.pp_images[0] = m_pImginfoBin;
  447. posinfo.pp_images[1] = m_pImgCutPoint;
  448. if(m_ppImgSaver && *m_ppImgSaver){
  449. (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  450. (*m_ppImgSaver)->saveImage(tmp, m_imgId+"_rst_1");
  451. }
  452. }
  453. if (m_pLogger) {
  454. if (m_stem_type == 0) {
  455. m_pLogger->INFO(m_imgId + " sola scion cut reid detect finished.");
  456. }
  457. else {
  458. m_pLogger->INFO(m_imgId + " sola rootstock cut reid detect finished.");
  459. }
  460. }
  461. return 0;
  462. };
  463. void CSolaCutPointReid::find_cut_ys(
  464. vector<cv::Point>&points,
  465. int&minx,int& maxx,
  466. int&miny,int&maxy,
  467. int&cut_miny, int&cut_midy, int&cut_maxy
  468. )
  469. {
  470. //找到切口的y值
  471. // 找到边框
  472. minx = miny = 1000;
  473. maxx = maxy = -1;
  474. for (auto &pt : points) {
  475. if (pt.x > maxx) { maxx = pt.x; }
  476. if (pt.x < minx) { minx = pt.x; }
  477. if (pt.y > maxy) { maxy = pt.y; }
  478. if (pt.y < miny) { miny = pt.y; }
  479. }
  480. // 找到水平的边
  481. std::vector<int> hist(maxy-miny+1,0);
  482. std::vector<int> hist_x0(maxy - miny + 1, 1000);
  483. std::vector<int> hist_x1(maxy - miny + 1, 0);
  484. std::vector<int> hist_width(maxy - miny + 1, 0);
  485. for (auto&pt : points) {
  486. int idx = pt.y - miny;
  487. hist.at(idx) += 1;
  488. if (pt.x > hist_x1.at(idx)) {
  489. hist_x1.at(idx) = pt.x;
  490. }
  491. if (pt.x < hist_x0.at(idx)) {
  492. hist_x0.at(idx) = pt.x;
  493. }
  494. }
  495. for (int i = 0; i < hist.size(); ++i) {
  496. hist_width.at(i) = hist_x1.at(i) - hist_x0.at(i);
  497. }
  498. //计算茎的直径
  499. std::vector<int> hist_width_copy(hist_width);
  500. sort(hist_width_copy.begin(), hist_width_copy.end());
  501. int pert_idx = int(0.95*hist_width_copy.size());
  502. int stem_width = hist_width_copy.at(pert_idx);
  503. int max_idx = max_element(hist.begin(), hist.end()) - hist.begin();
  504. if (max_idx<int(hist.size() / 2.0)) {
  505. //下锥,
  506. std::vector<int> hist_width_rev;
  507. for (int i = hist_width.size()-1; i >=0 ; i--) {
  508. hist_width_rev.push_back(hist_width.at(i));
  509. }
  510. //在茎的指导下进行查找
  511. int pos = trend_detect_pos(hist_width_rev, stem_width);
  512. cut_miny = maxy - pos;
  513. cut_maxy = maxy;
  514. cut_midy = int((cut_miny + cut_maxy) / 2);
  515. }
  516. else {
  517. //上锥,找到上锥点
  518. int pos = trend_detect_pos(hist_width, stem_width);
  519. cut_miny = miny;
  520. cut_maxy = miny + pos;
  521. cut_midy = int((cut_miny + cut_maxy) / 2);
  522. }
  523. }
  524. void CSolaCutPointReid::find_cut_curve(vector<cv::Point>&curve_points,
  525. vector<cv::Point>&cut_curve, cv::RotatedRect& retval)
  526. {
  527. int max_y = curve_points[0].y;
  528. int min_y = curve_points[0].y;
  529. int max_x = curve_points[0].x;
  530. int min_x = curve_points[0].x;
  531. for (auto&pt : curve_points) {
  532. if (pt.y > max_y) {
  533. max_y = pt.y;
  534. }
  535. if (pt.y < min_y) {
  536. min_y = pt.y;
  537. }
  538. if (pt.x > max_x) {
  539. max_x = pt.x;
  540. }
  541. if (pt.x < min_x) {
  542. min_x = pt.x;
  543. }
  544. }
  545. vector<int>stem_width;
  546. int line_min_x, line_max_x;
  547. for (int y = min_y; y <= max_y;++y) {
  548. line_min_x = max_x;
  549. line_max_x = min_x;
  550. for (auto&pt : curve_points) {
  551. if (pt.y != y) { continue; }
  552. if (pt.x < line_min_x) { line_min_x = pt.x; }
  553. if (pt.x > line_max_x) { line_max_x = pt.x; }
  554. }
  555. stem_width.push_back(line_max_x - line_min_x + 1);
  556. }
  557. //计算茎的直径
  558. std::vector<int> hist_width_copy(stem_width);
  559. sort(hist_width_copy.begin(), hist_width_copy.end());
  560. int max_idx = int(0.98*hist_width_copy.size());
  561. int max_stem_width = hist_width_copy.at(max_idx);
  562. // find y range curve
  563. int min_yc = min_y;
  564. int max_yc = max_y;
  565. if (m_stem_type != 0) {//砧木
  566. int pos = trend_detect_pos(stem_width, max_stem_width,15);
  567. if (pos > 0) {
  568. max_yc = min_yc + pos;
  569. }
  570. else {
  571. throw(m_imgId + " not found cut curve range");
  572. }
  573. }
  574. else { //接穗
  575. vector<int> stem_width_r;
  576. vector<int>::reverse_iterator rit = stem_width.rbegin();
  577. for (; rit != stem_width.rend(); ++rit) {
  578. stem_width_r.push_back(*rit);
  579. }
  580. int pos = trend_detect_pos(stem_width_r, max_stem_width, 15);
  581. if (pos > 0) {
  582. min_yc = max_yc - pos;
  583. }
  584. else {
  585. throw(m_imgId + " not found cut curve range");
  586. }
  587. }
  588. // copy cut curve points
  589. vector<cv::Point>cut_curve_points;
  590. for (auto&pt : curve_points) {
  591. if (pt.y >= min_yc && pt.y <= max_yc) {
  592. cut_curve_points.push_back(cv::Point(pt));
  593. }
  594. }
  595. // ecllipse fit
  596. retval = cv::fitEllipse(cut_curve_points);
  597. //if (m_cparam.image_show) {
  598. // //cv::destroyAllWindows();
  599. // cv::Mat tmp = m_grayImg.clone();
  600. // for (auto&pt : cut_curve_points) {
  601. // tmp.at<cv::uint8_t>(pt.y, pt.x+500) = 200;
  602. // }
  603. // //cv::ellipse(tmp, retval, cv::Scalar(200));
  604. // imshow_wait("cut_curve", tmp);
  605. //}
  606. }
  607. string CSolaCutPointReid::get_stem_type_name() {
  608. if (m_stem_type == 0) {
  609. return string("solanaceae scion");
  610. }
  611. else {
  612. return string("solanaceae rootstock");
  613. }
  614. }
  615. void CSolaCutPointReid::img_preprocess(cv::Mat&img)
  616. {
  617. //灰度化
  618. cv::Mat b_img;
  619. if (img.channels() != 1) {
  620. //color image ,bgr, for testing
  621. cvtColor(img, m_grayImg, cv::COLOR_BGR2GRAY);
  622. }
  623. else {
  624. m_grayImg = img.clone();
  625. }
  626. cv::Mat kernel = cv::getStructuringElement(
  627. cv::MORPH_ELLIPSE,
  628. cv::Size(5, 5),
  629. cv::Point(2,2)
  630. );
  631. double th = threshold(m_grayImg, b_img, 255, 255, cv::THRESH_OTSU);
  632. morphologyEx(
  633. b_img,
  634. m_binImg,
  635. cv::MORPH_CLOSE,
  636. kernel,
  637. cv::Point(-1, -1),
  638. 2
  639. );
  640. }
  641. }