参考文献:
http://blog.youkuaiyun.com/homechao/article/details/9056827
http://hi.baidu.com/lin65505578/item/20798e9748d936b7cd80e5dd
例程一:
//openCV中贝叶斯分类器的API函数用法举例
//运行环境:winXP + VS2008 + openCV2.1.0
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include <iostream>
using namespace std;
int main()
{
float inputArr[120] =
{
0.708333f,1.0f,1.0f,-0.320755f,-0.105023f,-1.0f,1.0f,-0.419847f,-1.0f,-0.225806f,0.f,1.f,
0.583333f,-1.f,0.333333f,-0.603774f,1.f,-1.f,1.f,0.358779f,-1.f,-0.483871f,0.f,-1.f,
0.166667f,1.f,-0.333333f,-0.433962f,-0.383562f,-1.f,-1.f,0.0687023f,-1.f,-0.903226f,-1.f,-1.f,
0.458333f,1.f,1.f,-0.358491f,-0.374429f,-1.f,-1.f,-0.480916f,1.f,-0.935484f,0.f,-0.333333f,
0.875f,-1.f,-0.333333f,-0.509434f,-0.347032f,-1.f,1.f,-0.236641f,1.f,-0.935484f,-1.f,-0.333333f,
0.5f,1.f,1.f,-0.509434f,-0.767123f,-1.f,-1.f,0.0534351f,-1.f,-0.870968f,-1.f,-1.f,
0.125f,1.f,0.333333f,-0.320755f,-0.406393f,1.f,1.f,0.0839695f,1.f,-0.806452f,0.f,-0.333333f,
0.25f,1.f,1.f,-0.698113f,-0.484018f,-1.f,1.f,0.0839695f,1.f,-0.612903f,0.f,-0.333333f,
0.291667f,1.f,1.f,-0.132075f,-0.237443f,-1.f,1.f,0.51145f,-1.f,-0.612903f,0.f,0.333333f,
0.416667f,-1.f,1.f,0.0566038f,0.283105f,-1.f,1.f,0.267176f,-1.f,0.290323f,0.f,1.f
};
CvMat trainData=cvMat(10,12,CV_32FC1,inputArr);
//标签
int label[10]={1,-1,1,-1,-1,-1,1,1,1,1};
CvMat trainResponse=cvMat(10,1,CV_32SC1,label);
//一个测试样本的特征向量
float testArr[]=
{
0.25f,1.f,1.f,-0.226415f,-0.506849f,-1.f,-1.f,0.374046f,-1.f,-0.83871f,0.f,-1.f
};
CvMat testSample=cvMat(1,12,CV_32FC1,testArr);
CvNormalBayesClassifier nbc;
bool trainFlag = nbc.train(&trainData, &trainResponse);//进行贝叶斯分类器训练
if (trainFlag)
{
cout<<"train over..."<<endl;
nbc.save("normalBayes.xml");//将训练出的模型以xml或yaml格式保存到硬盘
}
else
{
cout<<"train error..."<<endl;
system("pause");
exit(-1);
}
CvNormalBayesClassifier testNbc;
testNbc.load("normalBayes.xml");
float flag = testNbc.predict(&testSample);//进行测试
cout<<"flag = "<<flag<<endl;
system("pause");
return 0;
}
例程二:
//openCV中贝叶斯分类器的API函数用法举例
//运行环境:winXP + VS2008 + openCV2.1.0
#include "stdafx.h"
#include "cxcore.h"
#include "highgui.h"
#include "cvaux.h"
#include "string.h"
#include "stdio.h"
#include "time.h"
#include <ml.h>
#include <iostream>
using namespace cv;
using namespace std;
//10个样本特征向量维数为12的训练样本集,第一列为该样本的类别标签
double inputArr[10][13] =
{
1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1,
-1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,
1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,
-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,
-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,
-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,
1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,
1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,
1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1
};
//一个测试样本的特征向量
double testArr[]=
{
0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1
};
int _tmain(int argc, _TCHAR* argv[])
{
Mat trainData(10, 12, CV_32FC1);//构建训练样本的特征向量
for (int i=0; i<10; i++)
{
for (int j=0; j<12; j++)
{
trainData.at<float>(i, j) = inputArr[i][j+1];
}
}
Mat trainResponse(10, 1, CV_32FC1);//构建训练样本的类别标签
for (int i=0; i<10; i++)
{
trainResponse.at<float>(i, 0) = inputArr[i][0];
}
CvNormalBayesClassifier nbc;
bool trainFlag = nbc.train(trainData, trainResponse);//进行贝叶斯分类器训练
if (trainFlag)
{
cout<<"train over..."<<endl;
nbc.save("c:/normalBayes.txt");
}
else
{
cout<<"train error..."<<endl;
system("pause");
exit(-1);
}
CvNormalBayesClassifier testNbc;
testNbc.load("c:/normalBayes.txt");
Mat testSample(1, 12, CV_32FC1);//构建测试样本
for (int i=0; i<12; i++)
{
testSample.at<float>(0, i) = testArr[i];
}
float flag = testNbc.predict(testSample);//进行测试
cout<<"flag = "<<flag<<endl;
system("pause");
return 0;
}