Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

assertion failed error in kmeans

i am trying to write a code to posterize an image using kmeans in open 3.0 (c++) the code is as follows : int main() { Mat src = imread("1.jpg", 1 ); Mat samples(src.rows * src.cols, 3, CV_32F); //Mat samples(3,src.rowssrc.cols,CV_32F); for( int y = 0; y < src.rows; y++ ) for( int x = 0; x < src.cols; x++ ) for( int z = 0; z < 3; z++) samples.at<float>(y + xsrc.rows, z) = src.at<vec3b>(y,x)[z];

int clusterCount = 3; Mat labels; int attempts = 5; Mat centers; //kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers );

kmeans(samples,clusterCount, labels,TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 10, 1.0),3, KMEANS_RANDOM_CENTERS, centers); Mat new_image(src.rows * src.cols, 3, CV_32S); for( int y = 0; y < src.rows; y++ ) for( int x = 0; x < src.cols; x++ ) { int cluster_idx = labels.at<int>(y + x*src.rows,0); new_image.at<vec3b>(y,x)[0] = centers.at<int>(cluster_idx, 0); new_image.at<vec3b>(y,x)[1] = centers.at<int>(cluster_idx, 1); new_image.at<vec3b>(y,x)[2] = centers.at<int>(cluster_idx, 2); } imshow( "clustered image", new_image ); waitKey(); } i am getting an assertion failed error : OpenCV Error: Assertion failed (N >= K) in cv::kmeans, file C:\buildslave64\win64_amdocl\master_PackSlave-win64-vc11-shared\opencv\modules\core\src\kmeans.cpp, line 231 how should i resolve this ?

click to hide/show revision 2
No.2 Revision

updated 2017-01-14 02:06:57 -0600

berak gravatar image

assertion failed error in kmeans

i am trying to write a code to posterize an image using kmeans in open 3.0 (c++) the code is as follows : :

int main()
{
  Mat src = imread("1.jpg", 1 );
  Mat samples(src.rows * src.cols, 3, CV_32F);
  //Mat samples(3,src.rowssrc.cols,CV_32F);
samples(3,src.rows*src.cols,CV_32F);
  for( int y = 0; y < src.rows; y++ )
    for( int x = 0; x < src.cols; x++ )
      for( int z = 0; z < 3; z++)
        samples.at<float>(y + xsrc.rows, x*src.rows, z) = src.at<vec3b>(y,x)[z];

src.at<Vec3b>(y,x)[z]; int clusterCount = 3; Mat labels; int attempts = 5; Mat centers; //kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers );

); kmeans(samples,clusterCount, labels,TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 10, 1.0),3, KMEANS_RANDOM_CENTERS, centers); Mat new_image(src.rows * src.cols, 3, CV_32S); for( int y = 0; y < src.rows; y++ ) for( int x = 0; x < src.cols; x++ ) { int cluster_idx = labels.at<int>(y + x*src.rows,0); new_image.at<vec3b>(y,x)[0] new_image.at<Vec3b>(y,x)[0] = centers.at<int>(cluster_idx, 0); new_image.at<vec3b>(y,x)[1] new_image.at<Vec3b>(y,x)[1] = centers.at<int>(cluster_idx, 1); new_image.at<vec3b>(y,x)[2] new_image.at<Vec3b>(y,x)[2] = centers.at<int>(cluster_idx, 2); } imshow( "clustered image", new_image ); waitKey(); }

i am getting an assertion failed error : OpenCV Error: Assertion failed (N >= K) in cv::kmeans, file C:\buildslave64\win64_amdocl\master_PackSlave-win64-vc11-shared\opencv\modules\core\src\kmeans.cpp, line 231 how should i resolve this ?