0

我正在尝试绘制使用轮廓技术检测到的特定斑点的质心。我不希望遍历图像中检测到的所有斑点 - 我只想绘制一个的质心(即轮廓 [2])。理想情况下,我想使用最有效/最快的方法来完成此任务。

这是我的代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
    cv::Mat src = cv::imread("frame-1.jpg");
    if (src.empty())
        return -1;

    cv::Mat gray;
    cv::cvtColor(~src, gray, CV_BGR2GRAY);

    cv::threshold(gray, gray, 160, 255, cv::THRESH_BINARY);

    // Find all contours
    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

    // Fill holes in each contour
    cv::drawContours(gray, contours, -1, CV_RGB(255, 255, 255), -1);

    cout << contours.size();

    double avg_x(0), avg_y(0); // average of contour points
    for (int j = 0; j < contours[2].size(); ++j)
    {
       avg_x += contours[2][j].x;
       avg_y += contours[2][j].y;
    }

    avg_x /= contours[2].size();
    avg_y /= contours[2].size();
    cout << avg_x << " " << avg_y << endl;

    cv::circle(gray, {avg_x, avg_y}, 5, CV_RGB(5, 100, 100), 5);

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
    imshow("MyWindow", gray);

    waitKey(0);

    destroyWindow("MyWindow");

    return 0;
}

但是,使用坐标 (avg_x, avg_y) 绘制圆会导致“没有构造函数实例”cv::Point_< Tp>::Point [with_Tp=int]”与参数列表匹配 - 参数类型为:(double,双)'错误。

4

1 回答 1

0

使用最小封闭圆

float radius ;
Point2f center ;
minEnclosingCircle ( contours[i] , center , radius ) ; 



cv::circle(gray, center, 5, CV_RGB(5, 100, 100), 5);
于 2017-02-10T10:20:15.743 回答