2017-02-17 98 views
-1

如何检测圆圈并计算此图像中的数量。我是新来打开cv和c + +。任何人都可以帮助解决这个问题。我试着用圆圈。但没有奏效。如何在C++中使用opencv 3检测图像中的圆形

镂空二进制图像如下。

input image你必须找到所有的轮廓在图像的

+0

我觉得你的问题太研究者广阔d。 –

+0

**查找轮廓** - > **拟合椭圆** –

+1

霍夫圆圈不起作用duh ....首先没有圆圈。你唯一的赌注是近似值,使用省略号 –

回答

0
  1. 一(见函数CV :: findContours)。
  2. 您必须分析这些轮廓(根据您的要求检查它)。

P.S.图中的数字绝对不是圆圈。所以我不能确切地说你如何检查收到的轮廓。

0

从这个图像开始(我去掉了边框):

enter image description here

你可以按照这个方法:

1)使用findContour得到的轮廓。

2)只保留内部轮廓。您可以这样做,检查由contourArea(..., true)返回的区域的标志。你会得到2个内部轮廓:

enter image description here

3)现在,你有两个轮廓,你可以找到一个圆圈minEnclosingCircle(蓝色),或适合椭圆fitEllipse(红色) :

enter image description here

这里供参考全码:

#include <opencv2/opencv.hpp> 
#include <vector> 

using namespace std; 
using namespace cv; 

int main() 
{ 
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE); 

    // Get contours 
    vector<vector<Point>> contours; 
    findContours(img, contours, RETR_TREE, CHAIN_APPROX_NONE); 

    // Create output image 
    Mat3b out; 
    cvtColor(img, out, COLOR_GRAY2BGR); 

    Mat3b outContours = out.clone(); 

    // Get internal contours 
    vector<vector<Point>> internalContours; 
    for (size_t i = 0; i < contours.size(); ++i) { 
     // Find orientation: CW or CCW 
     double area = contourArea(contours[i], true); 
     if (area >= 0) { 
      // Internal contour 
      internalContours.push_back(contours[i]); 

      // Draw with different color 
      drawContours(outContours, contours, i, Scalar(rand() & 255, rand() & 255, rand() & 255)); 
     } 
    } 

    // Get circles 
    for (const auto& cnt : internalContours) { 
     Point2f center; 
     float radius; 
     minEnclosingCircle(cnt, center, radius); 

     // Draw circle in blue 
     circle(out, center, radius, Scalar(255, 0, 0)); 
    } 

    // Get ellipses 
    for (const auto& cnt : internalContours) { 
     RotatedRect rect = fitEllipse(cnt); 

     // Draw ellipse in red 
     ellipse(out, rect, Scalar(0, 0, 255), 2); 
    } 

    imshow("Out", out); 
    waitKey(); 

    return 0; 
} 
+0

非常感谢你的善意考虑。我试着用你的代码。但是我得到这个。我无法找到错误。 :(“未处理的异常在0x000007FDA3294388(ucrtbase.dll)在ConsoleApplication1.exe中:一个无效的参数传递给一个函数,该函数认为无效的参数是致命的。”此问题发生在行findContours(img,轮廓,RETR_TREE,CHAIN_APPROX_NONE); –

+0

确保你正确加载了输入图像(我的答案中的一个,而不是你问题中的那个)......否则我不知道......这在我的电脑上完美地工作 – Miki

+0

是的,“右击(https://i.stack.imgur.com/7qN0z.png) – Miki