diff --git a/modules/core/src/kmeans.cpp b/modules/core/src/kmeans.cpp index 543993355b..495ff5e3c1 100644 --- a/modules/core/src/kmeans.cpp +++ b/modules/core/src/kmeans.cpp @@ -458,7 +458,12 @@ double cv::kmeans( InputArray _data, int K, { best_compactness = compactness; if( _centers.needed() ) - centers.copyTo(_centers); + { + Mat reshaped = centers; + if(_centers.fixedType() && _centers.channels() == dims) + reshaped = centers.reshape(dims); + reshaped.copyTo(_centers); + } _labels.copyTo(best_labels); } } diff --git a/samples/cpp/kmeans.cpp b/samples/cpp/kmeans.cpp index eeba97a3ff..0a2663ac7c 100644 --- a/samples/cpp/kmeans.cpp +++ b/samples/cpp/kmeans.cpp @@ -37,7 +37,7 @@ int main( int /*argc*/, char** /*argv*/ ) Mat points(sampleCount, 1, CV_32FC2), labels; clusterCount = MIN(clusterCount, sampleCount); - Mat centers; + std::vector centers; /* generate random sample from multigaussian distribution */ for( k = 0; k < clusterCount; k++ ) @@ -65,9 +65,9 @@ int main( int /*argc*/, char** /*argv*/ ) Point ipt = points.at(i); circle( img, ipt, 2, colorTab[clusterIdx], FILLED, LINE_AA ); } - for (i = 0; i < centers.rows; ++i) + for (i = 0; i < (int)centers.size(); ++i) { - Point2f c = centers.at(i); + Point2f c = centers[i]; circle( img, c, 40, colorTab[i], 1, LINE_AA ); } cout << "Compactness: " << compactness << endl;