# Revision history [back]

### 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(r, c, CV_32F) and for each pixel i need to find the 20-Nearest neighbour.

I thought that i could use cv::ml::KNearest but i didn't get which are the correct parameters:

 cv::ml::KNearest::findNearest ( InputArray samples, int k, OutputArray results)


is ok if i use my Mat as sample and k = 20?

How can i get te 20-Nearest neighbour for each pixel after the train?

Thanks

### 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(r, c, CV_32F) and for each pixel i need to find the 20-Nearest neighbour.

I thought that i could use cv::ml::KNearest but i didn't get which are the correct parameters:

 cv::ml::KNearest::findNearest cv::ml::KNearest::train ( InputArray samples, int k, OutputArray ml::ROW_SAMPLES, results)


is ok if i use my Mat as sample and k = 20?

How but how can i get te 20-Nearest neighbour for each pixel after the train?

Thanks

### 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(r, c, CV_32F) and for each pixel i need to find the 20-Nearest neighbour.

I thought that i could use cv::ml::KNearest but i didn't get which are the correct parameters:parameters for the function findNearest :

 cv::ml::KNearest::train ( InputArray samples, ml::ROW_SAMPLES, results)


but how can i get te 20-Nearest neighbour for each pixel after the train?

cv::ml::KNearest::findNearest   (   InputArray      samples,
int     k,
OutputArray     results,
OutputArray     neighborResponses = noArray(),
OutputArray     dist = noArray()
)


i got that i have to use this but i didn't understand how.

Thanks

### 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(r, c, CV_32F) Mat_<float> that rapresent a confidence map, and for each pixel i need also have a set of feature (Mat_<float>[40]) that i used to find compute the 20-Nearest neighbour.confidence map. (so every point has 40 features)

I thought that need to use the set of feature to find the KNN (with K=20) of each pixel i could use cv::ml::KNearest in the confidence map because i need their values to calculate the average value 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 but i didn't get which are the correct parameters for the function findNearest :

 cv::ml::KNearest::train ( InputArray samples, ml::ROW_SAMPLES, results)


but how can i get te 20-Nearest neighbour for each pixel after the train?

cv::ml::KNearest::findNearest   (   InputArray      samples,
int     k,
OutputArray     results,
OutputArray     neighborResponses = noArray(),
OutputArray     dist = noArray()
)


i got that i have to use this but i didn't understand how.to functions properly.

Thanks

### 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.

### 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 confidence map (Mat_float) and a set of confidence features (Mat_float[40]) associated with this map. 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 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),
knn->findNearest(samples, 20,  result);
//Do stuff with result

num_samples++;
}
}


Am i doing something wrong? Thanks for the help, i already tried to ask this yesterday but my question wasn't clear so i closed it and posted this one.

### 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 confidence map (Mat_float) and a set of confidence features (Mat_float[40]) associated with this map. 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 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;

knn->findNearest(samples, 20, result);


Am i doing something wrong? Thanks for the help, i already tried to ask this yesterday but my question wasn't clear so i closed it and posted this one.help.

### 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 confidence map (Mat_float) and a set of confidence features (Mat_float[40]) associated with this map. 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 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;

knn->findNearest(samples, 20, result);


Am i doing something wrong? Thanks for the help.