Skip to content

Instantly share code, notes, and snippets.

@hkhojasteh
Created January 30, 2019 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkhojasteh/32802712b675359c309716c4d1d42ae1 to your computer and use it in GitHub Desktop.
Save hkhojasteh/32802712b675359c309716c4d1d42ae1 to your computer and use it in GitHub Desktop.
//Make good representation for clustering
Mat points = Mat::zeros(sum(img_zeroone)[0], 2, CV_32F);
for (int i = 0, k = 0; i < img_zeroone.rows; i++){
for (int j = 0; j < img_zeroone.cols; j++){
if ((int)img_zeroone.at<char>(i, j) == 255){
points.at<float>(k, 0) = i;
points.at<float>(k, 1) = j;
k++;
}
}
}
Mat kCenters, kLabels; //Clustering
int clusterCount = 5, attempts = 10, iterationNumber = 1e40;
kmeans(points, clusterCount, kLabels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, iterationNumber, 1e-4), attempts, KMEANS_PP_CENTERS, kCenters);
for (int i = 0; i < kCenters.rows; i++){
float x = kCenters.at<float>(i, 1), y = kCenters.at<float>(i, 0);
circle(img_sharp_not, Point(x, y), 2, (0, 0, 255), -1);
rectangle(img_sharp_not, Rect(x - 13, y - 13, 26, 26), Scalar(255, 255, 255));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment