From af74ec60447edd134d5aa38baf43d1ce9c01ac67 Mon Sep 17 00:00:00 2001 From: Alex Leontiev Date: Wed, 28 Aug 2013 00:27:59 +0800 Subject: [PATCH] Minor fixes In response to the pull request comments by Vadim Pisarevsky. In particular, the following was done: *)cv::reduce use instead of custom code for calculating per-coordinate sum *) naming style of private methods is made consisted with overall -- mixed-case style *) irrelevant create()'s were removed -- I did not know that copyTo() method itself calls create --- modules/optim/src/simplex.cpp | 46 ++++++++++++----------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/modules/optim/src/simplex.cpp b/modules/optim/src/simplex.cpp index dd49234b51..f45d0ce0b9 100644 --- a/modules/optim/src/simplex.cpp +++ b/modules/optim/src/simplex.cpp @@ -1,5 +1,6 @@ #include "precomp.hpp" #include "debug.hpp" +#include "opencv2/core/core_c.h" namespace cv{namespace optim{ @@ -19,15 +20,14 @@ namespace cv{namespace optim{ TermCriteria _termcrit; Mat _step; private: - inline void compute_coord_sum(Mat_& points,Mat_& coord_sum); - inline void create_initial_simplex(Mat_& simplex,Mat& step); - inline double inner_downhill_simplex(cv::Mat_& p,double MinRange,double MinError,int& nfunk, + inline void createInitialSimplex(Mat_& simplex,Mat& step); + inline double innerDownhillSimplex(cv::Mat_& p,double MinRange,double MinError,int& nfunk, const Ptr& f,int nmax); - inline double try_new_point(Mat_& p,Mat_& y,Mat_& coord_sum,const Ptr& f,int ihi, + inline double tryNewPoint(Mat_& p,Mat_& y,Mat_& coord_sum,const Ptr& f,int ihi, double fac,Mat_& ptry); }; - double DownhillSolverImpl::try_new_point( + double DownhillSolverImpl::tryNewPoint( Mat_& p, Mat_& y, Mat_& coord_sum, @@ -68,7 +68,7 @@ namespace cv{namespace optim{ form a simplex - each row is an ndim vector. On output, nfunk gives the number of function evaluations taken. */ - double DownhillSolverImpl::inner_downhill_simplex( + double DownhillSolverImpl::innerDownhillSimplex( cv::Mat_& p, double MinRange, double MinError, @@ -92,7 +92,7 @@ namespace cv{namespace optim{ nfunk = ndim+1; - compute_coord_sum(p,coord_sum); + reduce(p,coord_sum,0,CV_REDUCE_SUM); for (;;) { @@ -146,16 +146,16 @@ namespace cv{namespace optim{ } nfunk += 2; /*Begin a new iteration. First, reflect the worst point about the centroid of others */ - ytry = try_new_point(p,y,coord_sum,f,ihi,-1.0,buf); + ytry = tryNewPoint(p,y,coord_sum,f,ihi,-1.0,buf); if (ytry <= y(ilo)) { /*If that's better than the best point, go twice as far in that direction*/ - ytry = try_new_point(p,y,coord_sum,f,ihi,2.0,buf); + ytry = tryNewPoint(p,y,coord_sum,f,ihi,2.0,buf); } else if (ytry >= y(inhi)) { /* The new point is worse than the second-highest, but better than the worst so do not go so far in that direction */ ysave = y(ihi); - ytry = try_new_point(p,y,coord_sum,f,ihi,0.5,buf); + ytry = tryNewPoint(p,y,coord_sum,f,ihi,0.5,buf); if (ytry >= ysave) { /* Can't seem to improve things. Contract the simplex to good point in hope to find a simplex landscape. */ @@ -171,7 +171,7 @@ namespace cv{namespace optim{ } } nfunk += ndim; - compute_coord_sum(p,coord_sum); + reduce(p,coord_sum,0,CV_REDUCE_SUM); } } else --(nfunk); /* correct nfunk */ dprintf(("this is simplex on iteration %d\n",nfunk)); @@ -182,17 +182,7 @@ namespace cv{namespace optim{ return res; } - void DownhillSolverImpl::compute_coord_sum(Mat_& points,Mat_& coord_sum){ - for (int j=0;j& simplex,Mat& step){ + void DownhillSolverImpl::createInitialSimplex(Mat_& simplex,Mat& step){ for(int i=1;i<=step.cols;++i) { simplex.row(0).copyTo(simplex.row(i)); @@ -229,8 +219,8 @@ namespace cv{namespace optim{ Mat_ simplex=Mat_(ndim+1,ndim,0.0); simplex.row(0).copyTo(proxy_x); - create_initial_simplex(simplex,_step); - double res = inner_downhill_simplex( + createInitialSimplex(simplex,_step); + double res = innerDownhillSimplex( simplex,_termcrit.epsilon, _termcrit.epsilon, count,_Function,_termcrit.maxCount); simplex.row(0).copyTo(proxy_x); @@ -267,7 +257,6 @@ namespace cv{namespace optim{ return Ptr(DS); } void DownhillSolverImpl::getInitStep(OutputArray step)const{ - step.create(1,_step.cols,CV_64FC1); _step.copyTo(step); } void DownhillSolverImpl::setInitStep(InputArray step){ @@ -275,15 +264,10 @@ namespace cv{namespace optim{ Mat m=step.getMat(); dprintf(("m.cols=%d\nm.rows=%d\n",m.cols,m.rows)); CV_Assert(MIN(m.cols,m.rows)==1 && m.type()==CV_64FC1); - int ndim=MAX(m.cols,m.rows); - if(ndim!=_step.cols){ - _step=Mat_(1,ndim); - } if(m.rows==1){ m.copyTo(_step); }else{ - Mat step_t=Mat_(ndim,1,(double*)_step.data); - m.copyTo(step_t); + transpose(m,_step); } } }}