Ask Your Question

Revision history [back]

You could use image moments for that: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=moments#moments

You could use image moments for that: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=moments#moments

you can find a sample code at SO

You could use image moments for that: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=moments#moments

you can find EDIT : begging @foobar 's indulgence, i add a sample code at SO

code

@sturkmen

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

Point getCentroid( InputArray Points )
{
    Point Coord;
    Moments mm = moments( Points, false );
    double moment10 = mm.m10;
    double moment01 = mm.m01;
    double moment00 = mm.m00;
    Coord.x = int(moment10 / moment00);
    Coord.y = int(moment01 / moment00);
    return Coord;
}

int main( int argc, char* argv[] )
{
    Mat src = imread( argv[1] );

    Mat gray;
    cvtColor( src, gray, CV_BGR2GRAY );
    gray = gray > 127;

    Mat nonzeropixels;
    findNonZero( gray, nonzeropixels );

    Point pt = getCentroid( nonzeropixels );
    rectangle(src, pt,pt, Scalar(0, 255, 0), 2);

    imshow("result",src);
    waitKey();

    return 0;
}

test image and result image ( green dot is centroid of white blobs )

image description image description