SVM in openCV throws “cv::Exception at memory location”

时间秒杀一切 提交于 2019-12-13 03:34:49

问题


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 i = 0; i < 9; i++)
    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
            for (int l = 0; l < 784; l++)
            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);  
svms[i][j].save(CT2A(str));

str is a CString variable.

It remains even if I change to:

svms[i][j].save("Results\trained.xml");

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.


回答1:


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



来源:https://stackoverflow.com/questions/9621110/svm-in-opencv-throws-cvexception-at-memory-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!