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 }