Hello, I'm working on SVM train model for more than 2 classes. I was storing the result of predict in a mat variable and was getting same output for any sample i test for. I made some changes in the dataset, now im getting a memory exception. Please help me, i'm new to opencv.
My Code :
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<stdio.h>
#include <iostream>
#include<string>
#include <fstream>
#include<stdlib.h>
//#include <stdafx.h>
#include <windows.h>
#include <tchar.h>
#include <sstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
using namespace cv;
using namespace std;
Mat x[100];
int j;
// Dataset is imported
void importData()
{
j=0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
TCHAR *directory = TEXT("C:\\Users\\User\\Documents\\Visual Studio 2010\\Projects\\output");
TCHAR patter[MAX_PATH];
TCHAR fileName[MAX_PATH];
memset(patter, 0x00, MAX_PATH);
_stprintf(patter, TEXT("%s\\*.txt"), directory);
hFind = FindFirstFile(patter, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
}
else
{
do
{
//ignore current and parent directories
if(_tcscmp(FindFileData.cFileName, TEXT("."))==0 || _tcscmp(FindFileData.cFileName, TEXT(".."))==0)
continue;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//ignore directories
}
else
{
//list the Files
_tprintf (TEXT("%s\n"),
FindFileData.cFileName);
memset(fileName, 0x00, sizeof(fileName));
_stprintf(fileName, TEXT("%s\\%s"), directory, FindFileData.cFileName);
// FILE *fptr = fopen(fileName, "r");
//do here whatever you want to do..
//fclose(fptr);
FileStorage fs;
string pa(fileName);
// cout<<pa;
fs.open(pa,FileStorage::READ);
pa=pa.substr(pa.find_last_of("/\\")+1);
pa=pa.substr(0,pa.find_last_of("."));
// cout<<" >> " << pa << "\n" ;
fs["output"+pa] >> x[j] ;
//cout<<pa<<":"<<x[j].rows << "x" << x[j].cols <<"\n";
fs.release();
j++;
}
}while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
}
// Main function which handles all functions
int main(int argc, char** argv[])
{
importData();
int i=0,k=0;
/*//
Mat R = Mat_<double>::zeros(3, 3);
cout<<R;
FileStorage fs;
fs.open("new.txt",FileStorage::WRITE);
fs<<"new" << R;
fs.release();
fs.open("new.txt",FileStorage::READ);
fs["new"]>>x[0];
*/
Mat n[10],z[10];
int l=0;
while(l<5)
{
if(i%10==0)
{
x[i].copyTo(n[l]);
}
else if(i%10==1)
{
x[i].copyTo(z[k]);
}
else if(i%2==0)
{
vconcat(n[l],x[i],n[l]);
// cout<<"row:"<<n[l].rows<<"\n";
}
else
{
vconcat(z[k],x[i],z[k]);
// cout<<"row:"<<n[l].rows<<"\n";
}
i++;
// Increment the class
if(i%10==0)
{
l++;
k++;
}
}
// cout<<n[0].rows << " x " << n[0].cols << "\n";
// cout<<n[1].rows << " x " << n[1].cols;
Mat m[10];
for(int r =0; r<l;r++)
{
Mat A(n[r].rows, 1, DataType<float>::type);
for(int s=0;s<n[r].rows;s++)
{
A.at<float>(s,0)=r+1;
}
A.copyTo(m[r]);
}
Mat trainset,testset;
Mat labelset;
n[0].copyTo(trainset);
z[0].copyTo(testset);
m[0].copyTo(labelset);
//cout<<m[0].at<float>(0,0);
// Classifications
for(int r=1;r<l;r++)
{
//cout<<m[r].at<float>(0,0);
vconcat(trainset,n[r],trainset);
vconcat(testset,z[r],testset);
vconcat(labelset,m[r],labelset);
}
// Testing whether dataset (test data , label data , train data is loaded to the matrix)
FileStorage s;
s.open("pmat.txt",FileStorage::WRITE);
s << "testset" << testset;
s << "trainset" << trainset;
s << "labelset" << labelset;
s.release();
// All goooooood!!
// SVM model
CvSVM SVM;
Mat v1,v2; // To store the results
// Set up SVM's parameters
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::POLY;
params.degree=3;
params.gamma=1;
params.coef0=1;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
// params.C=trainset.rows;
//Train the SVM
SVM.train(trainset, labelset, Mat(), Mat(), params);
SVM.predict(z[0],v1);
system("pause");
return 0;
}