0
votes

My kmeans() function is failing with the error:

OpenCV Error: Assertion failed (N >= K) in cv::kmeans, file C:\builds\master_Pac kSlave-win32-vc12-shared\opencv\modules\core\src\kmeans.cpp, line 231

Whats my error? 'N >= K' must mean its checking whether the Mat rows*cols(length) is > clusters - which mine is (I think). My Mat has 1 row with around 80k columns. Is my Mat that I am passing as the first parameter (for kmeans) empty of pixel/voxel data? I have confirmed that this parameter is a 'collapsed' image (1 row, 80K columns). So its not quite empty but it could all that it's all black pixels which may be the error?

Mat image = imread("images/jp.png", CV_32F); // The Jurassic Park movie logo
cvtColor(image, image, CV_BGR2RGB);

Mat collapsedImage = image.reshape(1, 1);
collapsedImage.convertTo(collapsedImage, CV_32F);

int clusterCount = 2;
Mat labels, centres;

// Assertion error thrown here
kmeans(collapsedImage, clusterCount, labels,
    TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0),
    3, KMEANS_PP_CENTERS, centres);

imshow("flat", collapsedImage); // shows long flat with all black pixels
imshow("image", image);
1
In Mat image = imread("images/jp.png", CV_32F); the CV_32F doesn't make sense.... If you want to load a BGR image (3 channels), you can use Mat image = imread(path); as default, or Mat image = imread(path, IMREAD_COLOR);Miki
You can also check here how to use kmeans with color images.Miki

1 Answers

0
votes

Ok looks like the problem was my reshaping. It should be:

Mat collapsedImage = image.reshape(1, image.rows * image.cols);