OpenCV calculate center of mass for a single contour
It seem like you can definitely merge the two loops together.

Also you should avoid iterating using i, instead, use iterators, or the range-based for loop (since C++11).

// if you needed this vector
mu.reserve(contours.size());

// Get the mass centers:
std::vector&lt;cv::Point2f&gt; mc( contours.size() );
for( auto&amp; c : contours )
{
  auto m = moments(c, false); 
  mu.push_back(m); // only if you need the mu vector
  mc.push_back(cv::Point2f( m.m10/m.m00 , m.m01/m.m00 )); 
}


And this can even be simplified to a call to std::transfer with std::back_inserter:

std::transform(
  std::begin(contours),
  std::end(contours),
  std::back_inserter(mc),
  [](const contour&amp; c) -&gt; cv::Point2f {
    auto m = moments(c, false);
    return cv::Point2f( m.m10/m.m00 , m.m01/m.m00 )
  }
);


And of course if you only need to know moments of one countour, there is no need to count it for all, but since you haven't included that part of the code, it's hard to tell anything about it.