OPENCV dev 3.0.0 "DownhillSimplex Method" OpenCV Error: Assertion failed

asked 2014-05-20 01:41:27 -0500

updated 2014-05-20 02:33:16 -0500

berak gravatar image

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;
}
edit retag flag offensive close merge delete

Comments

1

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"

berak gravatar imageberak ( 2014-05-20 05:41:56 -0500 )edit
1

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:

res 6.2688e-007

would that be ok ? it probably needs more expressive testdata, no idea about that.

berak gravatar imageberak ( 2014-05-20 05:55:30 -0500 )edit

Thanks for that, compiling now. Also not sure about the value outputted, need to check with other examples.

wintersfury gravatar imagewintersfury ( 2014-05-21 01:07:46 -0500 )edit

who is going to report the issue ?

please, if you find a nicer test, report back here ;)

berak gravatar imageberak ( 2014-05-21 01:20:24 -0500 )edit

I have reported the issue. I will test the code on other examples and post back.

wintersfury gravatar imagewintersfury ( 2014-05-21 06:16:15 -0500 )edit

+1.

feel like doing the pr, too ? xD

berak gravatar imageberak ( 2014-05-21 06:35:43 -0500 )edit

Ok why not. Thanks :)

wintersfury gravatar imagewintersfury ( 2014-05-22 08:07:22 -0500 )edit

nice low hanging fruit, also you'll get added to the contibutors ;)

berak gravatar imageberak ( 2014-05-22 08:16:32 -0500 )edit