What you get in your current code snippet is, of course, the centroid of your bounding box.
"If you have a bunch of points (2nd vector), you can get a centroid by averaging these points: create a point to add all the positions of other points and separate the components of this point using the accumulated positions for the total number of points." - George Profenzi mentions
This is really the right approach for the exact center of gravity of any given object in two-dimensional space.
On Wikipedia, we have some common forms for finding the centroid of an object. http://en.wikipedia.org/wiki/Centroid
Personally, I asked myself what I need from this program. Do I wish for a thorough but high-performance work, or do I want to make some approximations? I could even find an OpenCV function that handles this correctly and efficiently.
You don't have a working example, so I write this in pseudo-code on a simple 5-pixel example using a thorough method.
x_centroid = (pixel1_x + pixel2_x + pixel3_x + pixel4_x +pixel5_x)/5 y_centroid = (pixel1_y + pixel2_y + pixel3_y + pixel4_y +pixel5_y)/5 centroidPoint(x_centroid, y_centroid)
Looped for x pixels
Loop j times *sample (for (int i=0, i < j, i++))* { x_centroid = pixel[j]_x + x_centroid y_centroid = pixel[j]_x + x_centroid } x_centroid = x_centroid/j y_centroid = y_centroid/j centroidPoint(x_centroid, y_centroid)
Essentially, you have type vector outlines
vector<vector<point>>
in OpenCV 2.3. I believe that you have something similar in earlier versions, and you must go through each frame in your picture with the first index of this βdouble vectorβ and go through each pixel in the inner vector.
Here is a link to the outline function documentation http://opencv.itseez.com/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=contours#cv.DrawContours
note: you marked your question as visual C ++. I would suggest using C ++ syntax in OpenCV 2.3 instead of c. The first and good reason for using 2.3 is that it is more based on the class, which in this case means that the Mat class (instead of IplImage) is leaking memory. No need to write destruction commands all live day :)
Hope this clarifies your issue. Enjoy it.