Reputation: 109
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
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
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
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?
If latter is the case, please elaborate the question in more detail.
Upvotes: 0