SVM in openCV throws "cv :: Exception in memory location"
I am trying to create a simple OCR application with SVM, openCV, C ++ and Visual Studio 2008 (mfc application).
My tutorials are binary images of typescript numbers (0-9). I want to use DAGSVM for this multiple class problem. So I need to create 45 SVMs, each of which is a class 2 SVM (SVM (0,1), SVM (0,2) ... SVM (8,9)).
Here's how things are going:
SVM parameters:
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
The training image data for class i is stored in the matrix sequence Data [i] (each row is a 28x28 image pixel, which means the matrix has 784 columns). When preparing each SVM, I create 2 matrices called curTrainData and 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 an error when calling the SVMs [i] [j] .train ... . Full error:
Unhandled exception at 0x75b5d36f in svm.exe: Microsoft C++ exception: cv::Exception at memory location 0x0022af8c..
To be honest, I don't fully understand the SVMs implemented in openCV and I can't find any examples of them working with objects in images.
I am very grateful if someone can tell me what is (wrong) :(
Update 09/03 : I was wrong. 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.
Remains even if I changed to:
svms[i][j].save("Results\trained.xml");
I created a Results folder and other files are well written in it (files for methods fopen (), imwrite () ...). I don't know why I can't add the folder when it comes to this svm save method.
source to share