Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.