Merge pull request #10258 from savuor:fix/kmeans_channels

* kmeans: number of channels in _centers fixed

* fixedType() is checked now
This commit is contained in:
Rostislav Vasilikhin 2017-12-15 21:48:48 +03:00 committed by Alexander Alekhin
parent 28b19d6e3e
commit bab86d65cb
2 changed files with 9 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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<Point2f> 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<Point2f>(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<Point2f>(i);
Point2f c = centers[i];
circle( img, c, 40, colorTab[i], 1, LINE_AA );
}
cout << "Compactness: " << compactness << endl;