Ask Your Question
1

how to find the centre of multiple objects in a image

asked 2016-01-07 04:02:02 -0600

avpai gravatar image

updated 2016-01-07 22:53:00 -0600

I got this image

image description

I like to find the centre of each white area and mark it as red (also get their coordinates). I am looking into moments functions, is there a better way to get the centre point? do help or share ur codes/ideas.

UPDATE:- I tried to find the contour of this image using the below codes:-

    IplImage* mask1 = NULL;        
CvMemStorage * storage5 = cvCreateMemStorage(0);    
CvSeq * contour1 = 0;
mask1= cvCloneImage(mask);
cvShowImage("mask1", mask1);

cvFindContours(mask, storage5, &contour1, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvDrawContours(mask1, contour1, CV_RGB(0, 255, 255), CV_RGB(255, 0, 0), 2, 1, 8);   

cvShowImage("result1", mask);

The original Image I have shown is declared as mask and I used a clone image mask1 to find out contours. But after this what I get is the below shown image:-

image description

I used the same method in other sections of the program and got good results,but I dunno what happened in this case..

edit retag flag offensive close merge delete

Comments

If you get their contours, then computing the mean point of the points in the contour you'll get the center of each blob

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-07 04:04:51 -0600 )edit
3

please avoid using the arcane c-api, please !

berak gravatar imageberak ( 2016-01-07 23:22:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-01-07 05:04:14 -0600

After finding the contours with cv::findContours(), you can use this function to retrieve the centroid of a blob.

cv::Point calculateBlobCentroid(const std::vector<cv::Point>& blob)
{
    cv::Moments mu = cv::moments(blob);
    cv::Point centroid = cv::Point (mu.m10/mu.m00 , mu.m01/mu.m00);

    return centroid;
}
edit flag offensive delete link more

Comments

2

You do not need to compute the moments, just a mean of the coordinates will return the centroid (ex)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-07 06:00:59 -0600 )edit
1

However I am wondering if the moment calculation is that heavy compared to the mean calculation?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-08 02:51:24 -0600 )edit

I've never tried the mean approach, but I use this method quite heavily in embedded systems and it doesn't seem to cause any strain on the weak CPU.

Pedro Batista gravatar imagePedro Batista ( 2016-01-08 04:19:18 -0600 )edit
1

What about the moments? I proposed the mean because when computing moments you compute more thing, not just the center of blobs, but I may loop some optimization hidden steps that make it faster than computing the mean :)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-08 05:04:02 -0600 )edit
2

@thdrksdfthmn be careful because mean gives geometric center and is same compared to moment only with BW images. More general using moment you have center of mass using mean of pixel coordinates you have a geometric center

pklab gravatar imagepklab ( 2016-01-08 11:35:56 -0600 )edit

Yes, you are right, but I do not see any word that describes the center of mass of the blobs

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 02:26:08 -0600 )edit

Yes, you are right :=) ...center of mass would be useful for other users

pklab gravatar imagepklab ( 2016-01-11 04:37:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-07 04:02:02 -0600

Seen: 4,930 times

Last updated: Jan 07 '16