So I have a matrix with my sample images (all turned into vectors) which was run trough PCA/LDA, and a vector which denotes the class each images belongs to. Now I want to use the OpenCV SVM class to train my SVM (I am using Python, OpenCV 2.3.1). But I have a problem with defining the parameters:
test = cv2.SVM()
test.train(trainData, responses, ????)
I am stuck on how to define the type of SVM (linear, etc.) and other stuff. In C++ you define it by stating for example: svm_type=CvSVM::C_SVC...Python doesn't have that. C++ also has a special class to store these parameters -> CvSVMParams. Can someone give me an example of this in Python? Like defining the SVM type, gamma, etc.
The 2.3.1 docs says it like this:
Python: cv2.SVM.train(trainData, responses[, varIdx[, sampleIdx[, params]]]) → retval
What are varIdx and sampleIdx, and how to define the params?
Adapted from timgluz version, but uses "train_auto" instead of "train". cv2 will find parameters "C", "gamma", ... for us.
To use OpenCV machine learning algorithms, you have to write some wrapper classes:
1. First parent class
2. Finally SvM wrapper:
3.Example usage:
Setting parameters
Setting parameters is simple - just write a dictionary that holds the parameters as keys. You should look original documentation to see all possible parameters and allowed values: http://opencv.itseez.com/modules/ml/doc/support_vector_machines.html#cvsvmparams
Yes, possible values for svm_type and kernel_type are in C++, but there is easy way to convert those constants into Python representation, for example CvSVM::C_SVC is written as cv2.SVM_C_SVC in Python.
Prelude To get more wrappers for machine learning algorithms, look into letter-recog.py example in your opencv examples on disk or open url of OpenCV repository: https://github.com/Itseez/opencv/tree/master/samples/python2