Merge pull request #10258 from savuor:fix/kmeans_channels

* kmeans: number of channels in _centers fixed

* fixedType() is checked now
pull/10329/head
Rostislav Vasilikhin 7 years ago committed by Alexander Alekhin
parent 28b19d6e3e
commit bab86d65cb
  1. 7
      modules/core/src/kmeans.cpp
  2. 6
      samples/cpp/kmeans.cpp

@ -458,7 +458,12 @@ double cv::kmeans( InputArray _data, int K,
{ {
best_compactness = compactness; best_compactness = compactness;
if( _centers.needed() ) 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); _labels.copyTo(best_labels);
} }
} }

@ -37,7 +37,7 @@ int main( int /*argc*/, char** /*argv*/ )
Mat points(sampleCount, 1, CV_32FC2), labels; Mat points(sampleCount, 1, CV_32FC2), labels;
clusterCount = MIN(clusterCount, sampleCount); clusterCount = MIN(clusterCount, sampleCount);
Mat centers; std::vector<Point2f> centers;
/* generate random sample from multigaussian distribution */ /* generate random sample from multigaussian distribution */
for( k = 0; k < clusterCount; k++ ) for( k = 0; k < clusterCount; k++ )
@ -65,9 +65,9 @@ int main( int /*argc*/, char** /*argv*/ )
Point ipt = points.at<Point2f>(i); Point ipt = points.at<Point2f>(i);
circle( img, ipt, 2, colorTab[clusterIdx], FILLED, LINE_AA ); 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<Point2f>(i); Point2f c = centers[i];
circle( img, c, 40, colorTab[i], 1, LINE_AA ); circle( img, c, 40, colorTab[i], 1, LINE_AA );
} }
cout << "Compactness: " << compactness << endl; cout << "Compactness: " << compactness << endl;

Loading…
Cancel
Save