Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

there you go ;)

Mat coords;
for ( int i=0; i<tmpsp.rows; i++ )
for ( int j=0; j<tmpsp.cols; j++ )
{
    if ( tmpsp.at<uchar>(i,j) < 60 ) // some threshold
       continue;
    coords.push_back(Point(j,i));
}
coords = coords.reshape( 1, coords.cols/2 ); // n rows a 2 cols
coords.convertTo(coords , CV_32F);


Mat labels,centers;

kmeans(coords , 4, labels, TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0/*CV_TERMCRIT_ITER, 1, 1.0*/), 3, KMEANS_RANDOM_CENTERS, centers);

cerr << centers.size() << endl;   
cerr << centers<< endl;
cerr << labels.size() << endl;

for (int i=0; i<4; i++) {
    circle(ocv,Point(centers.at<float>(i,0),centers.at<float>(i,1)),4,Scalar(200));
}

[2 x 4]
[26.261696, 15.105264;
  46.481853, 10.9375;
  72.73671, 30.278481;
  14.530172, 38.497845]
[1 x 2039]

image description

there you go ;)

Mat coords;
for ( int i=0; i<tmpsp.rows; i++ )
for ( int j=0; j<tmpsp.cols; j++ )
{
    if ( tmpsp.at<uchar>(i,j) < 60 ) // some threshold
       continue;
    coords.push_back(Point(j,i));
}
coords = coords.reshape( 1, coords.cols/2 ); // n rows a 2 cols
coords.convertTo(coords , CV_32F);


Mat labels,centers;

kmeans(coords , 4, labels, TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0/*CV_TERMCRIT_ITER, 1, 1.0*/), 3, KMEANS_RANDOM_CENTERS, centers);

cerr << centers.size() << endl;   
cerr << centers<< endl;
cerr << labels.size() << endl;

for (int i=0; i<4; i++) {
    circle(ocv,Point(centers.at<float>(i,0),centers.at<float>(i,1)),4,Scalar(200));
}

[2 x 4]
[26.261696, 15.105264;
  46.481853, 10.9375;
  72.73671, 30.278481;
  14.530172, 38.497845]
[1 x 2039]

image description

so, answer to the rephrased question:

"trying to seperate the white area into 4 section and find their centroids"


Mat coords;
for ( int i=0; i<tmpsp.rows; i++ )
for ( int j=0; j<tmpsp.cols; j++ )
{
    if ( tmpsp.at<uchar>(i,j) < 60 ) // some threshold
       continue;
    coords.push_back(Point(j,i));
}
coords = coords.reshape( 1, coords.cols/2 ); // n rows a 2 cols
coords.convertTo(coords , CV_32F);


Mat labels,centers;

kmeans(coords , 4, labels, TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0/*CV_TERMCRIT_ITER, 1, 1.0*/), 3, KMEANS_RANDOM_CENTERS, centers);

cerr << centers.size() << endl;   
cerr << centers<< endl;
cerr << labels.size() << endl;

for (int i=0; i<4; i++) {
    circle(ocv,Point(centers.at<float>(i,0),centers.at<float>(i,1)),4,Scalar(200));
}

[2 x 4]
[26.261696, 15.105264;
  46.481853, 10.9375;
  72.73671, 30.278481;
  14.530172, 38.497845]
[1 x 2039]

image description