cut_point_rs_reid.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. // 夹爪基面的y
  381. int base_y = maxy; // 砧木情况
  382. if (m_stem_type == 0) {// 接穗情况
  383. base_y = miny;
  384. }
  385. if (m_cparam.image_show) {
  386. cv::Mat tmp = m_grayImg.clone();
  387. cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200));
  388. cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200));
  389. cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200));
  390. cv::line(tmp, cv::Point(minx, base_y), cv::Point(maxx, base_y), cv::Scalar(255));
  391. imshow_wait("gray_lines", tmp);
  392. }
  393. if (m_stem_type == 0) {//接穗,坐标左上角位0点
  394. posinfo.sc_reid_sola_upoint_x = base_y; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿)
  395. posinfo.sc_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1 ;
  396. posinfo.sc_reid_sola_cpoint_x = minx;
  397. posinfo.sc_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1;
  398. posinfo.sc_reid_sola_lpoint_x = minx;
  399. posinfo.sc_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1;
  400. }
  401. else {//砧木,坐标左下角位0点
  402. posinfo.rs_reid_sola_upoint_x = m_grayImg.rows - base_y - 1; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿)
  403. posinfo.rs_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1;
  404. posinfo.rs_reid_sola_cpoint_x = minx;
  405. posinfo.rs_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1;
  406. posinfo.rs_reid_sola_lpoint_x = minx;
  407. posinfo.rs_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1;
  408. }
  409. if(m_pLogger){
  410. stringstream buff;
  411. buff<<m_imgId<<" sola rootstock(scion) image, rs_cut_upoint("<< posinfo.rs_reid_sola_upoint_x
  412. <<","<< posinfo.rs_reid_sola_upoint_y <<")"
  413. <<", rs_cut_cpoint=("<< posinfo.rs_reid_sola_cpoint_x
  414. << "," << posinfo.rs_reid_sola_cpoint_y << ")"
  415. <<", rs_cut_lpoint(mm)("<< posinfo.rs_reid_sola_lpoint_x
  416. <<","<< posinfo.rs_reid_sola_lpoint_y <<")";
  417. m_pLogger->INFO(buff.str());
  418. }
  419. // return images: posinfo.pp_images
  420. if(m_cparam.image_return){
  421. this->clear_imginfo();
  422. //0) image id
  423. strcpy(posinfo.rs_img_id,m_imgId.c_str());
  424. //1) bin image
  425. m_pImginfoBin=mat2imginfo(m_binImg);
  426. //2 cut point int gray image
  427. cv::Mat tmp = m_grayImg.clone();
  428. cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200));
  429. cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200));
  430. cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200));
  431. m_pImgCutPoint = mat2imginfo(tmp);
  432. posinfo.pp_images[0] = m_pImginfoBin;
  433. posinfo.pp_images[1] = m_pImgCutPoint;
  434. if(m_ppImgSaver && *m_ppImgSaver){
  435. (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
  436. (*m_ppImgSaver)->saveImage(tmp, m_imgId+"_rst_1");
  437. }
  438. }
  439. if (m_pLogger) {
  440. if (m_stem_type == 0) {
  441. m_pLogger->INFO(m_imgId + " sola scion cut reid detect finished.");
  442. }
  443. else {
  444. m_pLogger->INFO(m_imgId + " sola rootstock cut reid detect finished.");
  445. }
  446. }
  447. return 0;
  448. };
  449. void CSolaCutPointReid::find_cut_ys(
  450. vector<cv::Point>&points,
  451. int&minx,int& maxx,
  452. int&miny,int&maxy,
  453. int&cut_miny, int&cut_midy, int&cut_maxy
  454. )
  455. {
  456. //找到切口的y值
  457. // 找到边框
  458. minx = miny = 1000;
  459. maxx = maxy = -1;
  460. for (auto &pt : points) {
  461. if (pt.x > maxx) { maxx = pt.x; }
  462. if (pt.x < minx) { minx = pt.x; }
  463. if (pt.y > maxy) { maxy = pt.y; }
  464. if (pt.y < miny) { miny = pt.y; }
  465. }
  466. // 找到水平的边
  467. std::vector<int> hist(maxy-miny+1,0);
  468. std::vector<int> hist_x0(maxy - miny + 1, 1000);
  469. std::vector<int> hist_x1(maxy - miny + 1, 0);
  470. std::vector<int> hist_width(maxy - miny + 1, 0);
  471. for (auto&pt : points) {
  472. int idx = pt.y - miny;
  473. hist.at(idx) += 1;
  474. if (pt.x > hist_x1.at(idx)) {
  475. hist_x1.at(idx) = pt.x;
  476. }
  477. if (pt.x < hist_x0.at(idx)) {
  478. hist_x0.at(idx) = pt.x;
  479. }
  480. }
  481. for (int i = 0; i < hist.size(); ++i) {
  482. hist_width.at(i) = hist_x1.at(i) - hist_x0.at(i);
  483. }
  484. //计算茎的直径
  485. std::vector<int> hist_width_copy(hist_width);
  486. sort(hist_width_copy.begin(), hist_width_copy.end());
  487. int pert_idx = int(0.98*hist_width_copy.size());
  488. int stem_width = hist_width_copy.at(pert_idx);
  489. int max_idx = max_element(hist.begin(), hist.end()) - hist.begin();
  490. if (max_idx<int(hist.size() / 2.0)) {
  491. //下锥,
  492. std::vector<int> hist_width_rev;
  493. for (int i = hist_width.size()-1; i >=0 ; i--) {
  494. hist_width_rev.push_back(hist_width.at(i));
  495. }
  496. //在茎的指导下进行查找
  497. int pos = trend_detect_pos(hist_width_rev, stem_width);
  498. cut_miny = maxy - pos;
  499. cut_maxy = maxy;
  500. cut_midy = int((cut_miny + cut_maxy) / 2);
  501. }
  502. else {
  503. //上锥,找到上锥点
  504. int pos = trend_detect_pos(hist_width, stem_width);
  505. cut_miny = miny;
  506. cut_maxy = miny + pos;
  507. cut_midy = int((cut_miny + cut_maxy) / 2);
  508. }
  509. }
  510. void CSolaCutPointReid::find_cut_curve(vector<cv::Point>&curve_points,
  511. vector<cv::Point>&cut_curve, cv::RotatedRect& retval)
  512. {
  513. int max_y = curve_points[0].y;
  514. int min_y = curve_points[0].y;
  515. int max_x = curve_points[0].x;
  516. int min_x = curve_points[0].x;
  517. for (auto&pt : curve_points) {
  518. if (pt.y > max_y) {
  519. max_y = pt.y;
  520. }
  521. if (pt.y < min_y) {
  522. min_y = pt.y;
  523. }
  524. if (pt.x > max_x) {
  525. max_x = pt.x;
  526. }
  527. if (pt.x < min_x) {
  528. min_x = pt.x;
  529. }
  530. }
  531. vector<int>stem_width;
  532. int line_min_x, line_max_x;
  533. for (int y = min_y; y <= max_y;++y) {
  534. line_min_x = max_x;
  535. line_max_x = min_x;
  536. for (auto&pt : curve_points) {
  537. if (pt.y != y) { continue; }
  538. if (pt.x < line_min_x) { line_min_x = pt.x; }
  539. if (pt.x > line_max_x) { line_max_x = pt.x; }
  540. }
  541. stem_width.push_back(line_max_x - line_min_x + 1);
  542. }
  543. //计算茎的直径
  544. std::vector<int> hist_width_copy(stem_width);
  545. sort(hist_width_copy.begin(), hist_width_copy.end());
  546. int max_idx = int(0.98*hist_width_copy.size());
  547. int max_stem_width = hist_width_copy.at(max_idx);
  548. // find y range curve
  549. int min_yc = min_y;
  550. int max_yc = max_y;
  551. if (m_stem_type != 0) {//砧木
  552. int pos = trend_detect_pos(stem_width, max_stem_width,15);
  553. if (pos > 0) {
  554. max_yc = min_yc + pos;
  555. }
  556. else {
  557. throw(m_imgId + " not found cut curve range");
  558. }
  559. }
  560. else { //接穗
  561. vector<int> stem_width_r;
  562. vector<int>::reverse_iterator rit = stem_width.rbegin();
  563. for (; rit != stem_width.rend(); ++rit) {
  564. stem_width_r.push_back(*rit);
  565. }
  566. int pos = trend_detect_pos(stem_width_r, max_stem_width, 15);
  567. if (pos > 0) {
  568. min_yc = max_yc - pos;
  569. }
  570. else {
  571. throw(m_imgId + " not found cut curve range");
  572. }
  573. }
  574. // copy cut curve points
  575. vector<cv::Point>cut_curve_points;
  576. for (auto&pt : curve_points) {
  577. if (pt.y >= min_yc && pt.y <= max_yc) {
  578. cut_curve_points.push_back(cv::Point(pt));
  579. }
  580. }
  581. // ecllipse fit
  582. retval = cv::fitEllipse(cut_curve_points);
  583. //if (m_cparam.image_show) {
  584. // //cv::destroyAllWindows();
  585. // cv::Mat tmp = m_grayImg.clone();
  586. // for (auto&pt : cut_curve_points) {
  587. // tmp.at<cv::uint8_t>(pt.y, pt.x+500) = 200;
  588. // }
  589. // //cv::ellipse(tmp, retval, cv::Scalar(200));
  590. // imshow_wait("cut_curve", tmp);
  591. //}
  592. }
  593. string CSolaCutPointReid::get_stem_type_name() {
  594. if (m_stem_type == 0) {
  595. return string("solanaceae scion");
  596. }
  597. else {
  598. return string("solanaceae rootstock");
  599. }
  600. }
  601. void CSolaCutPointReid::img_preprocess(cv::Mat&img)
  602. {
  603. //灰度化
  604. cv::Mat b_img;
  605. if (img.channels() != 1) {
  606. //color image ,bgr, for testing
  607. cvtColor(img, m_grayImg, cv::COLOR_BGR2GRAY);
  608. }
  609. else {
  610. m_grayImg = img.clone();
  611. }
  612. cv::Mat kernel = cv::getStructuringElement(
  613. cv::MORPH_ELLIPSE,
  614. cv::Size(5, 5),
  615. cv::Point(2,2)
  616. );
  617. double th = threshold(m_grayImg, b_img, 255, 255, cv::THRESH_OTSU);
  618. morphologyEx(
  619. b_img,
  620. m_binImg,
  621. cv::MORPH_CLOSE,
  622. kernel,
  623. cv::Point(-1, -1),
  624. 2
  625. );
  626. }
  627. }