KNearest train parameters? C++
Hi guys, i need to use the KNearest from OpenCV in C++, i read the Doc but i did't undertand it well.
I have a Mat_<float> that rapresent a confidence map, and i also have confidence map (Mat_float) and a set of feature (Mat_<float>[40]) that i used to compute the confidence confidence features (Mat_float[40]) associated with this map. (so every point So each value of confidence map is associated with 40 values.
I need to find the mean value of the KNN of each pixel of the confidence map and i need to use the confidence features as weight for the knn search.
I tryied to implement this in c++, it works in some ways but it is very slow. (maybe 1/2 hours to complete the run)
Ptr<ml::KNearest> knn(ml::KNearest::create());
knn->setIsClassifier(false);
int num_samples = rows*cols;
Mat samples; //Each row has 40 features) I need to use the set of feature to find the KNN (with K=20) of each pixel i in the confidence map because i need their values to calculate the average
features and represents a pixel
samples.create(num_samples, 40, CV_32F);
Mat response; //The i-row of the sample is associated with te (0, i) value of the array (or not?)
response.create(1, num_samples, CV_32F);
//Here i just fill the samples and response
num_samples = 0;
for(int r = 0; r < rows; r++){
for(int c = 0; c < cols; c++){
response.at<float>(0, num_samples) = _confidence_map.at<float>(r, c);
for(int k = 0; k < 40; k++){
samples.at<float>(num_samples, k) = feature_array[k].at<float>(r,c);
}
num_samples++;
}
}
//The train is very fast
knn->train(samples, cv::ml::ROW_SAMPLE, response);
Mat result;
num_samples = 0;
//here the code starts to slow a lot
for(int r = 0; r < rows; r++){
for(int c = 0; c < cols; c++){
knn->findNearest(samples.row(num_samples), 20, result);
//Do stuff with result
num_samples++;
}
}
Am i doing something wrong? Thanks for the pixel i (the value of i is the average ok the value of the K-nearest neighbour).
Opencv offer KNearest class with thei function train and findNearest help, i already tried to ask this yesterday but i didn't get how to use to functions properly.
Thanksmy question wasn't clear so i closed it and posted this one.