2011-08-12 99 views
2

我正在嘗試使用opencv檢測實心圓圈。來自opencv文檔的示例代碼看起來好像不能檢測純白色。我將如何修改該代碼以適用於純白色圓圈?你能解釋爲什麼它不適用於純白色圓圈?使用opencv檢測純白色圓圈HoughCircles

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <stdio.h> 

using namespace cv; 

/** @function main */ 
int main(int argc, char** argv) 
{ 
    Mat src, src_gray; 

    /// Read the image 
    src = imread(argv[1], 1); 

    if(!src.data) 
    { return -1; } 

    /// Convert it to gray 
    cvtColor(src, src_gray, CV_BGR2GRAY); 

    /// Reduce the noise so we avoid false circle detection 
    GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2); 

    vector<Vec3f> circles; 

    /// Apply the Hough Transform to find the circles 
    HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0); 

    /// Draw the circles detected 
    for(size_t i = 0; i < circles.size(); i++) 
    { 
     Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 
     int radius = cvRound(circles[i][2]); 
     // circle center 
     circle(src, center, 3, Scalar(0,255,0), -1, 8, 0); 
     // circle outline 
     circle(src, center, radius, Scalar(0,0,255), 3, 8, 0); 
    } 

    /// Show your results 
    namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE); 
    imshow("Hough Circle Transform Demo", src); 

    waitKey(0); 
    return 0; 
} 

我會發布圖片,但我沒有足夠的棧溢出街道信用尚未。抱歉!

回答

4

您應該首先提取邊緣。這是Hough Transform檢測到的。在HoughCircles之前添加一個cvCanny變換。

+2

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html官方教程有沒有Canny的代碼。 –