shahd
shahd

Reputation: 109

Finding the center of a contour using opencv and visual c++

I want to find the center of a contour without doing so much calculations. Is there a built in function for that in opencv?

Upvotes: 6

Views: 20775

Answers (3)

J.Zhao
J.Zhao

Reputation: 2330

hope this could help you. this link has python and C++ code snippet

std::vector<cv::Point> centers; 
for (int i=0; i<contours.size(); i++){
     cv::Moments M = cv::moments(contours[i]);
     cv::Point center(M.m10/M.m00, M.m01/M.m00);
     centers.push_back(center);
}

Upvotes: 1

berak
berak

Reputation: 39796

for the 'geometric center', get the boundingRect() of the contour, then:

   cx = br.x+br.width/2; cy = br.y+br.height/2; 

for the 'center of mass' get the moments() of the contour, then:

   cx = m.m10 / m.m00;   cy = m.m01 / m.m00;

Upvotes: 16

bosnjak
bosnjak

Reputation: 8614

Either you haven't done any research, or these questions already asked and answered here are not what you are asking:

centroid of contour/object in opencv in c?

OpenCV 2 Centroid

If latter is the case, please elaborate the question in more detail.

Upvotes: 0

Related Questions