SVM在openCV中抛出“cv :: Exception at memory location” [英] SVM in openCV throws "cv::Exception at memory location"

查看:1145
本文介绍了SVM在openCV中抛出“cv :: Exception at memory location”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用SVM,openCV,C ++和Visual Studio 2008(mfc app)创建一个简单的OCR应用程序。



我的训练样本是二进制图像机器印刷数字(0-9)。我想使用DAGSVM这个多类问题。所以我需要创建45个SVM,每个SVM是2类(SVM(0,1),SVM(0,2)... SVM(8,9))的SVM。



以下是操作方式:



SVM的参数:

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

类i的训练图像的数据存储在矩阵trainData [i]的28x28图像,这意味着矩阵有784 cols)。
当训练每个SVM时,我创建两个矩阵,称为curTrainData& curTrainLabel。

  for(int i = 0; i <9; i ++)
for +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]&对于(int k = 0; k {
curTrainLabel.at< float>(k,0)= 1.0; //数字类i
for(int l = 0; l <784; l ++)
curTrainData.at< float>(k,l)= trainData [i] .at< float> k,l);
}
for(int k = 0; k {
curTrainLabel.at< float>(k + trainData [i] .rows,0)= - 1.0; //数字类别j
for(int l = 0; l <784; l ++)
curTrainData.at (k + trainData [i] .rows,l)= trainData [j ] .at< float>(k,l);
}

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

我在调用时遇到错误 svms [i] [j] .train ... 。完整的错误是:

  svm.exe中0x75b5d36f处的未处理异常:Microsoft C ++异常:cv ::内存位置异常0x0022af8c .. 

说实话,我不完全理解在openCV中实现的SVM,找到他们使用图像中的对象的任何示例。



我真的很感激,如果有人能告诉我什么是错误:(



更新09/03
我错了。错误来自:

  str.Format (_T(Results\trained_%d_%d.xml),i,j); 
svms [i] [j] .save(CT2A(str));



str是一个CString变量。



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

我创建了文件夹Results和其他文件写入很好(方法fopen(),imwrite()的文件。

解决方案

p>如果使用反斜杠\,则必须改为\\(或者可以使用frontslash/\").


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.

解决方案

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

这篇关于SVM在openCV中抛出“cv :: Exception at memory location”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆