本帖最后由 smallyouyou 于 2016-5-15 22:46 编辑
用類神經做看看,
這邊有基本的用法,
我還沒用過opencv這塊,
可能要研究一下,
只能先找了這個網址給你參考用法,
http://blog.csdn.net/xiaowei_cqu/article/details/9027617
然後下面是貝氏分類器的例子也可以做看看,
- #include "opencv2/opencv.hpp"
- #include "iostream"
- using namespace cv;
- using namespace std;
- 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[11][12]=
- {
- 0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
- 0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1,
- 0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,
- 0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,
- 0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,
- 0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,
- 0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,
- 0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
- 0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,
- 0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,
- 0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1
- };
- int main()
- {
- 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("d:/normalBayes.txt");
- }
- else
- {
- cout<<"train error..."<<endl;
- system("pause");
- exit(-1);
- }
- CvNormalBayesClassifier testNbc;
- testNbc.load("d:/normalBayes.txt");
- Mat testSample(1, 12, CV_32FC1);//构建测试样本
- for (int i=0; i<11; i++)
- {
- for (int j=0; j<12; j++)
- {
- testSample.at<float>(0, j) = testArr[i][j];
- }
- float flag = testNbc.predict(testSample);//进行测试
- cout<<"flag = "<<flag<<endl;
- }
- system("pause");
- return 0;
- }
复制代码
|