/* 砧木切割点后识别 */ #include #include //#include #include #include #include "cut_point_rs_reid.h" #include "utils.h" #include "data_def.h" #include "logger.h" //using namespace cv; namespace graft_cv{ CRootStockCutPointReid::CRootStockCutPointReid(ConfigParam&cp,CGcvLogger*pLog/*=0*/) :m_cparam(cp), m_pLogger(pLog), m_pImginfoBinFork(0), m_pImgCorners(0), m_pImgCutPoint(0), m_imgId(""), m_ppImgSaver(0) { } CRootStockCutPointReid::~CRootStockCutPointReid() { this->clear_imginfo(); } void CRootStockCutPointReid::clear_imginfo(){ if (m_pImginfoBinFork){ imginfo_release(&m_pImginfoBinFork); m_pImginfoBinFork=0; } if (m_pImgCorners){ imginfo_release(&m_pImgCorners); m_pImgCorners=0; } if (m_pImgCutPoint){ imginfo_release(&m_pImgCutPoint); m_pImgCutPoint=0; } } int CRootStockCutPointReid::cut_point_reid( ImgInfo* imginfo, cv::Mat&cimg, const char * pre_img_id, PositionInfo& posinfo, map& img_cache ) { // cimg --- color image, bgr if(m_pLogger){ m_pLogger->INFO(m_imgId +" rootstock cut_pt reid begin"); } if(!pre_img_id){ if(m_pLogger){ m_pLogger->ERRORINFO(m_imgId +" pre-image id is NULL"); } return 1; } string pre_imgid(pre_img_id); map::iterator iter = img_cache.find(pre_imgid); if(iter==img_cache.end()){ if(m_pLogger){ m_pLogger->ERRORINFO(m_imgId +" pre-image NOT in cache"); } return 1; } m_preGrayImg = iter->second; m_imgId = getImgId(img_type::rs_reid); //1 image segment clock_t t; clock_t t0 = clock(); cv::Mat img; if(imginfo){ if(m_pLogger){ stringstream buff; buff<DEBUG(m_imgId+" image set bottom with pixel value 20."); } } if(m_cparam.rs_y_flip){ flip(img,img,0); if(m_pLogger){ m_pLogger->DEBUG(m_imgId+" image y fliped."); } } //image saver if(m_ppImgSaver && *m_ppImgSaver){ (*m_ppImgSaver)->saveImage(img, m_imgId); } if(m_pLogger){ m_pLogger->DEBUG(m_imgId+" before image segment."); } /////////////////////////////////////////////////////// // image segment this->img_preprocess(img); if(m_pLogger){ m_pLogger->DEBUG(m_imgId+" after image gray."); } if(m_cparam.image_show){ cv::destroyAllWindows(); imshow_wait("rs_pre_gray",m_preGrayImg); imshow_wait("rs_gray",m_grayImg); } else{ t = clock(); if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_proc){ if(m_pLogger){ m_pLogger->ERRORINFO(m_imgId+" rootstock reid timeout."); } throw_msg(m_imgId+" time out"); } } if(m_pLogger){ m_pLogger->DEBUG(m_imgId+" after pre- and cur- gray image show."); } //特征提取 //int max_feature = 500; //cv::OrbFeatureDetector fts_detector(max_feature); ////SurfFeatureDetector fts_detector(max_feature); //std::vector keypoints_pre, keypoints_cur; //fts_detector.detect( m_preGrayImg, keypoints_pre ); //fts_detector.detect( m_grayImg, keypoints_cur ); //cv::SurfDescriptorExtractor extractor; //cv::Mat descriptors_pre, descriptors_cur; //extractor.compute( m_preGrayImg, keypoints_pre, descriptors_pre ); //extractor.compute( m_grayImg, keypoints_cur, descriptors_cur ); ////-- Step 3: Matching descriptor vectors with a brute force matcher //cv::BFMatcher matcher(cv::NORM_L2); //std::vector< cv::DMatch > matches; //cv::matcher.match( descriptors_pre, descriptors_cur, matches ); //if(m_cparam.image_show){ // //-- Draw matches // cv::Mat img_matches; // cv::drawMatches( m_preGrayImg, keypoints_pre, m_grayImg, keypoints_cur, matches, img_matches ); // //-- Show detected matches // cv::imshow("Matches", img_matches ); // cv::waitKey(-1); //} posinfo.rs_reid_upoint_x = 10.0; posinfo.rs_reid_upoint_y = 10.0; posinfo.rs_reid_lpoint_x = 20.0; posinfo.rs_reid_lpoint_y = 20.0; //if(m_pLogger){ // stringstream buff; // buff<INFO(buff.str()); //} //// return images: posinfo.pp_images //if(m_cparam.image_return){ // this->clear_imginfo(); // //0) image id // strcpy(posinfo.rs_img_id,m_imgId.c_str()); // //1) // //stem x-range // line(m_binImg,Point(stem_x0,0),Point(stem_x0,m_binImg.cols-1),Scalar(100),2); // line(m_binImg,Point(stem_x1,0),Point(stem_x1,m_binImg.cols-1),Scalar(100),2); // //fork right point // circle(m_binImg, Point(stem_fork_left_x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0); // m_pImginfoBinFork=mat2imginfo(m_binImg); // //3 cut point int gray image // circle(m_grayImg, Point(stem_fork_left_x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0); // 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 // circle(m_grayImg, Point(cut_pt.x,stem_fork_y),5, Scalar(128,0,128), -1, 8,0);//v0.5.9.3 reference point // circle(m_grayImg, Point(cut_pt.x,stem_fork_y),2, Scalar(255,0,255), -1, 8,0); // circle(m_grayImg, Point(lower_cut_pt.x,lower_cut_pt.y),5, Scalar(200,0,200), -1, 8,0); // image_draw_line(m_grayImg,cut_pt.x,cut_pt.y,lower_cut_pt.x,lower_cut_pt.y); // // m_pImgCutPoint = mat2imginfo(m_grayImg); // posinfo.pp_images[0]=m_pImginfoBinFork; // posinfo.pp_images[1]=m_pImgCutPoint; // if(m_ppImgSaver && *m_ppImgSaver){ // (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0"); // (*m_ppImgSaver)->saveImage(m_grayImg, m_imgId+"_rst_1"); // } //} if(m_pLogger){ m_pLogger->INFO(m_imgId +" rootstock cut reid detect finished."); } return 0; }; void CRootStockCutPointReid::img_preprocess(cv::Mat&img) { //灰度化 cv::Mat b_img; if(img.channels()!=1){ //color image ,bgr, for testing cvtColor(img,m_grayImg, cv::COLOR_BGR2GRAY); } else{ m_grayImg = img.clone(); } /*Mat kernel = getStructuringElement( MORPH_ELLIPSE, Size( 2*m_cparam.rs_morph_radius + 1, 2*m_cparam.rs_morph_radius+1 ), Point( m_cparam.rs_morph_radius, m_cparam.rs_morph_radius ) ); double th = threshold(m_grayImg, b_img, 255, 255,THRESH_OTSU); morphologyEx( b_img, m_binImg, MORPH_CLOSE, kernel, Point(-1,-1), m_cparam.rs_morph_iteration );*/ } /////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////// CSolaCutPointReid::CSolaCutPointReid(ConfigParam&cp, int stemType, CGcvLogger*pLog) :m_cparam(cp), m_pLogger(pLog), m_stem_type(stemType), m_imgId(""), m_ppImgSaver(0), m_pImginfoBin(0), m_pImgCutPoint(0) { //m_stem_type -- 0-接穗, 1-砧木 } CSolaCutPointReid::~CSolaCutPointReid() {} void CSolaCutPointReid::clear_imginfo() { if (m_pImginfoBin) { imginfo_release(&m_pImginfoBin); m_pImginfoBin = 0; } if (m_pImgCutPoint) { imginfo_release(&m_pImgCutPoint); m_pImgCutPoint = 0; } } int CSolaCutPointReid::cut_point_reid( ImgInfo* imginfo, cv::Mat&cimg, PositionInfo& posinfo ) { if (m_stem_type == 0) { m_imgId = getImgId(img_type::sola_sc_reid); } else { m_imgId = getImgId(img_type::sola_rs_reid); } //1 image segment clock_t t; clock_t t0 = clock(); cv::Mat img; if (imginfo) { if (m_pLogger) { stringstream buff; buff << m_imgId<<" "<DEBUG(m_imgId + " image set bottom with pixel value 20."); } }*/ if (m_cparam.rs_y_flip) { flip(img, img, 0); if (m_pLogger) { m_pLogger->DEBUG(m_imgId + string(" ") + get_stem_type_name() + " image y fliped."); } } //image saver if (m_ppImgSaver && *m_ppImgSaver) { (*m_ppImgSaver)->saveImage(img, m_imgId); } if (m_pLogger) { m_pLogger->DEBUG(m_imgId + string(" ") + get_stem_type_name() + " before image segment."); } /////////////////////////////////////////////////////// // image segment this->img_preprocess(img); if (m_pLogger) { m_pLogger->DEBUG(m_imgId + " after image gray."); } if (m_cparam.image_show) { cv::destroyAllWindows(); imshow_wait("gray", m_grayImg); imshow_wait("binary", m_binImg); } else { t = clock(); if (1000.0*((float)(t - t0)) / CLOCKS_PER_SEC>(float)m_cparam.timeout_proc) { if (m_pLogger) { m_pLogger->ERRORINFO(m_imgId + " sola cut points reid timeout."); } throw_msg(m_imgId + " time out"); } } if (m_pLogger) { m_pLogger->DEBUG(m_imgId + " after gray image show."); } // find object vector> contours; vector hierarchy; contours.clear(); hierarchy.clear(); int obj_area_th = 100;//m_cparam double max_area = 0; int max_idx = -1; findContours(m_binImg, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); for (int i = 0; i area_th) { throw_msg(m_imgId + " no valid object"); } // 夹爪基面的y int base_y = maxy; // 砧木情况 if (m_stem_type == 0) {// 接穗情况 base_y = miny; } if (m_cparam.image_show) { cv::Mat tmp = m_grayImg.clone(); cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200)); cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200)); cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200)); cv::line(tmp, cv::Point(minx, base_y), cv::Point(maxx, base_y), cv::Scalar(255)); imshow_wait("gray_lines", tmp); } if (m_stem_type == 0) {//接穗,坐标左上角位0点 posinfo.sc_reid_sola_upoint_x = base_y; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿) posinfo.sc_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1 ; posinfo.sc_reid_sola_cpoint_x = minx; posinfo.sc_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1; posinfo.sc_reid_sola_lpoint_x = minx; posinfo.sc_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1; } else {//砧木,坐标左下角位0点 posinfo.rs_reid_sola_upoint_x = m_grayImg.rows - base_y - 1; //夹爪基面的y, 砧木在下方(夹爪的上沿),接穗在上方(夹爪的下沿) posinfo.rs_reid_sola_upoint_y = fabs(cut_miny - base_y) + 1; posinfo.rs_reid_sola_cpoint_x = minx; posinfo.rs_reid_sola_cpoint_y = fabs(cut_midy - base_y) + 1; posinfo.rs_reid_sola_lpoint_x = minx; posinfo.rs_reid_sola_lpoint_y = fabs(cut_maxy - base_y) + 1; } if(m_pLogger){ stringstream buff; buff<INFO(buff.str()); } // return images: posinfo.pp_images if(m_cparam.image_return){ this->clear_imginfo(); //0) image id strcpy(posinfo.rs_img_id,m_imgId.c_str()); //1) bin image m_pImginfoBin=mat2imginfo(m_binImg); //2 cut point int gray image cv::Mat tmp = m_grayImg.clone(); cv::line(tmp, cv::Point(minx, cut_miny), cv::Point(maxx, cut_miny), cv::Scalar(200)); cv::line(tmp, cv::Point(minx, cut_midy), cv::Point(maxx, cut_midy), cv::Scalar(200)); cv::line(tmp, cv::Point(minx, cut_maxy), cv::Point(maxx, cut_maxy), cv::Scalar(200)); m_pImgCutPoint = mat2imginfo(tmp); posinfo.pp_images[0] = m_pImginfoBin; posinfo.pp_images[1] = m_pImgCutPoint; if(m_ppImgSaver && *m_ppImgSaver){ (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0"); (*m_ppImgSaver)->saveImage(tmp, m_imgId+"_rst_1"); } } if (m_pLogger) { if (m_stem_type == 0) { m_pLogger->INFO(m_imgId + " sola scion cut reid detect finished."); } else { m_pLogger->INFO(m_imgId + " sola rootstock cut reid detect finished."); } } return 0; }; void CSolaCutPointReid::find_cut_ys( vector&points, int&minx,int& maxx, int&miny,int&maxy, int&cut_miny, int&cut_midy, int&cut_maxy ) { //找到切口的y值 // 找到边框 minx = miny = 1000; maxx = maxy = -1; for (auto &pt : points) { if (pt.x > maxx) { maxx = pt.x; } if (pt.x < minx) { minx = pt.x; } if (pt.y > maxy) { maxy = pt.y; } if (pt.y < miny) { miny = pt.y; } } // 找到水平的边 std::vector hist(maxy-miny+1,0); std::vector hist_x0(maxy - miny + 1, 1000); std::vector hist_x1(maxy - miny + 1, 0); std::vector hist_width(maxy - miny + 1, 0); for (auto&pt : points) { int idx = pt.y - miny; hist.at(idx) += 1; if (pt.x > hist_x1.at(idx)) { hist_x1.at(idx) = pt.x; } if (pt.x < hist_x0.at(idx)) { hist_x0.at(idx) = pt.x; } } for (int i = 0; i < hist.size(); ++i) { hist_width.at(i) = hist_x1.at(i) - hist_x0.at(i); } //计算茎的直径 std::vector hist_width_copy(hist_width); sort(hist_width_copy.begin(), hist_width_copy.end()); int pert_idx = int(0.95*hist_width_copy.size()); int stem_width = hist_width_copy.at(pert_idx); int max_idx = max_element(hist.begin(), hist.end()) - hist.begin(); if (max_idx hist_width_rev; for (int i = hist_width.size()-1; i >=0 ; i--) { hist_width_rev.push_back(hist_width.at(i)); } //在茎的指导下进行查找 int pos = trend_detect_pos(hist_width_rev, stem_width); cut_miny = maxy - pos; cut_maxy = maxy; cut_midy = int((cut_miny + cut_maxy) / 2); } else { //上锥,找到上锥点 int pos = trend_detect_pos(hist_width, stem_width); cut_miny = miny; cut_maxy = miny + pos; cut_midy = int((cut_miny + cut_maxy) / 2); } } void CSolaCutPointReid::find_cut_curve(vector&curve_points, vector&cut_curve, cv::RotatedRect& retval) { int max_y = curve_points[0].y; int min_y = curve_points[0].y; int max_x = curve_points[0].x; int min_x = curve_points[0].x; for (auto&pt : curve_points) { if (pt.y > max_y) { max_y = pt.y; } if (pt.y < min_y) { min_y = pt.y; } if (pt.x > max_x) { max_x = pt.x; } if (pt.x < min_x) { min_x = pt.x; } } vectorstem_width; int line_min_x, line_max_x; for (int y = min_y; y <= max_y;++y) { line_min_x = max_x; line_max_x = min_x; for (auto&pt : curve_points) { if (pt.y != y) { continue; } if (pt.x < line_min_x) { line_min_x = pt.x; } if (pt.x > line_max_x) { line_max_x = pt.x; } } stem_width.push_back(line_max_x - line_min_x + 1); } //计算茎的直径 std::vector hist_width_copy(stem_width); sort(hist_width_copy.begin(), hist_width_copy.end()); int max_idx = int(0.98*hist_width_copy.size()); int max_stem_width = hist_width_copy.at(max_idx); // find y range curve int min_yc = min_y; int max_yc = max_y; if (m_stem_type != 0) {//砧木 int pos = trend_detect_pos(stem_width, max_stem_width,15); if (pos > 0) { max_yc = min_yc + pos; } else { throw(m_imgId + " not found cut curve range"); } } else { //接穗 vector stem_width_r; vector::reverse_iterator rit = stem_width.rbegin(); for (; rit != stem_width.rend(); ++rit) { stem_width_r.push_back(*rit); } int pos = trend_detect_pos(stem_width_r, max_stem_width, 15); if (pos > 0) { min_yc = max_yc - pos; } else { throw(m_imgId + " not found cut curve range"); } } // copy cut curve points vectorcut_curve_points; for (auto&pt : curve_points) { if (pt.y >= min_yc && pt.y <= max_yc) { cut_curve_points.push_back(cv::Point(pt)); } } // ecllipse fit retval = cv::fitEllipse(cut_curve_points); //if (m_cparam.image_show) { // //cv::destroyAllWindows(); // cv::Mat tmp = m_grayImg.clone(); // for (auto&pt : cut_curve_points) { // tmp.at(pt.y, pt.x+500) = 200; // } // //cv::ellipse(tmp, retval, cv::Scalar(200)); // imshow_wait("cut_curve", tmp); //} } string CSolaCutPointReid::get_stem_type_name() { if (m_stem_type == 0) { return string("solanaceae scion"); } else { return string("solanaceae rootstock"); } } void CSolaCutPointReid::img_preprocess(cv::Mat&img) { //灰度化 cv::Mat b_img; if (img.channels() != 1) { //color image ,bgr, for testing cvtColor(img, m_grayImg, cv::COLOR_BGR2GRAY); } else { m_grayImg = img.clone(); } cv::Mat kernel = cv::getStructuringElement( cv::MORPH_ELLIPSE, cv::Size(5, 5), cv::Point(2,2) ); double th = threshold(m_grayImg, b_img, 255, 255, cv::THRESH_OTSU); morphologyEx( b_img, m_binImg, cv::MORPH_CLOSE, kernel, cv::Point(-1, -1), 2 ); } }