霍夫圆,以及过于复杂的解决方案

问题描述 投票:0回答:1

所以我一直致力于用霍夫圆来识别瑜伽球。现在,当转换为灰度时,它可以立即工作。不幸的是,由于有多个这些彩球,我必须采取更复杂的程序,并且只想检测蓝色。

未过滤的球:

unfiltered ball

过滤球:

filtered ball

我的算法步骤:

  1. 从 BGR 转换为 HSV
  2. 模糊图像
  3. 仅筛选选定值的 HSV(在我的例子中,由于光照原因,深蓝色变为浅蓝色)
  4. 反转图像
  5. 使用形态学来填充被照亮的部分
  6. 再次模糊
  7. 过滤模糊,使其成为实体形状,而不是无法识别的模糊灰度
  8. 用霍夫圆检测。 MAT 仍然是灰度,所以这不是问题。

代码:

#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 检测时包含一些失败的算法,所以不要太在意它们。有谁知道这个检测问题的解决方案吗?

c++ opencv computer-vision geometry
1个回答
0
投票

您可以执行以下操作,而不是使用霍夫圆。

  1. 分割蓝色。

  2. 找到轮廓(最大)。

  3. 轮廓的最小外接圆。

© www.soinside.com 2019 - 2024. All rights reserved.