Click here to Skip to main content
12,957,007 members (58,837 online)
Rate this:
Please Sign up or sign in to vote.
See more:

does anyone know how to find the center of mass of an object in an image?
I'm working with C # and OpenCV

thank you!
Posted 13-Dec-12 5:29am
Sergey Alexandrovich Kryukov 13-Dec-12 14:12pm
Do you think "does anyone know" a question? Do you think it's a difficult task or that not many members have graduated from school?
arthur.rafa10 18-Dec-12 14:02pm
Hello to All!!
I will give more details of what I'm trying to do, to see if the question is clear. I have an image (70x50) containing handwritten characters (these characters are tuned, ie with only 1 pixel wide) and need to know how to find the center of mass of the character contained in the image. the image has a white background and character in this black color. any suggestions??
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This is a very simple task, I would say, for the middle school or something. Here is the solution:[^].

Interestingly, in your algorithm you first select some arbitrary point (close to center to get better accuracy), do the calculation relative to this point, and it gives you the real center of mass.

Also, it's not clear what do you mean by image and its center of mass. If the image is rectangle and all pixels has the same weight, this is simply a center of the rectangle. If pixels are weighted by transparency or colors, use the algorithm mentioned above. If this is some sort of vector graphics, you will need to use calculus instead of discreet summation, which is also pretty easy in most cases.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I don't think this is an easy task Sergey, perhaps the question is not clear enough. For the algorithm to find the center of a visual object,the first thing is to detect it, I can suggest using voting based methods such as generalized hough transform[^] or geometric hashing[^], each feature point should vote for the object center, the feature point can be a point on an edge detected by canny edge detector or SIFT key points, which one to use depends on the application. For simple images even segmentation using pixel grouping can find the center of the visual object.

In that case try using the methods cvFindContours and cvBoundingRect, the center of the of the bounding rectangle will give the approximate if not the exact center of your objects.

#include <iostream>
#include <vector>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv){

IplImage* img_in = cvLoadImage("your_image.jpg",1);

IplImage* img_working = cvCreateImage(cvGetSize(img_in), 8, 1);
cvCvtColor(img_in, img_working, CV_BGR2GRAY);
CvSeq* seq;
vector<CvRect> boxes;
CvMemStorage* storage = cvCreateMemStorage(0);
cvFindContours(img_working, storage, &seq, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(600,200));
CvRect boundbox ;
for(; seq; seq = seq->h_next) {
 boundbox = cvBoundingRect(seq);
return 0;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Assuming that the object has uniform density, I think you can pick a any point in the image and then take all the vectors from that point to all other points that fall in the object (the pixels that make up the object essentially). Then sum those vectors and divide them by the number of vectors and the center of mass should be at the start point plus the resulting vector.

Hope this helps,
Fredrik Bornander
Sergey Alexandrovich Kryukov 13-Dec-12 14:05pm
Well, sorry, but... think again. If you consider point-by-point calculation, uniform density is not required. Also: "divide by the number of vectors"? Isn't that obvious that this is wrong? This is a very simple task; come on, you can do better...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,924
CHill60 3,550
Maciej Los 3,312
ppolymorphe 2,120
Jochen Arndt 1,973

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100