OpenCV KNN加载训练好的模型

回眸只為那壹抹淺笑 提交于 2019-12-01 21:47:32

 

 

 1 #include<iostream>
 2 #include <opencv2\opencv.hpp>
 3 using namespace cv;
 4 using namespace std;
 5 #include "test.h"
 6 
 7 int main()
 8 {
 9     ///********************测试***************************///
10     Mat test = imread(".\\test\\3.jpg", 0);//截取图像中一个数字
11     Mat bw;
12     threshold(test, bw, 0, 255, CV_THRESH_BINARY);
13     Mat samples = bw.reshape(0, 1);
14     samples.convertTo(samples, CV_32F);
15     // 开始用KNN预测分类,返回识别结果
16     const int K = 4;//testModel->getDefaultK()  
17     Ptr<KNearest> Model = StatModel::load<KNearest>("KnnTest.xml");
18     Mat MatResult(0, 0, CV_32F);//保存测试结果  
19     Model->findNearest(samples, K, MatResult);//knn分类预测 
20     //方法二:
21     //Ptr<KNearest> Model = StatModel::load<KNearest>("KnnTest.xml");
22     //Mat test = imread(".\\test\\3.jpg", 0);//截取图像中一个数字
23     //Mat bw;
24     //threshold(test, bw, 0, 255, CV_THRESH_BINARY);
25     //Mat I0 = bw.reshape(0, 1);
26     //I0.convertTo(I0, CV_32F);
27     //// 开始用KNN预测分类,返回识别结果
28     //float r = Model->predict(I0);    
29 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!