123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- //#include "StdAfx.h"
- #include <opencv2\imgproc\imgproc.hpp>
- #include "optimal_angle.h"
- #include "utils.h"
- #include <string.h>
- namespace graft_cv{
- COptimalAngle::COptimalAngle(ConfigParam&cp, CGcvLogger* pLog/*=0*/)
- :m_cparam(cp),
- m_pLogger(pLog),
- m_pImginfo(0),
- m_pImginfoBase(0),
- m_height(0),
- m_width(0),
- m_patch_id(""),
- m_imgIndex(0),
- m_imgId(""),
- m_ppImgSaver(0)
- {
- }
- COptimalAngle::~COptimalAngle(void)
- {
- this->clear_imginfo();
- }
- void COptimalAngle::clear_imginfo(){
- if (m_pImginfo){
- imginfo_release(&m_pImginfo);
- m_pImginfo=0;
- }
- if (m_pImginfoBase){
- imginfo_release(&m_pImginfoBase);
- m_pImginfoBase=0;
- }
- }
- int COptimalAngle::reset()
- {
- if(m_pLogger){
- m_pLogger->DEBUG("optimal angle reset begin.");
- }
- m_fork_ys.clear();
- m_end_ys.clear();
- m_widths.clear();
- m_an2width.clear();
- m_an2widthBase.clear();
- m_width=0;
- m_height=0;
- m_patch_id="";
- m_imgIndex=0;
- if(m_pLogger){
- m_pLogger->DEBUG("optimal angle reset finished.");
- }
- return 0;
- }
- int COptimalAngle::append(
- ImgInfo* imginfo,
- PositionInfo& posinfo
- )
- {
- //clear opencv windows
- if(m_cparam.image_show){destroyAllWindows();}
- clock_t t;
- clock_t t0 = clock();
- if(m_imgIndex==0){
- //new patch
- m_patch_id = getImgId(img_type::oa);
- }
- m_imgId = getImgIdOa(m_patch_id,m_imgIndex);
- m_imgIndex+=1;
- if(m_pLogger){
- m_pLogger->INFO(m_imgId + " append image begin.");
- }
- if(!isvalid(imginfo)){
- throw_msg(m_imgId+" invalid input image");
- }
- if(m_width==0 || m_height==0){
- m_width = imginfo->width;
- m_height = imginfo->height;
- }
- int angle = imginfo->angle;
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, angle="<<angle
- <<"\twidth="<<imginfo->width
- <<"\theight="<<imginfo->height;
- m_pLogger->INFO(buff.str());
- }
- Mat img = imginfo2mat(imginfo);
-
- //imshow_wait("src", img);
- if(m_cparam.self_camera){
- image_set_bottom(img,20,8);
- if(m_pLogger){
- m_pLogger->DEBUG(m_imgId+" oa_append image set bottom with pixel value 20.");
- }
- }
- if(m_cparam.oa_y_flip){
- flip(img,img,0);
- if(m_pLogger){
- m_pLogger->DEBUG(m_imgId+" oa_append image y fliped.");
- }
- }
- if(m_ppImgSaver && *m_ppImgSaver){
- (*m_ppImgSaver)->saveImage(img, m_imgId);
- if(m_pLogger){
- m_pLogger->DEBUG(m_imgId+" oa_append after image save.");
- }
- }
- if(m_cparam.image_show){
- Mat tmp_b = img.clone();
- 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);
- 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);
- imshow_wait("oa_sub_image_range", tmp_b);
- }
- Rect rect_rs(Point(0,0),Point(img.cols,m_cparam.oa_clip_y_min));
- //Rect rect_base(Point(0,m_cparam.oa_clip_y_max),Point(img.cols,img.rows));
- Mat rs_img = img(rect_rs);
- //Mat bs_img = img(rect_base);
-
- // 1 rootstock image processing
- // 1.1 calculate the image, get binary image and get object width
- int min_idx, max_idx;
- vector<int> hist_col;
- int width = imgproc(rs_img, hist_col, min_idx, max_idx);
- m_an2width.insert(make_pair<int,int>(angle, width));
- posinfo.rs_oa_width=width;
- if(m_cparam.image_show){
- Mat hist_col_img;
- hist2image(hist_col,hist_col_img,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
- imshow_wait("oa_hist_col", hist_col_img);
- }
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, rootstock width="<<width;
- m_pLogger->INFO(buff.str());
- }
- if(!m_cparam.image_show){
- t = clock();
- if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
- throw_msg(m_imgId+" time out");
- }
- }
- //1.2 茎粗计算
- //1.2.1 茎x方向位置:x方向位置范围
- int r0,r1;
- r0=r1=-1;
- std::vector<int>hist_row_leaf;
- mat_histogram(m_binImg,hist_row_leaf,1);
- for(size_t i=0;i<hist_row_leaf.size();++i){
- if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value && r0<0){r0=i;}
- if(hist_row_leaf[i]>=m_cparam.oa_min_hist_value){r1=i;}
- }
- if(r0<0 || r1<0){throw_msg(string(m_imgId+" invalid image, with no object in binary image"));}
-
- vector<int> hist_col_yfork;
- mat_histogram_yfork(m_binImg,hist_col_yfork,r0,r1);
- if(m_cparam.image_show){
- Mat tmp;
- hist2image(hist_col_yfork,tmp,1,m_cparam.image_col_grid,m_cparam.image_row_grid);
- imshow_wait("oa_hist_col_yfork", tmp);
- }
- int x0,x1,stem_x0, stem_x1;
- int centx = (int)((min_idx+ max_idx)/2.0);
- get_stem_x_range_oa(
- hist_col_yfork,
- m_cparam.oa_col_th_ratio,
- m_cparam.oa_stem_x_padding,
- centx,
- width,
- x0,
- x1,
- stem_x0,
- stem_x1);
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, x0="<<x0
- <<"\tx1="<<x1
- <<"\tstem_x0="<<stem_x0
- <<"\tstem_x1="<<stem_x1;
- m_pLogger->INFO(buff.str());
- }
- if(m_cparam.image_show){
- Mat tmp_b = m_binImg.clone();
- cv::line(tmp_b,Point(x0,0), Point(x0,tmp_b.rows),Scalar(200),3);
- cv::line(tmp_b,Point(x1,0), Point(x1,tmp_b.rows),Scalar(200),3);
- imshow_wait("oa_x_range", tmp_b);
- }
- if(!m_cparam.image_show){
- t = clock();
- if(1000.0*((float)(t-t0))/CLOCKS_PER_SEC>(float)m_cparam.timeout_append){
- throw_msg(m_imgId+" time out");
- }
- }
- vector<int> hist_row;
- mat_histogram(m_binImg,hist_row,1,-1,-1,x0,x1+1);
- if(m_cparam.image_show){
- Mat hist_row_img;
- hist2image(hist_row,hist_row_img, 0,m_cparam.image_col_grid,m_cparam.image_row_grid);
- imshow_wait("oa_hist_row", hist_row_img);
- }
- // 1.2.2 茎分叉点检测,y方向检测
- int stem_fork_y=-1,stem_end_y=-1, stem_dia=-1;
- get_stem_y_fork(
- hist_row,
- m_cparam.oa_row_th_ratio,
- m_cparam.oa_stem_dia_min,
- m_cparam.oa_stem_fork_y_min,
- m_cparam.oa_stem_dia_mp,
- stem_fork_y,
- stem_end_y,
- stem_dia);
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, stem_fork_y="<<stem_fork_y
- <<"\tstem_end_y="<<stem_end_y
- <<"\tstem_dia="<<stem_dia;
- m_pLogger->INFO(buff.str());
- }
-
- if(m_cparam.image_show){
- Mat tmp_bin = m_binImg.clone();
- cv::line(tmp_bin,Point(min_idx,0), Point(min_idx,tmp_bin.rows),Scalar(200),3);
- cv::line(tmp_bin,Point(max_idx,0), Point(max_idx,tmp_bin.rows),Scalar(200),3);
- cv::line(tmp_bin,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
- cv::line(tmp_bin,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
- imshow_wait("oa_result", tmp_bin);
- }
-
- m_fork_ys.push_back(stem_fork_y);
- m_end_ys.push_back(stem_end_y);
- m_widths.push_back(width);
- //2 基质图像处理
- /*int min_idx_base, max_idx_base;
- vector<int> hist_col_base;
- int width_base = imgprocBase(bs_img, hist_col_base, min_idx_base, max_idx_base);
- m_an2widthBase.insert(make_pair<int,int>(angle, width_base));
- posinfo.rs_oa_width_base = width_base;
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, base width="<<width_base;
- m_pLogger->INFO(buff.str());
- }*/
- // 3 返回结果
- double fork_y = (double)stem_fork_y;
- double end_y = (double)stem_end_y;
- fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
- end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
- posinfo.rs_oa_stem_y_fork=fork_y;
- posinfo.rs_oa_clamp_y_end=end_y;
- if(m_pLogger){
- stringstream buff;
- buff<<m_imgId<<" append image, stem_fork_y(mm)="<<fork_y
- <<"\tstem_end_y(mm)="<<end_y;
- m_pLogger->INFO(buff.str());
- }
- //4 返回图像
- if(m_cparam.image_return){
- this->clear_imginfo();
- cv::line(m_binImg,Point(min_idx,0), Point(min_idx,rs_img.rows),Scalar(200),3);
- cv::line(m_binImg,Point(max_idx,0), Point(max_idx,rs_img.rows),Scalar(200),3);
- cv::line(m_binImg,Point(x0,stem_fork_y), Point(x1,stem_fork_y),Scalar(200),3);
- cv::line(m_binImg,Point(x0,stem_end_y), Point(x1,stem_end_y),Scalar(200),3);
- clear_imginfo();
- m_pImginfo = mat2imginfo(m_binImg);
- //m_pImginfoBase = mat2imginfo(m_binImgBase);
- posinfo.pp_images[0]=m_pImginfo;
- //posinfo.pp_images[1]=m_pImginfoBase;
- if(m_ppImgSaver && *m_ppImgSaver){
- (*m_ppImgSaver)->saveImage(m_binImg, m_imgId+"_rst_0");
- }
- }
- if(m_pLogger){
- m_pLogger->INFO(m_imgId + " append image finished.");
- }
- return 0;
- }
- double COptimalAngle::infer(PositionInfo& posinfo){
-
- if(m_pLogger){
- m_pLogger->INFO(m_patch_id + " optimal angle infer begin.");
- }
- double oa = this->angle_fit(this->m_an2width);
- /*double oa_base = 0.0;
- try{
- oa_base = this->angle_fit_base(this->m_an2widthBase);
- }
- catch(...){
- if(m_pLogger){
- m_pLogger->WARNING("angle_fit_base() error");
- }
- }*/
- posinfo.rs_oa = oa;
- //posinfo.rs_oa_base=oa_base;
- if(m_fork_ys.size()==0 ||m_end_ys.size()==0){
- throw_msg(m_patch_id+ " empty fork_ys or end_ys, NEED append image");
- }
- vector<size_t>des_idx = sort_indexes_e(m_widths,false);
- int e_idx = (int)((float)m_end_ys.size() * 0.5);
- sort(m_end_ys.begin(), m_end_ys.end());
- double fork_y = ((m_fork_ys[des_idx[0]] + m_fork_ys[des_idx[1]])/2.0);
- double end_y = m_end_ys[e_idx];
- if(m_pLogger){
- stringstream buff;
- buff<<m_patch_id<<" angle fit result, stem_fork_y(pixel)="<<(int)(fork_y)
- <<"\tstem_end_y(pixel)="<<(int)(end_y);
- m_pLogger->INFO(buff.str());
- }
- fork_y = ((double)m_height/2.0 - fork_y)*m_cparam.rs_oa_pixel_ratio;
- end_y = ((double)m_height/2.0 - end_y)*m_cparam.rs_oa_pixel_ratio;
- posinfo.rs_oa_stem_y_fork=fork_y;
- posinfo.rs_oa_clamp_y_end=end_y;
- if(m_pLogger){
- stringstream buff;
- buff<<m_patch_id<<" angle fit result, stem_fork_y(mm)="<<fork_y
- <<"\tstem_end_y(mm)="<<end_y;
- m_pLogger->INFO(buff.str());
-
- m_pLogger->INFO(m_patch_id + " optimal angle infer finished.");
- }
- return oa;
- }
- int COptimalAngle::imgproc(
- Mat& img,
- vector<int>& hist,
- int& min_idx,
- int& max_idx
- )
- {
- //int morph_size = 1;
- //int min_hist_value = 10;
- hist.clear();
- Mat b_img;
- double th = threshold(
- img,
- b_img,
- 255,
- 255,
- THRESH_OTSU);//+THRESH_BINARY_INV
- Mat kernel = getStructuringElement(
- MORPH_RECT,
- Size( 2*m_cparam.oa_morph_radius+1, 2*m_cparam.oa_morph_radius+1),
- Point( m_cparam.oa_morph_radius, m_cparam.oa_morph_radius)
- );
-
- morphologyEx(
- b_img,
- m_binImg,
- MORPH_CLOSE,
- kernel,
- Point(-1,-1),
- m_cparam.oa_morph_iteration);
-
-
- mat_histogram(m_binImg, hist, 0);
- /*int min_idx, max_idx;*/
- min_idx = hist.size();
- max_idx = 0;
- vector<int>::const_iterator it0 = hist.begin();
- for(vector<int>::const_iterator it = hist.begin();it!=hist.end();++it){
- if(*it>=m_cparam.oa_min_hist_value){
- int idx = it - it0;
- if(idx<min_idx){min_idx = idx;}
- if(idx > max_idx) {max_idx = idx;}
- }
- }
- if(max_idx<=min_idx){
- throw_msg(m_imgId+" invalid binary image, not exist valid objects");
- }
- if(m_cparam.image_show){
- Mat tmp = m_binImg.clone();
- cv::line(tmp,Point(min_idx,0), Point(min_idx,tmp.rows),Scalar(156),3);
- cv::line(tmp,Point(max_idx,0), Point(max_idx,tmp.rows),Scalar(156),3);
- imshow_wait("oa_bin", tmp);
- }
- int width = max_idx-min_idx+1;
- return width;
- }
- int COptimalAngle::imgprocBase(
- Mat& img,
- vector<int>& hist,
- int& min_idx,
- int& max_idx
- )
- {
- //int morph_size = 1;
- //int min_hist_value = 10;
- hist.clear();
- Mat b_img;
- double th = threshold(
- img,
- b_img,
- 255,
- 255,
- THRESH_OTSU+THRESH_BINARY_INV);
- Mat kernel = getStructuringElement(
- MORPH_RECT,
- Size( 2*m_cparam.oa_morph_radius_base+1, 2*m_cparam.oa_morph_radius_base+1),
- Point( m_cparam.oa_morph_radius_base, m_cparam.oa_morph_radius_base)
- );
-
- morphologyEx(
- b_img,
- m_binImgBase,
- MORPH_OPEN,
- kernel,
- Point(-1,-1),
- m_cparam.oa_morph_iteration_base);
- if(m_cparam.image_show){
- destroyAllWindows();
- imshow_wait("oa_bin_base", m_binImgBase);
- }
-
- mat_histogram(m_binImgBase, hist, 0);
- /*int min_idx, max_idx;*/
- min_idx = hist.size();
- max_idx = 0;
- vector<int>::const_iterator it0 = hist.begin();
- for(vector<int>::const_iterator it = hist.begin()+5;it!=hist.end();++it){
- if(*it>=m_cparam.oa_min_hist_value_base){
- int idx = it - it0;
- if(idx<min_idx){min_idx = idx;}
- if(idx > max_idx) {max_idx = idx;}
- }
- }
- if(max_idx<=min_idx){
- throw_msg(m_imgId+" invalid binary image, not exist valid objects");
- }
- int width = max_idx-min_idx+1;
- return width;
- }
- //angle_fit()方法
- //append()加载的图片,满足下述条件:0-180度采样,其中0和180度都有数值
- double COptimalAngle::angle_fit(map<int,int>& an2width,bool is_base/*=false*/)
- {
- if(an2width.size()<3){
- throw_msg(m_patch_id+" not enough valid images, NEED append image");
- }
- double oa = 0.0;
- std::vector<double> x;//angle
- std::vector<double> y;//width
- map<int,int>::iterator it = an2width.begin();
- for(it=an2width.begin(); it!=an2width.end(); ++it)
- {
- x.push_back((double)(it->first));
- y.push_back((double)(it->second));
- }
- //mean value of first and latest y
- double y_mu = 0.5*(y[0]+ y[y.size()-1]);
- y[0] = y[y.size()-1] = y_mu;
- size_t ap_times = x.size()-1;
- for (size_t i=0; i<ap_times;++i)
- {
- x.push_back(x[i+1]+180.0);
- y.push_back(y[i+1]);
- }
- vector<double>::iterator smallest = min_element(begin(y), end(y));
- size_t min_idx, max_idx;
- min_idx = distance(y.begin(), smallest);
- for(size_t i = min_idx+1;i<y.size();++i){
- if(fabs(y[i]-*smallest)<0.1){
- max_idx = i;
- break;
- }
- }
- // slice x and y the convex segment
- vector<double>xx;
- vector<double>yy;
- for(size_t i=min_idx;i<=max_idx;++i){
- xx.push_back(x[i]);
- yy.push_back(y[i]);
- }
- // fit quadratic function with opencv
- Mat A = cv::Mat::zeros(cv::Size(3,xx.size()), CV_64FC1);
- for(int i=0; i<xx.size();++i){
- A.at<double>(i,0) = 1;
- A.at<double>(i,1) = xx[i];
- A.at<double>(i,2) = xx[i] * xx[i];
- }
- Mat b = cv::Mat::zeros(cv::Size(1,yy.size()), CV_64FC1);
- for(int i = 0; i<yy.size(); ++i){
- b.at<double>(i,0) = yy[i];
- }
- Mat c, d;
- c = A.t() * A;
- d = A.t() * b;
- Mat result = cv::Mat::zeros(cv::Size(1,3),CV_64FC1);
- cv::solve(c,d,result);
-
- double a0 = result.at<double>(0, 0);
- double a1 = result.at<double>(1, 0);
- double a2 = result.at<double>(2, 0);
- if(fabs(a2)<1.0e-6){
- throw_msg(m_patch_id+" a2 = 0 in solver");
- }
- oa = -a1 / a2 / 2;
- if(oa >180.0){oa -= 180.0;}
- return oa;
- }
- double COptimalAngle::angle_fit_base(map<int, int>&){
- return 0.0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- COptimalAnglePart::COptimalAnglePart(ConfigParam&cp, CGcvLogger* pLog/*=0*/)
- :COptimalAngle(cp,pLog)
- {
- }
- COptimalAnglePart::~COptimalAnglePart()
- {
- COptimalAngle::clear_imginfo();
- }
- // append()加载的图片,满足不是0-180度采样,但保证部分采样中一定存在最大值,
- // 直接利用这些数值进行二次函数的拟合
- double COptimalAnglePart::angle_fit(map<int,int>& an2width,bool is_base/*=false*/)
- {
-
- double oa = 0.0;
- std::vector<double> xx;//angle
- std::vector<double> yy;//width
- map<int,int>::iterator it = an2width.begin();
- for(it=an2width.begin(); it!=an2width.end(); ++it)
- {
- xx.push_back((double)(it->first));
- yy.push_back((double)(it->second));
- }
- //sort
- for(size_t i =0;i<xx.size()-1;++i){
- for(size_t j = i+1; j<xx.size();++j){
- if(xx[j]<xx[i]){
- double tmp = xx[i];
- xx[i]=xx[j];
- xx[j]=tmp;
- tmp = yy[i];
- yy[i]=yy[j];
- yy[j]=tmp;
- }
- }
- }
- if(m_pLogger){
- stringstream buff;
- buff<<m_patch_id;
- if(is_base){
- buff<<" rootstock_base ";
- }
- else{
- buff<<" rootstock_leaf ";
- }
- buff<<"opt-angle, (xi,yi): ";
- for(size_t i=0;i<xx.size();++i){
- buff<<"("<<xx[i]<<","<<yy[i]<<"), ";
- }
- m_pLogger->INFO(buff.str());
- }
- if(an2width.size()<3){
- throw_msg(m_patch_id+" not enough valid images, NEED append image");
- }
- // angle field check
- if((xx.back()-xx.front())<90){
- if(m_pLogger){
- stringstream buff;
- buff<<m_patch_id<<" x_end="<<xx.back()<<" x_front="<<xx.front();
- buff<<" not enough angle field, small than 90 degree.";
- m_pLogger->ERRORINFO(buff.str());
- }
- throw_msg(m_patch_id+" not enough angle field, small than 90 degree");
- }
- oa = opt_angle_part_impl(xx,yy,is_base);
- if(m_pLogger){
- stringstream buff;
- buff<<m_patch_id;
- if(is_base){
- buff<<" rootstock_base ";
- }
- else{
- buff<<" rootstock_leaf ";
- }
- buff<<" opt-angle: "<<oa;
- m_pLogger->INFO(buff.str());
- }
- return oa;
- }
- double COptimalAnglePart::angle_fit_base(map<int, int>& an2width){
-
- double oa = 0.0;
- //find the maximun angle
- oa = angle_fit(an2width,true);
- //get the minimum angle
- if(oa>45.0){oa -= 45.0;}
- else{oa +=45.0;}
- return oa;
- }
- void COptimalAnglePart::zero_cross_detect(
- vector<int>&d_sign, //input
- bool& is_local_max, // whether exists local max
- int& local_limit_idx // local limit index of original vector
- )
- {
- int min_zc_idx = -1;
- int max_zc_idx = -1;
- bool b_local_min =false;
- bool b_local_max =false;
- bool is_all_down=true;
- bool is_all_up = true;
- for (size_t i=0; i<d_sign.size();++i){
- if (d_sign[i] >0){is_all_down=false;}
- if (d_sign[i] <0){is_all_up=false;}
- if (i == d_sign.size()-1){continue;}
- if (d_sign[i] >=0 && d_sign[i+1] <=0){
- //is_local_max=true;
- max_zc_idx = i+1;
- b_local_max=true;
-
- }
- if (d_sign[i] <=0 && d_sign[i+1] >=0){
- //is_local_max=false;
- min_zc_idx=i+1;
- b_local_min=true;
- }
- }
- if(b_local_min){
- is_local_max=false;
- local_limit_idx = min_zc_idx;
- return;
- }
- if(b_local_max){
- is_local_max=true;
- local_limit_idx = max_zc_idx;
- return;
- }
- if (is_all_down){
- is_local_max=true;
- local_limit_idx=0;
- return;
- }
- if( is_all_up){
- is_local_max=true;
- local_limit_idx=d_sign.size();
- return;
- }
- is_local_max=true;
- local_limit_idx=-1;
- return;
- }
- double COptimalAnglePart::limit_min_angle(
- vector<double>&x, //sorted, ascending
- vector<double>&y,
- size_t min_idx
- )
- {
- size_t i = min_idx;
- if(fabs(x[i+1]-x[i])<1.0e-6 || fabs(x[i]-x[i-1])<1.0e-6){
- throw_msg(m_patch_id+" limit_min_angle, diff_x < 1.0e-6");
- }
- double k_r = (y[i+1]-y[i])/(x[i+1]-x[i]);
- double k_l = (y[i]-y[i-1])/(x[i]-x[i-1]);
- double oa = 0.0;
- double b_r = 0.0;
- double b_l = 0.0;
- double cross_x = 0;
- if (fabs(k_r) > fabs(k_l)){
- // right line
- b_r = y[i]-k_r*x[i];
- b_l = y[i-1] + k_r*x[i-1];
- cross_x = line_cross(k_r,b_r,-k_r,b_l);
- oa = cross_x;
- }
- else{
- //# left line
- b_l = y[i]-k_l*x[i];
- b_r = y[i+1] + k_l*x[i+1];
- cross_x = line_cross(k_l,b_l,-k_l,b_r);
- oa = cross_x;
- }
- return oa;
- }
- double COptimalAnglePart::line_cross(double k1,double b1,double k2,double b2)
- {
- double x = (b2-b1) / (k1-k2);
- return x;
- }
- double COptimalAnglePart::opt_angle_part_impl(
- vector<double>&x, //sorted, ascending
- vector<double>&y,
- bool is_base/*=false*/)
- {
- // sign of difference of y
- vector<int> dy_sign;
- for(size_t i=1; i<y.size();++i){
- double dd = y[i] - y[i-1];
- if(dd>0){
- dy_sign.push_back(1);
- }
- else{
- if(dd<0){dy_sign.push_back(-1);}
- else{dy_sign.push_back(0);}
- }
- }
- //local limit search
- bool is_local_max = false;
- int local_limit_idx = -1;
- zero_cross_detect(dy_sign,is_local_max,local_limit_idx);
- if(local_limit_idx<0){
- throw_msg(m_patch_id+" not found local limit");
- }
- // optimal angle calculation
- vector<double> xx;
- vector<double> yy;
- double oa=0.0;
- if(is_local_max){
- //存在局部最大
- if( local_limit_idx == 0 || local_limit_idx==y.size()-1){
- //#单调
- oa = x[local_limit_idx];
- /*if (local_limit_idx == 0){
- for(size_t i=0;i<3;++i){
- xx.push_back(x[i]);
- yy.push_back(y[i]);
- }
- }
- else {
- for(size_t i=y.size()-3;i<y.size();++i){
- xx.push_back(x[i]);
- yy.push_back(y[i]);
- }
- }
- oa = qua_fitting(xx,yy); */
- if(m_pLogger){
- m_pLogger->INFO(m_patch_id+ " angle fit -- signle --- max_angle");
- }
-
- }
- else{
- // 极大值
- for(size_t i=local_limit_idx-1;i<local_limit_idx+2;++i){
- xx.push_back(x[i]);
- yy.push_back(-y[i]);
- }
- oa = limit_min_angle(xx,yy,1);
- if(m_pLogger){
- m_pLogger->INFO(m_patch_id+" angle fit -- local max --- 3p insert");
- }
- }
- }
- else{
- // 极小值
- oa = limit_min_angle(x,y,local_limit_idx);
- if(is_base){
- //for base, offset 45 degree
- if(oa >45){oa = oa-45.0;}
- else{oa = oa+45.0;}
- }
- else{
- // for rootstock, offset 90 degree
- if(oa >90){oa = oa-90.0;}
- else{oa = oa+90.0;}
- }
- if(m_pLogger){
- m_pLogger->INFO(m_patch_id+" angle fit -- local min --- 3p insert");
- }
- }
- return oa;
- }
- };
|