Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detect circles using the openCV

Hi all,

I am using the following code to detect the circles but it is also detecting the other shapes . Please help me to detect the circles .

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 );

/*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 outline
    circle( src, center, radius, Scalar(255,0,0), 3, 8, 0 );
}*/

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 );
}

Thanks in advance .

Detect Only circles using the openCV

Hi all,

I am using the following code to detect the circles but it is also detecting the other shapes . Please help me to detect the circles .

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 );

/*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 outline
    circle( src, center, radius, Scalar(255,0,0), 3, 8, 0 );
}*/

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 );
}

Thanks in advance .