Ask Your Question

Copying an SVM

asked 2012-07-31 15:46:31 -0500

awknaust gravatar image

I try the following code in C++ using OpenCV 2.42 and it fails when attempting to copy the svm

include <opencv2/opencv.hpp>
using namespace cv;

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

    Mat trainData(100, 3, CV_32F);
    randu(trainData, 0.0f, 10.f);

    Mat classes = Mat::zeros(100, 1, CV_32F);
    for(int i = 50; i < classes.rows; i++)<float>(i) = 1.0f;

    Mat sample = Mat::ones(1, 3, CV_32F);

    SVM svm;
    svm.train_auto(trainData, classes, Mat(), Mat(), SVMParams(), 2);

    SVM svm2 = SVM(svm);

and gives the message (VS2010 Windows XP)

OpenCV Error: Assertion failed (udata < (uchar*)ptr && ((uchar*)ptr - udata) <=
(ptrdiff_t)(sizeof(void*)+16)) in unknown function, file ..\..\..\src\opencv\mod
ules\core\src\alloc.cpp, line 78

Is this a bug, or is there something I am missing about copying an SVM? I expected it to copy the decision function, support vectors, and other parameters, but something seems to be going wrong. I had a look at the source in ml.hpp and it doesn't seem to define a copy constructor.

edit retag flag offensive close merge delete


I also meet the same case:" OpenCV Error: Assertion failed (udata < (uchar)ptr && ((uchar)ptr - udata) <=(ptrdiff_t)(sizeof(void*)+16)) in unknown function." If you fix it,please let me know how.

Jerry Yu gravatar imageJerry Yu ( 2012-09-12 23:31:21 -0500 )edit

I am also apparently having this problem. At first I got the same error, but after initializing the CvSVM outside of the loop where I tried to load and push SVMs into a vector, I only get a debug error saying R6010 - abort() has been called. (Microsoft Visual C++ 2010 Express)

mgrano gravatar imagemgrano ( 2013-12-18 11:48:52 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-31 16:36:55 -0500

updated 2012-08-01 13:23:38 -0500

You are right. I neither find a copy constructor (see modules/ml/include/opencv2/ml/ml.hpp), nor is the operator= is implemented. I'd like to correct my answer and thanks to Michael for pointing out: indeed the implicitly generated copy constructor should work, should have thought of in the first place.

edit flag offensive delete link more

answered 2012-08-01 01:23:48 -0500

Michael Burdinov gravatar image

updated 2012-08-01 07:03:07 -0500

I can't tell why the error is thrown, but it is definitely not because of missing copy constractors. They do exist.

Even if there is no explicitly defined copy constractor or assignment operator, it doesn't mean that they do not exist. Your compiler will generate them for you. If that wasn't the case you couldn't pass your object to function, because function should get a copy of object (if not passed by reference). In fact making uncopyable class is quite a tricky task (you have to explicitly declare copy/assignment to be 'private' functions).

If you want to get better understanding of implicitly generated functions I recommend you to read Scott Meyers book "Effective C++", items 5 and 6 ("Know what functions C++ silently writes and calls" and "Explicitly disallow the use of compiler-generated functions you do not want"). The best book on C++ I ever read.

edit flag offensive delete link more


I understand the concept of a default copy constructor.. but in this case the default copy constructor seems to be failing to copy the object correctly... you can in fact not pass an svm to a function without using a reference. Nor can you add a copy to a vector (which doesn't throw an error until you attempt to actually use the SVM to predict)

awknaust gravatar imageawknaust ( 2012-08-01 10:22:52 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2012-07-31 15:46:31 -0500

Seen: 1,276 times

Last updated: Aug 01 '12