OPENCV dev 3.0.0 "DownhillSimplex Method" OpenCV Error: Assertion failed
I am trying to implement a very simple example using the Downhill Simplex method in OpenCV dev 3.0.0 However I keep getting this error:
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)(s ize.p[0] * size.p[1]) && elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << ((((sizeof(size_t)/4+1)16384|0x3a50) >> ((DataType<_Tp>::typ e) & ((1 << 3) - 1))2) & 3))) in cv::Mat::at, file C:\builds\master_PackSlave-w in32-vc12-shared\opencv\modules\core\include\opencv2/core/mat.inl.hpp, line 893
My code is:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include "test_precomp.h"
using namespace std;
using namespace cv;
void test(Ptr<optim::DownhillSolver> solver, Ptr<optim::Solver::Function> ptr_F, Mat &P, Mat &step)
{
try{
solver->setFunction(ptr_F);
solver->setInitStep(step);
double res = solver->minimize(P);
cout << "res " << res << endl;
}
catch (exception e)
{
cerr << "Error:: " << e.what() << endl;
}
}
int main()
{
class DistanceToLines :public optim::Solver::Function {
public:
double calc(const double* x)const{
return x[0] * x[0] + x[1] * x[1];
}
};
Mat P = (Mat_<double>(1, 2) << 1.0, 1.0);
Mat step = (Mat_<double>(2, 1) << -0.5, 0.5);
Ptr<optim::Solver::Function> ptr_F(new DistanceToLines());
Ptr<optim::DownhillSolver> solver = optim::createDownhillSolver();
test(solver, ptr_F, P, step);
system("pause");
return 0;
}
that's an "index out of range" in Mat::at()
did you run that as a test inside the optim module ? if no, you probably want
"opencv2/optim/optim.hpp" instead of "test_precomp.hpp"
DownhillSolverImpl::innerDownhillSimplex something looks broken here:
Mat_<double> coord_sum(1,ndim,0.0),buf(1,ndim,0.0),y(1,ndim,0.0);
nfunk = 0;
for(i=0;i<ndim+1;++i)
{
y(i) = f->calc(p[i]);
}
y has only ndim elements, while the loop goes over ndim+1
[edit]
y(1,ndim+1,0.0);
and a recompile of the opencv libs result in:would that be ok ? it probably needs more expressive testdata, no idea about that.
Thanks for that, compiling now. Also not sure about the value outputted, need to check with other examples.
who is going to report the issue ?
please, if you find a nicer test, report back here ;)
I have reported the issue. I will test the code on other examples and post back.
+1.
feel like doing the pr, too ? xD
Ok why not. Thanks :)
nice low hanging fruit, also you'll get added to the contibutors ;)