The following is an example of the use of the K proximity classifier given in the manual, which is implemented in the form of Cvmat. The following examples show you how to use OpenCV's own classifiers, how matrix data is accessed, how to draw, how to use OPENCV random number generation functions, and so on. In the second example, the Code section has been annotated.
#include "ml.h" #include "highgui.h" int main (int argc, char** argv) {const int K = 10;int I, j, K, accuracy;float response ; int train_sample_count = 100; Cvrng rng_state = cvrng (-1); cvmat* traindata = Cvcreatemat (Train_sample_count, 2, CV_32FC1); cvmat* trainclasses = Cvcreatemat (Train_sample_count, 1, CV_32FC1); iplimage* img = Cvcreateimage (cvSize (500, 500), 8, 3); float _sample[2]; Cvmat sample = Cvmat (1, 2, CV_32FC1, _sample); Cvzero (IMG); Cvmat trainData1, TrainData2, trainClasses1, trainclasses2;//form the training samplescvgetrows (Traindata, & TrainData1, 0, TRAIN_SAMPLE_COUNT/2); Cvrandarr (&rng_state, &traindata1, Cv_rand_normal, Cvscalar (200, 200), Cvscalar, Cvgetrows (Traindata, &traindata2, TRAIN_SAMPLE_COUNT/2, Train_sample_count); CVRANDARR (& Rng_state, &traindata2, Cv_rand_normal, Cvscalar (+), cvscalar (+), Cvgetrows (trainclasses, &trainC Lasses1, 0, TRAIN_SAMPLE_COUNT/2); Cvset (&trainclasses1, Cvscalar (1); Cvgetrows (trainclasses, &trainclasses2, TRAIN_SAMPLE_COUNT/2, Train_sample_count); CvSet (&trainClasses2 , Cvscalar (2));//Learn Classifiercvknearest KNN (traindata, trainclasses, 0, False, K); cvmat* nearests = Cvcreatemat (1, K, CV_32FC1); for (i = 0; i < img->height; i++) {for (j = 0; J < img->width ; J + +) {sample.data.fl[0] = (float) j;sample.data.fl[1] = (float) i;//estimate the response and get the neighbors ' label Sresponse = knn.find_nearest (&sample,k, 0, 0,nearests, 0);//Compute the number of neighbors representing the Majorit Yfor (k = 0, accuracy = 0; k < K; k++) {if (nearests->data.fl[k] = = response) accuracy++;} Highlight the pixel depending on the accuracy (or confidence) cvset2d (IMG, I, j, response = = 1?) ( Accuracy > 5? Cv_rgb (0, 0): Cv_rgb (0):(accuracy > 5? Cv_rgb (0, 0): Cv_rgb (120, 120, 0));}} Display the original training samplesfor (i = 0; i < TRAIN_SAMPLE_COUNT/2; i++) {Cvpoint pt;pt. x = Cvround (traindata1.data.fl[i*2]);p t.y = Cvround (traindata1.data.fl[i*2+1]); Cvcircle (IMG, PT, 2, Cv_rgb (255, 0, 0), cv_filled);p t.x = Cvround (traindata2.data.fl[i*2]);p t.y = Cvround (traindata2.data.fl[i*2+1]); CvCircle (IMG, PT, 2, CV_ RGB (0, 255, 0), cv_filled);} Cvnamedwindow ("classifier result", 1), cvshowimage ("classifier result", IMG), Cvwaitkey (0); Cvreleasemat (&train Classes); Cvreleasemat (&traindata); return 0;}
Here is an example of the K proximity classifier that you modified based on the mat data type
#include <opencv.hpp>int main (int argc, char** argv) {const int K = 10;int I, j, K, Accuracy;float response;int tra In_sample_count = 100; RNG rng_state = rng (-1);//How to use the Random function Mat Traindata=mat::zeros (Train_sample_count, 2, CV_32FC1); Mat trainclasses =mat::zeros (train_sample_count, 1, CV_32FC1); Mat img = Mat::zeros (500,500,CV_8UC3); float _sample[2]; Mat Sample=mat (1, 2, CV_32FC1, _sample); Mat trainData1, TrainData2, trainClasses1, trainclasses2;//form the training samplestraindata1=traindata.rowrange (0, TRAIN_SAMPLE_COUNT/2);//How to extract some matrices from an existing matrix Rng_state.fill (TrainData1, Cv_rand_normal,mat (1,1,cv_64f,cvscalar (200, 200 ), Mat (1,1,cv_64f,cvscalar (50, 50));//How to use random functions to assign values to existing matrices Traindata2=traindata.rowrange (train_sample_count/2,train_ Sample_count) Rng_state.fill (TrainData2, Cv_rand_normal,mat (1,1,cv_64f,cvscalar), Mat (1,1,cv_64f, (Cvscalar)); Trainclasses1=trainclasses.rowrange (0,TRAIN_SAMPLE_COUNT/2); Trainclasses1.setto (Scalar (1)); /How to initialize the matrix to the same value Trainclasses2=traincLasses.rowrange (Train_sample_count/2,train_sample_count); Trainclasses2.setto (Scalar (2));//Learn Classifiercvknearest KNN (Traindata, Trainclasses,mat (), false, K);//How to establish the Classifier training object Mat nearests (1, K, CV_32FC1); for (i = 0; i < img.rows; i++) {for (j = 0; J < Img.cols; J + +) {sample.at<float> (0,0) = (float) j;sample.at<float> (0,1) = (float) i ;//estimate the response and get the neighbors ' Labelsresponse = knn.find_nearest (sample,k, 0, 0,&nearests, 0);//Use Training The well-practiced classifier classifies new data//compute the number of neighbors representing the majorityfor (k = 0, accuracy = 0; k < K; k++) {if (near Ests.at<float> (0,k) = = response) accuracy++;} Highlight the pixel depending on the accuracy (or confidence) img.at<vec3b> (i,j) [2]=response = = 1? (accuracy>5?180:180):(accuracy>5?0:120);//How to access the multi-channel matrix img.at<vec3b> (i,j) [1]=response = 1? (accuracy>5?0:120):(accuracy>5?180:120);img.at<vec3b> (i,j) [0]=response = 1? (accuracy>5?0:0):(accuracy>5?0:0);}} DiSplay the original training samplesfor (i = 0; i < TRAIN_SAMPLE_COUNT/2; i++) {Cvpoint pt;pt.x = Cvround (traindata1.at& Lt;float> (i,0));p T.y = Cvround (traindata1.at<float> (i,1)), Circle (IMG, PT, 2, Cv_rgb (255, 0, 0), cv_filled);// Paint in the mat data type Pt.x = Cvround (traindata2.at<float> (i,0));p T.y = Cvround (traindata2.at<float> (i,1)), Circle ( IMG, PT, 2, Cv_rgb (0, 255, 0), cv_filled);} Imshow ("classifier result", IMG); cvwaitkey (0); return 0;}
The following is the classifier classification result
Use of OpenCV K proximity classifier