简体   繁体   English

使用OpenCV仅检测圈子

[英]Detect Only Circles using OpenCV

I am using the following code to detect the circles only . 我使用以下代码仅检测圆圈。 But it is also detecting the other shapes . 但它也检测其他形状。 Please help to do this . 请帮忙做到这一点。 I have used the HoughCircles but it is not giving the Good results. 我使用过HoughCircles,但它没有给出好结果。 My requirement is have to detect the circles only . 我的要求是必须只检测圆圈。

Mat src, src_gray;

/// Read the image
src = t2;

if(! src.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    cv::waitKey(5000);
}

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

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


Mat src_lines; Mat src_gray_lines;
int thresh_lines = 100;
RNG rng_lines(12345);

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

src_gray_lines = src_gray;
/// Detect edges using Threshold
threshold( src_gray_lines, threshold_output, thresh_lines, 255, THRESH_BINARY );
/// Find contours
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() );

for( size_t i = 0; i < contours.size(); i++ )
{
    minRect[i] = minAreaRect( Mat(contours[i]) );
}

/// Draw contours + rotated rects + ellipses
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
    // rotated rectangle
    Point2f rect_points[4];
    minRect[i].points( rect_points );
    for( int j = 0; j < 4; j++ )
        line( src, rect_points[j], rect_points[(j+1)%4], Scalar(255,0,0), 1, 8 );
}

Please let me know if my question is not clear . 如果我的问题不明确,请告诉我。

You have contour vectors so you can easily check their length. 你有轮廓矢量,所以你可以很容易地检查它们的长度。 Circle has also area. 圈子也有区域。 Circle shape should have specific ratio area to length (you should compute what this ratio should be). 圆形形状应具有特定的面积与长度比(您应该计算该比率应该是多少)。 Now eliminating shapes which does not fit this ratio (with some delta) you are getting only circles. 现在消除不适合这个比例的形状(有一些三角形)你只得到圆圈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM