123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- /*
- 砧木切割点后识别
- */
- #include <opencv2\imgproc\imgproc.hpp>
- #include <opencv2\features2d\features2d.hpp>
- //#include <opencv2\nonfree\features2d.hpp>
- #include <math.h>
- #include <algorithm>
- #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<string, cv::Mat>& 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<string, cv::Mat>::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<<m_imgId<<" rootstock image, width="<<imginfo->width
- <<"\theight="<<imginfo->height;
- m_pLogger->INFO(buff.str());
- }
- if(!isvalid(imginfo)){
- if(m_pLogger){
- m_pLogger->ERRORINFO(m_imgId+" rootstock input image invalid.");
- }
- throw_msg(m_imgId+" invalid input image");
-
- }
- img = imginfo2mat(imginfo);
- }
- else{
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<"rootstock image, width="<<cimg.cols
- <<"\theight="<<cimg.rows;
- m_pLogger->INFO(buff.str());
- }
- if(cimg.empty()){
- if(m_pLogger){
- m_pLogger->ERRORINFO(m_imgId+" rootstock input image invalid.");
- }
- throw_msg(m_imgId +" invalid input image");
-
- }
- img = cimg;
- }
-
- if(m_cparam.self_camera){
- image_set_bottom(img,20,8);
- if(m_pLogger){
- m_pLogger->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<cv::KeyPoint> 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<<m_imgId<<" rootstock image, rs_cut_upoint(mm)("<<rs_cut_upoint_x
- // <<","<<rs_cut_upoint_y<<")"
- // <<", rs_stem_diameter(mm)="<<rs_stem_diameter
- // <<", lower_cut_pt(mm)("<<rs_cut_lpoint_x
- // <<","<<rs_cut_lpoint_y<<")";
- // m_pLogger->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<<" "<<get_stem_type_name() << " image, width=" << imginfo->width
- << "\theight=" << imginfo->height;
- m_pLogger->INFO(buff.str());
- }
- if (!isvalid(imginfo)) {
- if (m_pLogger) {
- m_pLogger->ERRORINFO(m_imgId +string(" ")+get_stem_type_name() + " input image invalid.");
- }
- throw_msg(m_imgId + string(" ") + get_stem_type_name() + " invalid input image");
- }
- img = imginfo2mat(imginfo);
- }
- else {
- if (m_pLogger) {
- stringstream buff;
- buff << m_imgId << " " << get_stem_type_name() << " image, width=" << cimg.cols
- << "\theight=" << cimg.rows;
- m_pLogger->INFO(buff.str());
- }
- if (cimg.empty()) {
- if (m_pLogger) {
- m_pLogger->ERRORINFO(m_imgId +string(" ")+ get_stem_type_name() + " input image invalid.");
- }
- throw_msg(m_imgId + string(" ") +get_stem_type_name() + " invalid input image");
- }
- img = cimg;
- }
- /*if (m_cparam.self_camera) {
- image_set_bottom(img, 20, 8);
- if (m_pLogger) {
- m_pLogger->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<vector<cv::Point>> contours;
- vector<cv::Vec4i> 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<contours.size(); ++i) {
- double area = contourArea(contours[i]);
- if (area < obj_area_th) { continue; }
- if(area<max_area) { continue; }
- max_area = area;
- max_idx = i;
- }
- if (max_idx < 0 || contours[max_idx].size()==0) {
- throw_msg(m_imgId + " no valid object");
- }
- // 找到切口处y值
- int minx, maxx, miny, maxy;
- int cut_miny, cut_midy, cut_maxy;
- find_cut_ys(contours[max_idx],
- minx, maxx, miny, maxy,
- cut_miny, cut_midy, cut_maxy);
- // 夹爪基面的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<<m_imgId<<" sola rootstock(scion) image, rs_cut_upoint("<< posinfo.rs_reid_sola_upoint_x
- <<","<< posinfo.rs_reid_sola_upoint_y <<")"
- <<", rs_cut_cpoint=("<< posinfo.rs_reid_sola_cpoint_x
- << "," << posinfo.rs_reid_sola_cpoint_y << ")"
- <<", rs_cut_lpoint(mm)("<< posinfo.rs_reid_sola_lpoint_x
- <<","<< posinfo.rs_reid_sola_lpoint_y <<")";
- m_pLogger->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<cv::Point>&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<int> hist(maxy-miny+1,0);
- std::vector<int> hist_x0(maxy - miny + 1, 1000);
- std::vector<int> hist_x1(maxy - miny + 1, 0);
- std::vector<int> 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<int> hist_width_copy(hist_width);
- sort(hist_width_copy.begin(), hist_width_copy.end());
- int pert_idx = int(0.98*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<int(hist.size() / 2.0)) {
- //下锥,
- std::vector<int> 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<cv::Point>&curve_points,
- vector<cv::Point>&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;
- }
- }
- vector<int>stem_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<int> 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<int> stem_width_r;
- vector<int>::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
- vector<cv::Point>cut_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<cv::uint8_t>(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
- );
- }
- }
|