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:
- Dimensions: 2
- Image data:
- Size 0: 256
- Size 1: 256
- Elemsize 1: 12
- Elemsize 2: 4
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 );
}
You are getting the assertion error at the call to mRegion.at(). Where is mRegion defined?
Here’s the equivalent snippet from the
kmeans.cppsample code:It uses the same variable
pointsfor both the first argument tokmeansand as the source foriptin theforloop. I think you probably want to usemImagein theforloop, not mRegion.Edit So now that you’ve changed the code to use mImage in the
forloop, 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:One or more of those tests fails. If you are using an empty mImage, that would be a failure because
datawould be nil. You can’t callaton an empty matrix. In any case, examine the properties of mImage to see which is causing the assertion error.