0

my project scope is currency note identification by comparing the sample images feature set.There, i have completed the feature extraction part of the sample images. Further i need to store the sample images features in the text file or XML file and the classification of them. please help me to do the image classification part by using SVM classifier on the OpenCv

this is the feature extraction code that i have completed.

int main( intargc, char** argv ) { /Loading the image as gray scale/

//declaring Mat object.This will holds an image(like iplimage in old opencv versions). 

Mat gray_scale_img; 


//imread is used to load an image. in here i have load the image as a grayscale image.

gray_scale_img=imread("100.jpg",CV_LOAD_IMAGE_GRAYSCALE);  


/*surf detector settings*/

//setting the threshold value.high value will result low number of keypoints.
int hessian=100;

//initializing the surf keypoint detector
SurfFeatureDetectordetector(hessian);


/*detect surf key points*/


//creating vector to store detected keypoints
std::vector<KeyPoint>keypoints;

//detect keypoints
detector.detect(gray_scale_img,keypoints);


/*extract descriptor vectors/feature vectors from each and every keypoints */

SurfDescriptorExtractor extractor;


//this mat object will goinf to hold the extracted descriptors.
Mat descriptors; 

//extracting descriptors/features
extractor.compute(gray_scale_img,keypoints,descriptors);

}

1 Answers1

1

SVM in OpenCV is implemented in CvSVM class;

You need to have feature vector in form of a Matrix (row wise).

Assuming you are using height, width as your feature vector, your mat will be as follows (assuming you have 20 feature vectors):

Mat FV(20,2, CV_32F);
Mat flagmat(20,1,CV_8U);

/*
code to populate the matrix FV. 

Fill the matrix with values so that it will look something as follows:

20 30
30 40
..
..
code to populate the matrix flagmat.
Fill the matrix with labels of each corresponding feature vector in matrix FV. It will look something as follows:
1
-1
1
1
-1
1
1
1
..
*/

CvSVM svm;

svm.train(datamat, flagmat,Mat(),Mat(),CvSVMParams());

Mat testFV(20,2,CV_32F);
Mat sample(1,2,CV_32F);

/* similarly as described above fill testFV matrix*/
float res;// to store result
for(int i =0;i<testFV.rows;i++)
{

    sample.at<float>(0,0)=testFV.at<float>(i,0);
    sample.at<float>(0,1)=testFV.at<float>(i,1);
    float res = svm.predict(sample);
    cout<<"predicted label: "<<res<<endl;
}

I'm assuming you can extract numerical values from the feature descriptors/vectors and put them in the sample matrix in above code. You can replace the feature vectors with any feature descriptor that you are using.

garak
  • 4,713
  • 9
  • 39
  • 56