Hough Circles,以及过于复杂的解决方案

时间:2014-02-09 01:18:25

标签: c++ opencv geometry computer-vision vision

所以我一直在努力识别Hough Circles的瑜伽球。现在,当转换为灰度时,它可以直接使用。不幸的是,我必须采取更复杂的程序,因为有多个这些彩球并且只想检测蓝色。

未过滤的球:

unfiltered ball

过滤球:

filtered ball

我的算法步骤:

  1. 从BGR转换为HSV
  2. 模糊图片
  3. 仅针对选择值过滤HSV(在我的情况下,由于照明,将深蓝色变为浅蓝色)
  4. 反转图像
  5. 使用形态填充被点亮的部分
  6. 再次模糊
  7. 过滤模糊,因此它是一个纯实的形状而不是无法识别的模糊灰度
  8. 用hough-circles检测。 MAT仍然是灰度,所以这不是问题。
  9. 代码:

    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <sstream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    using namespace std;
    using namespace cv;
    
    int main(int argc, char *argv[])
    {
        // Morphology stuff
        Mat element5(30, 30, CV_8U, Scalar(1));
        int morph_elem = 1; // 2
        int morph_size = 33;// 30
        int morph_operator = 2; // 2
        Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
        int const max_operator = 4;
        int const max_elem = 2;
        int const max_kernel_size = 21;
        Mat kernel;
        // Display Windows Name
        namedWindow("Testing Purposes", CV_WINDOW_AUTOSIZE);
        Mat src; // loaded image
        Mat hsv; // changed src into HSV
        Mat Filtered; // filtered w/ inRange for blue ball
        Mat Gray; // gray filter for src
        Mat dst; // destination for canny edge
        Mat detected_edges; // matrix of edges w/ canny
        // thresholds for canny
        int edgeThresh = 45;
        int lowThreshold;
        int const max_lowThreshold = 100;
        src = imread(argv[1]);
        cvtColor(src, Gray, CV_BGR2GRAY);
        cvtColor(src, hsv, CV_BGR2HSV);
        /*
        // CannyEdge Testing
        blur(Gray, detected_edges, Size(3, 3)); // blur the grayimage
        Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size);
        dst = Scalar::all(0);
        src.copyTo( dst, detected_edges);
        imshow(window_name,dst);
        */
        // hsv blur and then thresholds
        blur(hsv,hsv,Size(4, 4), Point(-1, -1));
        inRange(hsv, Scalar(100, 100, 0), Scalar(200, 200, 255), Filtered); //filtering after blur
        vector<Vec3f> circles; //vector for holding info on circles
        // houghcircles - attempts to detect circles in the Filtered image we passed it
    
        // morphology defintion for Kernel
        bitwise_not(Filtered, Filtered);
        // imwrite("/home/bjacobs/Desktop/Testing.jpg", Filtered);
        imwrite("/home/bjacobs/Desktop/Testingg.jpg", Filtered);
        morphologyEx(Filtered, dst, MORPH_OPEN, element);
        blur(dst, dst, Size(20, 20), Point(-1, -1));
        Mat baw = dst > 128;
        HoughCircles(baw ,circles, CV_HOUGH_GRADIENT, 1, baw.rows/8,200,100,0,0);
        imwrite("/home/bjacobs/Desktop/Testing.jpg", baw);
    
        // Draw the circles detected onto the SRC file
        for(size_t i = 0; i < circles.size(); i++)
        {
            Point center(cvRound(circles[i][0]), cvRound(circles[i][3]));
            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);
        }
        imwrite("/home/bjacobs/Desktop/Test.jpg", hsv);
        imshow("Testing Purposes", src);
        waitKey(0);
    }
    

    我已经尽可能多地在网上阅读了这个问题,到目前为止我发现的任何内容都没有帮助。请原谅评论,并且使用Canny Edge检测时会包含一些失败的算法,因此不要过多考虑它们。有谁知道这个检测问题的解决方案?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,而不是使用houghcircle。

  1. 细分蓝色。

  2. 查找轮廓(最大)。

  3. 轮廓的最小封闭圆。