我有一个图像如下:

python - 使用OpenCV查找图像中的圆圈数-LMLPHP

谁能告诉我如何检测其中的圆数。我正在使用霍夫圆变换来实现这一点,这是我的代码:

# import the necessary packages
import numpy as np
import sys
import cv2



# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(str(sys.argv[1]))
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 5)

no_of_circles = 0
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    no_of_circles = len(circles)

# loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

# show the output image

    cv2.imshow("output", np.hstack([image, output]))

print 'no of circles',no_of_circles

我收到此代码的错误答案。有人可以告诉我我哪里出错了吗?

最佳答案

我尝试了一种技巧来检测所有圈子。

我手动找到了HoughCircles参数

HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, 50, 40, 46, 0, 0 );

棘手的部分是
flip( src, flipped, 1 );
hconcat( src,flipped, flipped );
hconcat( flipped, src, src );

flip( src, flipped, 0 );
vconcat( src,flipped, flipped );
vconcat( flipped, src, src );

flip( src, src, -1 );

在检测之前将创建如下所示的模型。

python - 使用OpenCV查找图像中的圆圈数-LMLPHP

结果是这样的

python - 使用OpenCV查找图像中的圆圈数-LMLPHP

C++代码可以轻松转换为python
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat src, src_gray, flipped, display;

    if (argc < 2)
    {
        std::cerr<<"No input image specified\n";
        return -1;
    }

    // Read the image
    src = imread( argv[1], 1 );

    if( src.empty() )
    {
        std::cerr<<"Invalid input image\n";
        return -1;
    }

    flip( src, flipped, 1 );
    hconcat( src,flipped, flipped );
    hconcat( flipped, src, src );

    flip( src, flipped, 0 );
    vconcat( src,flipped, flipped );
    vconcat( flipped, src, src );

    flip( src, src, -1 );

    // Convert it to gray
    cvtColor( src, src_gray, COLOR_BGR2GRAY );

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

    // will hold the results of the detection
    std::vector<Vec3f> circles;
    // runs the actual detection
    HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, 50, 40, 46, 0, 0 );

    // clone the colour, input image for displaying purposes
    display = src.clone();
    Rect rect_src(display.cols / 3, display.rows / 3, display.cols / 3, display.rows / 3 );
    rectangle( display, rect_src, Scalar(255,0,0) );
    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]);

        Rect r = Rect( center.x-radius, center.y-radius, radius * 2, radius * 2 );
        Rect intersection_rect = r & rect_src;
        if( intersection_rect.width * intersection_rect.height >  r.width * r.height / 3 )
        {
            // circle center
            circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 );
            // circle outline
            circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 );
        }

    }

    // shows the results
    imshow( "results", display(rect_src));
    // get user key
    waitKey();

    return 0;
}

关于python - 使用OpenCV查找图像中的圆圈数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34216130/

10-12 21:20