在C ++ OpenCV中绘制特定Blob的质心图

时间:2015-07-20 06:01:35

标签: c++ arrays opencv image-processing centroid

我试图绘制使用轮廓技术检测到的特定斑点的质心。我不想遍历图像中检测到的所有斑点 - 我只想绘制一个质心(即轮廓[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)绘制圆形会导致构造函数没有&#34; cv :: Point_&lt; Tp&gt; :: Point [with_Tp = INT]&#34;匹配参数列表 - 参数类型是:(double,double)&#39;错误。

1 个答案:

答案 0 :(得分:0)

使用min around circle circle

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



cv::circle(gray, center, 5, CV_RGB(5, 100, 100), 5);