当内部有圆圈时,霍夫算法找不到外圆

时间:2015-10-18 11:16:17

标签: c++ qt opencv image-processing

我使用hough变换在我的图片中找到圆圈,但它没有找到外圆。(我使用opencv 2.4.2和QT 3.3.0)

我的代码:

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

using namespace cv;
using namespace std;

int  HoughCircle1()
{
    Mat src,dst, src_gray;

    /// Read the image
    src = imread("E:/imagesForImgProc/test.jpg",CV_LOAD_IMAGE_COLOR);
    if( !src.data )
    {
        return -1;
    }

    /// Reduce the size of image
    Size size(src.cols/2,src.rows/2);
    resize(src, dst,size, 0, 0,  CV_INTER_LINEAR);

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

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

    /// Apply the Hough Transform to find the circles
    vector<Vec3f> 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( dst, center, 3, Scalar(0,255,0), -1, 8, 0 );
       /// circle outline
       circle( dst, center, radius, Scalar(0,0,255), 1, 8, 0 );
   }

   /// Show your results
   namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
   imshow( "Hough Circle Transform Demo", dst );
   return 0;
}

int main()
{
    HoughCircle1();
    waitKey(0);
    return 0;
}

“原始图片” Original image

“代码结果” Code result

“通缉结果” Wanted result

0 个答案:

没有答案