Ask Your Question
0

Toy example using opencv 3.0 object in a custom class

asked 2016-04-19 11:54:36 -0600

mcExchange gravatar image

updated 2016-04-19 12:10:29 -0600

I'm new to opencv 3.0. I cannot figure out how to use a SVM object in a custom class. Can someone give me a toy example how to do it?

Here is an example for opencv 2.4.x:

// Class Definition    
class myClass{
    public:
        myClass();

    private:
        CvSVM svm;
};

// Constructor
myClass::myClass()
{
    myClass::svm.load("Some/hard/coded/path/to/a/SVM/File");
}

I guess the class definition should look similar to this:

class myClass {
    private:
    static cv::Ptr<cv::ml::SVM> svm;
};

But my Constructor keeps on failing:

myClass::myClass(){
    myClass::svm->load("Some/hard/coded/path/to/a/SVM/File");
}

with the following error message:

error: no matching function for call to 'cv::ml::SVM::load(const char [120])'
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-04-19 12:14:06 -0600

berak gravatar image

updated 2016-04-19 12:16:21 -0600

for opencv3, try like this:

(Algorithm::load is a static template function nowadays, it returns a new instance)

// Class Definition    
class myClass{
    public:
        myClass();

    int predict(const Mat &query) const;

    private:
        Ptr<ml::SVM> svm;
};

// Constructor
myClass::myClass()
{
    svm = Algorithm::load<ml::SVM>load("Some/hard/coded/path/to/a/SVM/File");
}

int myClass::predict(const Mat &query) const
{
    return (int)svm->predict(query);
}
edit flag offensive delete link more

Comments

1

sidenote: 3.0 has a couple of warts wrt. machine-learning. you probably should update to current master (3.1, atm.)

berak gravatar imageberak ( 2016-04-19 12:19:32 -0600 )edit

I'm not sure entirely whether it's 3.0 or 3.1 I'm dealing with, since I'm doing surgery in the code of somebody else, but it's clearly not 2.4.x.

mcExchange gravatar imagemcExchange ( 2016-04-19 12:24:24 -0600 )edit

cerr << cv::getBuildInformation(); will help you to find out.

berak gravatar imageberak ( 2016-04-19 12:30:16 -0600 )edit

I guess it's kind of fine thanks! I still get some error while compiling, but that might be due to some missing linked library: error: undefined reference to 'cv::ml::SVM::create()'

mcExchange gravatar imagemcExchange ( 2016-04-19 13:24:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-19 11:54:36 -0600

Seen: 732 times

Last updated: Apr 19 '16