OpenCV KNN classification numbers

(1), cv :: ml :: Knearest categories: Inherited from cv :: ml :: StateModel, and cv :: ml :: StateModel also inherited from cv :: Algorithm;

(2), create a function: as a static, new is used to create a KNearestImpl a KNearest objects;

(3), setDefaultK / getDefaultK function: When the prediction, the value of K is provided / acquired;

(4), setIsClassifier / getIsClassifier Function: Set / Get the application is KNN classification or regression;

(5), setEmax / getEmax function: When using KDTree algorithm, set / get parameter value Emax;

(6), setAlgorithmType / getAlgorithmType Function: Set / Get KNN algorithm types, currently supports two: brute_force and KDTree;

(7), findNearest function: The input prediction classification / regression results.

 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     Mat img = imread("1.png");
10     Mat gray;
11     cvtColor(img, gray, CV_BGR2GRAY);
12     threshold(gray, gray, 0, 255, CV_THRESH_BINARY);
13     //digits.png 2000 * 1000, wherein the size of each number is 20 * 20,
 14      // total of 5000 ((2000 * 1000) / (20 * 20)) numbers, type of [0-9],
 15      // [0-9] has 10 digits each digital samples 5000/10 = 500
 16      @ them into a single image and 20 * 20 serialized (converted to a one-dimensional array) 
. 17      int Side = 20 is ;
 18 is      int m = gray.rows / Side;
 . 19      int n-= gray.cols / Side;
 20 is      Mat Data, Labels;
 21 is      for ( int I = 0 ; I <m; I ++ ) {
 22 is  
23 is          int offsetRow = I * Side;
 24-          for( Int J = 0 ; J <n-; J ++ ) {
 25  
26 is              int offsetCol = J * Side;
 27              // intercepting pieces 20 * 20 
28              
29                  
30              Mat tmp;
 31 is              
32              Gray (the Range (offsetRow, offsetRow + Side) , the Range (offsetCol, offsetCol + . Side)) the copyTo (tmp);
 33 is              
34 is              data.push_back (tmp.reshape ( 0 , . 1 ));   // the sequence of one-dimensional vector is converted into a 
35              labels.push_back (I / . 5 );             // for each label 500 is a type             
36         }
 37 [      }
 38 is      data.convertTo (Data, CV_32F);
 39      COUT << " read End ... " << endl;
 40      // ************************************************** KNN algorithm trained using ************************************************************ //
 41 is      int K = . 7 ;     // change the value of K may appear different results, the larger the value of K, identification the slower 
42 is      the Ptr <TrainData> tData = TrainData :: Create (Data, ROW_SAMPLE, Labels);
 43 is      the Ptr <KNearest> Model = KNearest :: Create ();
 44 is      Model-> setDefaultK (K);
 45      Model-> setIsClassifier ( to true );
46 is      Model-> Train (tData);
 47      Model-> Save ( " KnnTest.xml " );
 48      /// ************************************************************ ** Test Model ************************************************************ ///
 49      Mat = imread Test ( " \\ \\ 3.jpg Test. " , 0 ); / / interception of a digital image 
50      Mat BW;
 51 is      threshold (Test, BW, 0 , 255 , CV_THRESH_BINARY);
 52 is      Mat bw.reshape I0 = ( 0 , . 1 );
 53 is      I0.convertTo (I0, CV_32F);
 54 is      //KNN classification prediction began, returned recognition result 
55      a float R & lt Model- => Predict (I0);
 56 is      
57 is }

 

Guess you like

Origin www.cnblogs.com/hsy1941/p/11717703.html