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
This commit is contained in:
parent
f2fd0ad153
commit
af74ec6044
@ -1,5 +1,6 @@
|
|||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
#include "debug.hpp"
|
#include "debug.hpp"
|
||||||
|
#include "opencv2/core/core_c.h"
|
||||||
|
|
||||||
namespace cv{namespace optim{
|
namespace cv{namespace optim{
|
||||||
|
|
||||||
@ -19,15 +20,14 @@ namespace cv{namespace optim{
|
|||||||
TermCriteria _termcrit;
|
TermCriteria _termcrit;
|
||||||
Mat _step;
|
Mat _step;
|
||||||
private:
|
private:
|
||||||
inline void compute_coord_sum(Mat_<double>& points,Mat_<double>& coord_sum);
|
inline void createInitialSimplex(Mat_<double>& simplex,Mat& step);
|
||||||
inline void create_initial_simplex(Mat_<double>& simplex,Mat& step);
|
inline double innerDownhillSimplex(cv::Mat_<double>& p,double MinRange,double MinError,int& nfunk,
|
||||||
inline double inner_downhill_simplex(cv::Mat_<double>& p,double MinRange,double MinError,int& nfunk,
|
|
||||||
const Ptr<Solver::Function>& f,int nmax);
|
const Ptr<Solver::Function>& f,int nmax);
|
||||||
inline double try_new_point(Mat_<double>& p,Mat_<double>& y,Mat_<double>& coord_sum,const Ptr<Solver::Function>& f,int ihi,
|
inline double tryNewPoint(Mat_<double>& p,Mat_<double>& y,Mat_<double>& coord_sum,const Ptr<Solver::Function>& f,int ihi,
|
||||||
double fac,Mat_<double>& ptry);
|
double fac,Mat_<double>& ptry);
|
||||||
};
|
};
|
||||||
|
|
||||||
double DownhillSolverImpl::try_new_point(
|
double DownhillSolverImpl::tryNewPoint(
|
||||||
Mat_<double>& p,
|
Mat_<double>& p,
|
||||||
Mat_<double>& y,
|
Mat_<double>& y,
|
||||||
Mat_<double>& coord_sum,
|
Mat_<double>& coord_sum,
|
||||||
@ -68,7 +68,7 @@ namespace cv{namespace optim{
|
|||||||
form a simplex - each row is an ndim vector.
|
form a simplex - each row is an ndim vector.
|
||||||
On output, nfunk gives the number of function evaluations taken.
|
On output, nfunk gives the number of function evaluations taken.
|
||||||
*/
|
*/
|
||||||
double DownhillSolverImpl::inner_downhill_simplex(
|
double DownhillSolverImpl::innerDownhillSimplex(
|
||||||
cv::Mat_<double>& p,
|
cv::Mat_<double>& p,
|
||||||
double MinRange,
|
double MinRange,
|
||||||
double MinError,
|
double MinError,
|
||||||
@ -92,7 +92,7 @@ namespace cv{namespace optim{
|
|||||||
|
|
||||||
nfunk = ndim+1;
|
nfunk = ndim+1;
|
||||||
|
|
||||||
compute_coord_sum(p,coord_sum);
|
reduce(p,coord_sum,0,CV_REDUCE_SUM);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
@ -146,16 +146,16 @@ namespace cv{namespace optim{
|
|||||||
}
|
}
|
||||||
nfunk += 2;
|
nfunk += 2;
|
||||||
/*Begin a new iteration. First, reflect the worst point about the centroid of others */
|
/*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 (ytry <= y(ilo))
|
||||||
{ /*If that's better than the best point, go twice as far in that direction*/
|
{ /*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))
|
else if (ytry >= y(inhi))
|
||||||
{ /* The new point is worse than the second-highest, but better
|
{ /* The new point is worse than the second-highest, but better
|
||||||
than the worst so do not go so far in that direction */
|
than the worst so do not go so far in that direction */
|
||||||
ysave = y(ihi);
|
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)
|
if (ytry >= ysave)
|
||||||
{ /* Can't seem to improve things. Contract the simplex to good point
|
{ /* Can't seem to improve things. Contract the simplex to good point
|
||||||
in hope to find a simplex landscape. */
|
in hope to find a simplex landscape. */
|
||||||
@ -171,7 +171,7 @@ namespace cv{namespace optim{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nfunk += ndim;
|
nfunk += ndim;
|
||||||
compute_coord_sum(p,coord_sum);
|
reduce(p,coord_sum,0,CV_REDUCE_SUM);
|
||||||
}
|
}
|
||||||
} else --(nfunk); /* correct nfunk */
|
} else --(nfunk); /* correct nfunk */
|
||||||
dprintf(("this is simplex on iteration %d\n",nfunk));
|
dprintf(("this is simplex on iteration %d\n",nfunk));
|
||||||
@ -182,17 +182,7 @@ namespace cv{namespace optim{
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownhillSolverImpl::compute_coord_sum(Mat_<double>& points,Mat_<double>& coord_sum){
|
void DownhillSolverImpl::createInitialSimplex(Mat_<double>& simplex,Mat& step){
|
||||||
for (int j=0;j<points.cols;j++) {
|
|
||||||
double sum=0.0;
|
|
||||||
for (int i=0;i<points.rows;i++){
|
|
||||||
sum += points(i,j);
|
|
||||||
coord_sum(0,j)=sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DownhillSolverImpl::create_initial_simplex(Mat_<double>& simplex,Mat& step){
|
|
||||||
for(int i=1;i<=step.cols;++i)
|
for(int i=1;i<=step.cols;++i)
|
||||||
{
|
{
|
||||||
simplex.row(0).copyTo(simplex.row(i));
|
simplex.row(0).copyTo(simplex.row(i));
|
||||||
@ -229,8 +219,8 @@ namespace cv{namespace optim{
|
|||||||
Mat_<double> simplex=Mat_<double>(ndim+1,ndim,0.0);
|
Mat_<double> simplex=Mat_<double>(ndim+1,ndim,0.0);
|
||||||
|
|
||||||
simplex.row(0).copyTo(proxy_x);
|
simplex.row(0).copyTo(proxy_x);
|
||||||
create_initial_simplex(simplex,_step);
|
createInitialSimplex(simplex,_step);
|
||||||
double res = inner_downhill_simplex(
|
double res = innerDownhillSimplex(
|
||||||
simplex,_termcrit.epsilon, _termcrit.epsilon, count,_Function,_termcrit.maxCount);
|
simplex,_termcrit.epsilon, _termcrit.epsilon, count,_Function,_termcrit.maxCount);
|
||||||
simplex.row(0).copyTo(proxy_x);
|
simplex.row(0).copyTo(proxy_x);
|
||||||
|
|
||||||
@ -267,7 +257,6 @@ namespace cv{namespace optim{
|
|||||||
return Ptr<DownhillSolver>(DS);
|
return Ptr<DownhillSolver>(DS);
|
||||||
}
|
}
|
||||||
void DownhillSolverImpl::getInitStep(OutputArray step)const{
|
void DownhillSolverImpl::getInitStep(OutputArray step)const{
|
||||||
step.create(1,_step.cols,CV_64FC1);
|
|
||||||
_step.copyTo(step);
|
_step.copyTo(step);
|
||||||
}
|
}
|
||||||
void DownhillSolverImpl::setInitStep(InputArray step){
|
void DownhillSolverImpl::setInitStep(InputArray step){
|
||||||
@ -275,15 +264,10 @@ namespace cv{namespace optim{
|
|||||||
Mat m=step.getMat();
|
Mat m=step.getMat();
|
||||||
dprintf(("m.cols=%d\nm.rows=%d\n",m.cols,m.rows));
|
dprintf(("m.cols=%d\nm.rows=%d\n",m.cols,m.rows));
|
||||||
CV_Assert(MIN(m.cols,m.rows)==1 && m.type()==CV_64FC1);
|
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_<double>(1,ndim);
|
|
||||||
}
|
|
||||||
if(m.rows==1){
|
if(m.rows==1){
|
||||||
m.copyTo(_step);
|
m.copyTo(_step);
|
||||||
}else{
|
}else{
|
||||||
Mat step_t=Mat_<double>(ndim,1,(double*)_step.data);
|
transpose(m,_step);
|
||||||
m.copyTo(step_t);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user