Seb
Seb

Reputation: 3564

Creating a clustered image from kmeans data in OpenCV

I am trying to create a clustered image from data being returned from the kmeans function. I try to pull out the data in a similar fashion from the OpenCV example but that seems to crash on me. After some further research I saw that someone had pulled data out by using the centers, but did nothing with that data so my trail ended there.

I have included a snippet of my code and what I am doing below. Any help would be greatly appreciated.

EDIT I've reverted my code back to it's original state without any test variables. Still the above error persists. I have also added some debugging information about my image below:

Image information:

While data is NULL I am still able to view the data if I call cv::imshow on it.

// mImage is a cv::Mat that was created from a 256 x 256 image with the depth of
// CV_32F and 3 channels
// labels is a cv::Mat that was created by converting the image into a 256 x 256
// CV_8UC1 IplImage
kmeans(mImage, 6, labels, termcrit, 3, cv::KMEANS_PP_CENTERS, centers);
float* c = new float[6];
memcpy(c, centers.ptr<float>(0), sizeof(float)*6);

// Block of code that crashes when I do "mImage.at<cv::Point2f>(i)" with the error:
/* OpenCV Error: Assertion failed (dims <= 2 && data && (size.p[0] == 1 || 
       size.p[1] == 1) && (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && 
       elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << 
       ((((sizeof(size_t)/ 4+1)*16384|0x3a50) >> ((DataType<_Tp>::type) & 
       ((1 << 3) - 1))*2) & 3))) in unknown function, file 
       c:\opencv-2.3.0\modules\core\include\opencv2\core\mat.hpp, line 583 */

for(int i = 0; i < list.rows; i++)
{
    int clusterIdx = list.at<int>(i);
    cv::Point ipt = mImage.at<cv::Point2f>(i);
    circle( miplImage, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA );
}

Upvotes: 3

Views: 3029

Answers (1)

SSteve
SSteve

Reputation: 10738

You are getting the assertion error at the call to mRegion.at(). Where is mRegion defined?

Here's the equivalent snippet from the kmeans.cpp sample code:

    kmeans(points, clusterCount, labels, 
           TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
           3, KMEANS_PP_CENTERS, centers);

    img = Scalar::all(0);

    for( i = 0; i < sampleCount; i++ )
    {
        int clusterIdx = labels.at<int>(i);
        Point ipt = points.at<Point2f>(i);
        circle( img, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA );
    }

It uses the same variable points for both the first argument to kmeans and as the source for ipt in the for loop. I think you probably want to use mImage in the for loop, not mRegion.

Edit So now that you've changed the code to use mImage in the for loop, you need to see why you are getting the assertion error. The assertion message is very cluttered by the template code so here it is from mat.hpp:

dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) &&
             (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) &&
             elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type)

One or more of those tests fails. If you are using an empty mImage, that would be a failure because data would be nil. You can't call at on an empty matrix. In any case, examine the properties of mImage to see which is causing the assertion error.

Upvotes: 1

Related Questions