Skip to content

Commit bab86d6

Browse files
savuoralalek
authored andcommitted
Merge pull request opencv#10258 from savuor:fix/kmeans_channels
* kmeans: number of channels in _centers fixed * fixedType() is checked now
1 parent 28b19d6 commit bab86d6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

modules/core/src/kmeans.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,12 @@ double cv::kmeans( InputArray _data, int K,
458458
{
459459
best_compactness = compactness;
460460
if( _centers.needed() )
461-
centers.copyTo(_centers);
461+
{
462+
Mat reshaped = centers;
463+
if(_centers.fixedType() && _centers.channels() == dims)
464+
reshaped = centers.reshape(dims);
465+
reshaped.copyTo(_centers);
466+
}
462467
_labels.copyTo(best_labels);
463468
}
464469
}

samples/cpp/kmeans.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main( int /*argc*/, char** /*argv*/ )
3737
Mat points(sampleCount, 1, CV_32FC2), labels;
3838

3939
clusterCount = MIN(clusterCount, sampleCount);
40-
Mat centers;
40+
std::vector<Point2f> centers;
4141

4242
/* generate random sample from multigaussian distribution */
4343
for( k = 0; k < clusterCount; k++ )
@@ -65,9 +65,9 @@ int main( int /*argc*/, char** /*argv*/ )
6565
Point ipt = points.at<Point2f>(i);
6666
circle( img, ipt, 2, colorTab[clusterIdx], FILLED, LINE_AA );
6767
}
68-
for (i = 0; i < centers.rows; ++i)
68+
for (i = 0; i < (int)centers.size(); ++i)
6969
{
70-
Point2f c = centers.at<Point2f>(i);
70+
Point2f c = centers[i];
7171
circle( img, c, 40, colorTab[i], 1, LINE_AA );
7272
}
7373
cout << "Compactness: " << compactness << endl;

0 commit comments

Comments
 (0)