Take the 2-minute tour ×

I'm trying to create a simple OCR application with SVM, openCV, C++ and Visual Studio 2008 (mfc app).

My training samples are binary images of machine-printed digits (0-9). I want to use DAGSVM for this multi-class problem. So I need to create 45 SVMs, each of which is the SVM of 2 class (SVM(0,1), SVM(0,2)... SVM(8,9)).

Here's how things are going:

SVM's parameters:

CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

Data of training images of class i are stored in matrix trainData[i] (each row is the pixels of a 28x28 image, which means the matrix has 784 cols). When training each SVM, I create 2 matrix called curTrainData & curTrainLabel.

    for (int j = i+1; j < 10; j++)
        curTrainData.create(trainData[i].rows + trainData[j].rows, 784, CV_32FC1);
        curTrainLabel.create(curTrainData.rows, 1, CV_32FC1);

        // merge 2 matrix: trainData[i] & trainData[j]
        for (int k = 0; k < trainData[i].rows; k++)
            curTrainLabel.at<float>(k, 0) = 1.0; // class of digit i
            for (int l = 0; l < 784; l++)
                curTrainData.at<float>(k,l) = trainData[i].at<float>(k,l);
        for (int k = 0; k < trainData[j].rows; k++)
            curTrainLabel.at<float>(k + trainData[i].rows, 0) = -1.0; // class of digit j
            curTrainData.at<float>(k + trainData[i].rows,l) = trainData[j].at<float>(k,l);

        svms[i][j].train(curTrainData, curTrainLabel, Mat(), Mat(), params);

I got error at the call svms[i][j].train.... The full error is:

Unhandled exception at 0x75b5d36f in svm.exe: Microsoft C++ exception: cv::Exception at memory location 0x0022af8c..

To tell the truth I don't fully understand SVM implemented in openCV and I can't find any example of them working with objects in images.

I'm really grateful if someone can tell me what is (are) wrong :(

Update 09/03: I had mistaken. The error comes from:

str.Format(_T("Results\trained_%d_%d.xml"), i, j);  

str is a CString variable.

It remains even if I change to:


I've created the folder Results and others files are written well into it (files for methods fopen(), imwrite()...). I don't know why I can't add the folder when it comes to this save method of svm.

share|improve this question
just a guess, but this only looks like a memory issue, where is svms declared and what is its size ? (must be at least 9x10 following your loop statement) –  Smash Mar 8 '12 at 17:40
@Smash: Yes, I use a 10x10 array. It turns out the problem is in the call svm.save() (like my update above). If I can save the model, I'll have to use only one svm each time. –  Risa Mar 8 '12 at 18:30
if you use backslash \, you have to put \\ instead (or you can use frontslash /) –  Smash Mar 8 '12 at 18:36
add comment

1 Answer

up vote 1 down vote accepted

If you use backslash "\", you have to put "\\" instead (or you can use a frontslash "/").

share|improve this answer
Thank you very much. It's silly of me to ignoring the IDE's warnings. –  Risa Mar 21 '12 at 17:30
add comment

Your Answer


