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

标签 python opencv image-processing computer-vision

我有一张图片如下:

Sample image with overlapping circles

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

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

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

enter image description here

结果是这样的

enter image description here

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/

相关文章:

python - 如何在 dataframe.set_value 中使用 "takeable=True"和索引器,尤其是多重索引

c++ - OpenCV 3 HOG 检测信心?

c++ - 用于无损压缩一系列屏幕截图的适当图像文件格式

image-processing - 从视频中提取中文字幕

java - opencv错误 "Error: org.opencv.objdetect.CascadeClassifier.CascadeClassifier_1(Ljava/lang/String;)J"

python - 测试矩阵是对称矩阵还是斜对称矩阵?

python - Bottle python 中的 Rebase 模板函数不会按应有的方式呈现模板

Python:断言我在 POSIX 上运行?

c++ - 如何将 boost.Asio 与 MJPEG 一起使用?

image-processing - 如何使用 opencv 2.4 python 2.7 和 numpy 将图像从 BGR 转换为 LAB