Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello

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
Comments
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?
--SA
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??

This is a very simple task, I would say, for the middle school or something. Here is the solution:
http://en.wikipedia.org/wiki/Center_of_mass[^].

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.

—SA
 
Share this answer
 
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 http://www.cs.utexas.edu/~dana/HoughT.pdf[^] or geometric hashing http://graphics.stanford.edu/courses/cs468-01-winter/papers/wr-ghao-97.pdf[^], 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.

C++
#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);
cvClearMemStorage(storage);

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);
 boxes.push_back(boundbox);
}

cvWaitKey(0);

return 0;

}
 
Share this answer
 
v5
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
 
Share this answer
 
Comments
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...
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900