mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
Fix ml::KDTree::findNearest
This commit is contained in:
@@ -396,8 +396,21 @@ public:
|
|||||||
{
|
{
|
||||||
Mat _res, _nr, _d;
|
Mat _res, _nr, _d;
|
||||||
tr.findNearest(test_samples.row(i), k, Emax, _res, _nr, _d, noArray());
|
tr.findNearest(test_samples.row(i), k, Emax, _res, _nr, _d, noArray());
|
||||||
res.push_back(_res.t());
|
if( _results.needed() )
|
||||||
_results.assign(res);
|
{
|
||||||
|
res.push_back(_res.t());
|
||||||
|
_results.assign(res);
|
||||||
|
}
|
||||||
|
if( _neighborResponses.needed() )
|
||||||
|
{
|
||||||
|
nr.push_back(_nr.t());
|
||||||
|
_neighborResponses.assign(nr);
|
||||||
|
}
|
||||||
|
if( _dists.needed() )
|
||||||
|
{
|
||||||
|
d.push_back(_d.t());
|
||||||
|
_dists.assign(d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result; // currently always 0
|
return result; // currently always 0
|
||||||
|
|||||||
@@ -39,11 +39,16 @@ TEST(ML_KNearest, accuracy)
|
|||||||
{
|
{
|
||||||
SCOPED_TRACE("KDTree");
|
SCOPED_TRACE("KDTree");
|
||||||
Mat neighborIndexes;
|
Mat neighborIndexes;
|
||||||
|
Mat neighborResponses;
|
||||||
|
Mat dists;
|
||||||
float err = 1000;
|
float err = 1000;
|
||||||
Ptr<KNearest> knn = KNearest::create();
|
Ptr<KNearest> knn = KNearest::create();
|
||||||
knn->setAlgorithmType(KNearest::KDTREE);
|
knn->setAlgorithmType(KNearest::KDTREE);
|
||||||
knn->train(trainData, ml::ROW_SAMPLE, trainLabels);
|
knn->train(trainData, ml::ROW_SAMPLE, trainLabels);
|
||||||
knn->findNearest(testData, 4, neighborIndexes);
|
knn->findNearest(testData, 4, neighborIndexes, neighborResponses, dists);
|
||||||
|
EXPECT_EQ(neighborIndexes.size(), Size(4, pointsCount));
|
||||||
|
EXPECT_EQ(neighborResponses.size(), Size(4, pointsCount * 2));
|
||||||
|
EXPECT_EQ(dists.size(), Size(4, pointsCount));
|
||||||
Mat bestLabels;
|
Mat bestLabels;
|
||||||
// The output of the KDTree are the neighbor indexes, not actual class labels
|
// The output of the KDTree are the neighbor indexes, not actual class labels
|
||||||
// so we need to do some extra work to get actual predictions
|
// so we need to do some extra work to get actual predictions
|
||||||
|
|||||||
Reference in New Issue
Block a user