2017-02-17 75 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