Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV running kmeans algorithm on an image

I am trying to run kmeans on a 3 channel color image, but every time I try to run the function it seems to crash with the following error:

OpenCV Error: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0) in unknown function, file ..\..\..\OpenCV-2.3.0\modules\core\src\matrix.cpp, line 2271

I've included the code below with some comments to help specify what is being passed in. Any help is greatly appreciated.

// Load in an image
// Depth: 8, Channels: 3
IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");

// Create a matrix to the image
cv::Mat mImage = cv::Mat(iplImage);

// Create a single channel image to create our labels needed
IplImage* iplLabels = cvCreateImage(cvGetSize(iplImage), iplImage->depth, 1);

// Convert the image to grayscale
cvCvtColor(iplImage, iplLabels, CV_RGB2GRAY);

// Create the matrix for the labels
cv::Mat mLabels = cv::Mat(iplLabels);

// Create the labels
int rows = mLabels.total();
int cols = 1;
cv::Mat list(rows, cols, mLabels .type());
uchar* src;
uchar* dest = list.ptr(0);
for(int i=0; i<mLabels.size().height; i++) 
{
    src = mLabels.ptr(i);
    memcpy(dest, src, mLabels.step);
    dest += mLabels.step;
}
list.convertTo(list, CV_32F);

// Run the algorithm
cv::Mat labellist(list.size(), CV_8UC1);
cv::Mat centers(6, 1, mImage.type());
cv::TermCriteria termcrit(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0);
kmeans(mImage, 6, labellist, termcrit, 3, cv::KMEANS_PP_CENTERS, centers);
like image 377
Seb Avatar asked Aug 10 '11 18:08

Seb


People also ask

Can Kmeans be used for image classification?

Yes! K-Means Clustering can be used for Image Classification of MNIST dataset. Here's how. K-means clustering is an unsupervised learning algorithm which aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest centroid.

How k-means algorithm can be used for image segmentation?

K -means clustering algorithm is an unsupervised algorithm and it is used to segment the interest area from the background. But before applying K -means algorithm, first partial stretching enhancement is applied to the image to improve the quality of the image.

How do you cluster an image?

Clustering of images is a multi-step process for which the steps are to pre-process the images, extract the features, cluster the images on similarity, and evaluate for the optimal number of clusters using a measure of goodness.


1 Answers

The error says all: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0)

These are very simple rules to understand, the function will work only if:

  • mImage.depth() is CV_32F

  • if mImage.dims is <= 2

  • and if K > 0. In this case, you define K as 6.

From what you stated on the question, it seems that:

IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");` 

is loading the image as IPL_DEPTH_8U by default and not IPL_DEPTH_32F. This means that mImage is also IPL_DEPTH_8U, which is why your code is not working.

like image 108
karlphillip Avatar answered Sep 23 '22 03:09

karlphillip