Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# OpenCV
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 13-Dec-12 5:29am
Comments
Sergey Alexandrovich Kryukov at 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 at 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
good
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:
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
  Permalink  
Rate this: bad
good
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 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.
 
#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;
 
}
  Permalink  
v5
Rate this: bad
good
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
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 470
1 Sergey Alexandrovich Kryukov 274
2 George Jonsson 265
3 Maciej Los 260
4 Peter Leow 200
0 Sergey Alexandrovich Kryukov 10,879
1 OriginalGriff 5,555
2 Maciej Los 5,534
3 CPallini 3,819
4 BillWoodruff 3,594


Advertise | Privacy | Mobile
Web01 | 2.8.141030.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2014
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