#include <opencv2/opencv.hpp> // Include the appropriate OpenCV header
int main() {
// Define a vector of vector of cv::Point to store contours
std::vector<std::vector<cv::Point>> contours;
// Assuming you have filled the 'contours' vector with contour data
// Find the contour with the maximum area using a lambda function
std::vector<cv::Point> c = *std::max_element(contours.begin(), contours.end(), [](const std::vector<cv::Point>& a, const std::vector<cv::Point>& b) {
return cv::contourArea(a) < cv::contourArea(b);
});
// Calculate the moments of the contour
cv::Moments M = cv::moments(c);
// Calculate the centroid of the contour
int cx = static_cast<int>(M.m10 / M.m00); // X-coordinate of centroid
int cy = static_cast<int>(M.m01 / M.m00); // Y-coordinate of centroid
// Display the centroid coordinates
std::cout << "Centroid X: " << cx << ", Y: " << cy << std::endl;
return 0;
}
This code will find the contour with the largest area, calculate its moments, and then calculate the centroid using the moment values. The centroid coordinates are then displayed. Make sure to include the correct OpenCV header and library paths for your development environment.