Implementation of OpenCV 3 KNN

As you may know, a lot has changed in OpenCV 3. In the previous version of OpenCV, I did it like this:

Mat trainData(classes * samples, ImageSize, CV_32FC1); Mat trainClasses(classes * samples, 1, CV_32FC1); KNNLearning(&trainData, &trainClasses); //learning function KNearest knearest(trainData, trainClasses); //creating //loading input image Mat input = imread("input.jpg"); //digital recognition learningTest(input, knearest);//test 

I also found an example of how to understand this, but I still have errors in the create function:

 Ptr<KNearest> knearestKdt = KNearest::create(ml::KNearest::Params(10, true, INT_MAX, ml::KNearest::KDTREE)); knearestKdt->train(trainData, ml::ROW_SAMPLE, trainLabels); knearestKdt->findNearest(testData, 4, bestLabels); 

Can you provide me with information on how to correctly rewrite the actual KNearest code to openCV 3?

+5
source share
2 answers

The API has changed again since the @ aperture labs respond. I hope they keep up with the documentation when they release new features or changes in the future.

A working example is as follows

 using namespace cv::ml; //Be sure to change number_of_... to fit your data! Mat matTrainFeatures(0,number_of_train_elements,CV_32F); Mat matSample(0,number_of_sample_elements,CV_32F); Mat matTrainLabels(0,number_of_train_elements,CV_32F); Mat matSampleLabels(0,number_of_sample_elements,CV_32F); Mat matResults(0,0,CV_32F); //etcetera code for loading data into Mat variables suppressed Ptr<TrainData> trainingData; Ptr<KNearest> kclassifier=KNearest::create(); trainingData=TrainData::create(matTrainFeatures, SampleTypes::ROW_SAMPLE,matTrainLabels); kclassifier->setIsClassifier(true); kclassifier->setAlgorithmType(KNearest::Types::BRUTE_FORCE); kclassifier->setDefaultK(1); kclassifier->train(trainingData); kclassifier->findNearest(matSample,kclassifier->getDefaultK(),matResults); //Just checking the settings cout<<"Training data: "<<endl <<"getNSamples\t"<<trainingData->getNSamples()<<endl <<"getSamples\n"<<trainingData->getSamples()<<endl <<endl; cout<<"Classifier :"<<endl <<"kclassifier->getDefaultK(): "<<kclassifier->getDefaultK()<<endl <<"kclassifier->getIsClassifier() : "<<kclassifier->getIsClassifier()<<endl <<"kclassifier->getAlgorithmType(): "<<kclassifier->getAlgorithmType()<<endl <<endl; //confirming sample order cout<<"matSample: "<<endl <<matSample<<endl <<endl; //displaying the results cout<<"matResults: "<<endl <<matResults<<endl <<endl; //etcetera ending for main function 
+7
source
 KNearest::Params params; params.defaultK=5; params.isclassifier=true; //////// Train and find with knearest Ptr<TrainData> knn; knn= TrainData::create(AmatOfFeatures,ROW_SAMPLE,AmatOfLabels); Ptr<KNearest> knn1; knn1=StatModel::train<KNearest>(knn,params); knn1->findNearest(AmatOfFeaturesToTest,4,ResultMatOfNearestNeighbours); ///////////////// 

The names of these functions will help you find them in the documentation. However, the documentation can be a bit confusing until it is completely updated, so the best way to do exactly what you want is to make a small toy example and use the trial and error method. This is a working example, inserted directly from my own code, which has been proven to work. Hope this helps.

0
source

Source: https://habr.com/ru/post/1211453/


All Articles