Python OpenCV SVM实施 [英] Python OpenCV SVM implementation

查看:147
本文介绍了Python OpenCV SVM实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个矩阵,其中包含我的样本图像(全部转变为矢量),该图像通过PCA/LDA运行,还有一个矢量,表示每个图像所属的类.现在,我想使用OpenCV SVM类来训练我的SVM(我正在使用Python,OpenCV 2.3.1).但是我在定义参数时遇到了问题:

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, ????)

我坚持如何定义SVM(线性等)的类型以及其他内容.在C ++中,您可以通过举例说明来定义它:svm_type = CvSVM :: C_SVC ... Python没有该功能. C ++还有一个特殊的类来存储这些参数-> CvSVMParams.有人可以用Python给我一个例子吗?就像定义SVM类型,伽玛等一样.

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.

2.3.1文档这样说:

The 2.3.1 docs says it like this:

Python: cv2.SVM.train(trainData, responses[, varIdx[, sampleIdx[, params]]]) → retval

什么是varIdx和sampleIdx,以及如何定义参数?

What are varIdx and sampleIdx, and how to define the params?

推荐答案

要使用OpenCV机器学习算法,您必须编写一些包装器类:

To use OpenCV machine learning algorithms, you have to write some wrapper classes:

1.第一堂课

class StatModel(object):
    '''parent class - starting point to add abstraction'''    
    def load(self, fn):
        self.model.load(fn)
    def save(self, fn):
        self.model.save(fn)

2.最后是SvM包装器:

class SVM(StatModel):
    '''wrapper for OpenCV SimpleVectorMachine algorithm'''
    def __init__(self):
        self.model = cv2.SVM()

    def train(self, samples, responses):
        #setting algorithm parameters
        params = dict( kernel_type = cv2.SVM_LINEAR, 
                       svm_type = cv2.SVM_C_SVC,
                       C = 1 )
        self.model.train(samples, responses, params = params)

    def predict(self, samples):
        return np.float32( [self.model.predict(s) for s in samples])

3.用法示例:

import numpy as np
import cv2

samples = np.array(np.random.random((4,2)), dtype = np.float32)
y_train = np.array([1.,0.,0.,1.], dtype = np.float32)

clf = SVM()
clf.train(samples, y_train)
y_val = clf.predict(samples)

设置参数

设置参数很简单-只需编写一个字典即可将参数保存为键.您应该查看原始文档以查看所有可能的参数和允许的值: http://opencv.itseez. com/modules/ml/doc/support_vector_machines.html#cvsvmparams

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

是的,svm_type和kernel_type的可能值在C ++中,但是有一种简单的方法可以将这些常量转换为Python表示形式,例如CvSVM :: C_SVC在Python中写为cv2.SVM_C_SVC.

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.

前奏 要获取更多用于机器学习算法的包装器,请查看磁盘上的opencv示例中的 letter-recog.py 示例或OpenCV存储库的打开url:

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

这篇关于Python OpenCV SVM实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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