代码
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>int main(int argc, char* argv[])
{cv::Mat input_image = cv::imread("Untitled.png", cv::IMREAD_GRAYSCALE);cv::Mat thr;cv::threshold(input_image, thr, 0, 255, cv::THRESH_BINARY_INV);cv::imshow("w", thr);cv::Moments m = cv::moments(thr, true);double centroid_x = m.m10 / m.m00;double centroid_y = m.m01 / m.m00;std::cout << "Centroid: (";std::cout << centroid_x << ", ";std::cout << centroid_y << ")" << std::endl;std::vector<cv::Mat> input3channel;input3channel.emplace_back(input_image);input3channel.emplace_back(input_image);input3channel.emplace_back(input_image);cv::Mat dst;cv::merge(input3channel, dst);circle(dst, cv::Point(centroid_x,centroid_y), 2, cv::Scalar(0,0,255), -1);cv::imshow("show", dst);cv::waitKey();return EXIT_SUCCESS;
}
结果
图1: w

图2:show
