am running openCv kMeans on a vector of centre-points of contours. Like this:
/// Load source image
src = imread("image.jpg", 1);
/// Convert image to gray and blur it
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));
/// Detect edges using Threshold
threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
/// Find contours
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Get the moments
vector<Moments> mu(contours.size());
for (int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
/// Get the mass centers:
vector<cv::Point2f> centroids(contours.size());
for (int i = 0; i < contours.size(); i++)
{
centroids[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
}
int cNumber = sqrt(centroids.size() / 2);
vector<Point2f> markerRawCorners(cNumber);
//get clusters
//FAILS BETWEEN HERE
Mat labels, centers;
int attempts = 50, flags = cv::KMEANS_PP_CENTERS;
TermCriteria tc;
if (centroids.size() > 5)
{
kmeans(centroids, cNumber, labels, tc, attempts, flags, centers); // points will get cast implicitly to Mat
}
//AND HERE
on some images, it works fine, but if i adjust the threshold, or use a different image, it gives me two Assertion failed errors, as below:
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0])) in cv::Mat::ptr, file C:\OpenCV\opencv\sources\modules\core\include\opencv2/core/mat.hpp, line 429
OpenCV Error: Assertion failed (udata < (uchar)ptr && ((uchar)ptr - udata) <=(ptrdiff_t)(sizeof(void*)+16)) in cv::fastFree, file src\alloc.cpp, line 78
What do these errors mean? And how can i stop them happening?