【问题标题】:Plot Centroid of Specific Blob in C++ OpenCV在 C++ OpenCV 中绘制特定 Blob 的质心
【发布时间】:2015-07-20 06:01:34
【问题描述】:

我正在尝试绘制使用轮廓技术检测到的特定斑点的质心。我不希望遍历图像中检测到的所有斑点 - 我只想绘制一个的质心(即轮廓 [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, double)' 错误。

【问题讨论】:

  • 你是如何试图描绘这一点的?看起来这不是您的代码的一部分,即使它是给您错误的部分。请显示错误的完整堆栈跟踪并将您的代码更改为MCVE
  • 检查编辑 - 现已更改
  • docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/moments/… 你读过这个吗?尝试找到轮廓 ID 2 的矩和质心。
  • 是的,我有,但是一旦我指定轮廓 ID,我就会得到同样的错误。
  • {avg_x, avg_y} 不构造 cv::Point。你需要:Point(avg_x, avg_y)

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


【解决方案1】:

使用最小包围圈

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



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

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 2011-02-08
    • 2014-03-29
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多