First time here? Check out the FAQ!

Ask Your Question
2

Downhill Solver -- error due to pure virtual functions

asked Mar 30 '17

guidovitale gravatar image

updated Mar 30 '17

I'm familiarizing with Optimization Algorithm in OpenCV. I've found an interesting example to start with:

http://answers.opencv.org/question/33...

so, I developed some code based on that:

#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace cv;
using namespace std;


class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {

        return x[0] * x[0] + x[1] * x[1];
    }
};


int main()
{

    Ptr<DownhillSolver> DHS = DownhillSolver::create();


//    Ptr<MinProblemSolver::Function> ptr_F = makePtr<DistanceToLines>();  // NOT WORKING!!!
    Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());                // NOT WORKING!!!

    return 0;

}

It gives me the following error, when building:

D:\ALGORITMI\minDownhill\main.cpp:50: error: invalid new-expression of abstract class type 'DistanceToLines'
     Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());
D:\ALGORITMI\minDownhill\main.cpp:33: note:   because the following virtual functions are pure within 'DistanceToLines':
 class DistanceToLines :public MinProblemSolver::Function

any hint? I saw that there are not many examples on the web about these methods, so I cannot figure out what is happening.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
3

answered Apr 4 '17

guidovitale gravatar image

thanks berak. I will post my small code as an hint for those, like me, are novice to opencv.

// Qt stuff
    #include "mainwindow.h"
    #include <QApplication>

// OpenCV libraries 
    #include <opencv2/opencv.hpp>
    #include "opencv2/core/utility.hpp"
    #include "opencv2/highgui.hpp"

    #include "opencv2/core/core_c.h"
    #include "opencv2/core/cvdef.h"
    #include "opencv2/core/optim.hpp"

    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>

    using namespace cv;
    using namespace std;




    class DistanceToLines :public MinProblemSolver::Function
    {
    public:
// quadratic functional to minimize (f(x1,x2) = x1^2 + x2^2)
        double calc(const double* x)const
        {
            return x[0] * x[0] + x[1] * x[1];
        }
// nb of degrees of freedom (i.e. how many unknowns)
        int getDims() const
        {
            return 2;
        }
    };



    int main(int argc, char *argv[])
    {

        QApplication a(argc, argv);


        // create a nelder-mead simplex solver
        Ptr<DownhillSolver> DHS = DownhillSolver::create();

        // function to minimize
        Ptr<MinProblemSolver::Function> ptr_F(new DistanceToLines());

        // init guess
        Mat P = (Mat_<double>(1, 2) << 1.0, 1.0);
        DHS->setFunction(ptr_F);

        // init step
        Mat step = (Mat_<double>(2, 1) << -0.5, 0.5);
        DHS->setInitStep(step);

        // solve the preoblem
        double res = DHS->minimize(P);

        // output reidual
        cout << "res " << res << endl;


        return 0;

    }

I'm using opencv with qt on a win10 machine, compiling with mingw.

Preview: (hide)
1

answered Mar 30 '17

berak gravatar image

updated Mar 30 '17

if you look at the MinProblemSolver::Function interface, you'll see, that there are actually two pure virtual functions to implement (yea, doc fail ., also it wasn't in the 3.0 version, your example is refering to),

so, you'll need an implementation for MinProblemSolver::Function::getDims() maybe like this:

class DistanceToLines :public MinProblemSolver::Function
{
public:
    double calc(const double* x)const
    {
        return x[0] * x[0] + x[1] * x[1];
    }
    int getDims() const
    {
         return 2;
    }
};
Preview: (hide)

Comments

1

Thank you, Berak. the code is now working. couldnt post it as a comment since it's too long. btw, I'm using opencv 3.0.0 with qt and mingw as a compiler on a win10 machine

guidovitale gravatar imageguidovitale (Mar 30 '17)edit

added some karma, so, if you want to (and think it's useful), you could write your own answer now.

berak gravatar imageberak (Mar 30 '17)edit

Question Tools

1 follower

Stats

Asked: Mar 30 '17

Seen: 937 times

Last updated: Apr 04 '17