Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用OpenCV查找图像中的圆数_Python_Opencv_Image Processing_Computer Vision - Fatal编程技术网

Python 使用OpenCV查找图像中的圆数

Python 使用OpenCV查找图像中的圆数,python,opencv,image-processing,computer-vision,Python,Opencv,Image Processing,Computer Vision,我的图片如下: 有谁能告诉我如何检测其中的圆数。我正在使用Hough圆变换来实现这一点,这是我的代码: # 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 = ima

我的图片如下:

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

# 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需要一些参数调整才能正常工作。
这可能是因为在您的情况下,Param1和Param2的默认值(设置为100)不好。

您可以通过计算最终腐蚀值,使用Houghcirle微调检测。它将给出图像中的圆圈数。

如果输入上只有圆圈和背景,则可以计算连接组件的数量,并忽略与背景关联的组件。这将是最简单、最可靠的解决方案

我尝试了一种巧妙的方法来检测所有圆圈

我手动找到了
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 );
将在检测前创建一个如下所示的模型

结果是这样的

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;
}
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{
Mat src,src_灰色,翻转,显示;
如果(argc<2)
{

std::cerr这篇文章描述了半圆的检测,可能对你来说是一个好的开始:

如果你陷入OpenCV,试着自己编写解决方案。为你的特定应用程序编写一个参数化的Hough circle finder相对简单。如果你编写了几次特定于应用程序的Hough算法,你应该能够在更短的时间内编写出一个合理的解决方案,而不是在谷歌上进行排序结果,破译别人的密码,等等

对于这样的图像,您肯定不需要精明的边缘检测,但这不会造成伤害

其他库(特别是商业库)将允许您为Hough circle查找设置更多参数。我希望Hough circle函数会有一些重载,以允许传入搜索参数的结构,包括允许的最小圆完整性百分比(弧长)


虽然学习RANSAC和Hough技术是件好事——随着时间的推移,学习更多的奇异技术也是件好事——但当你把圆定义得如此清晰清晰时,我不一定推荐使用RANSAC。在没有提供具体证据的情况下,我只会说,摆弄RANSAC参数可能不如摆弄Hough参数直观s、

你能画出检测到的圆并发布结果图像吗?重叠的圆是否总是有不同的颜色?输入时是否只有圆?测试:HoughCircles不会在图像边界处获得半圆。如果你的图像总是与发布的图像相似,简单的RANSAC圆检测可能非常好,因为nny边非常完美,完全没有噪音。输入中总是有圆圈。圆圈可能/可能不会相互接触。重叠的圆圈可能/可能没有相同的颜色。请详细说明一下。我如何微调参数?最终腐蚀后的中心数将给出数字(假设为N)必须在图像中找到的圆的数目。然后,可以使用敏感参数启动HoughCircle,以捕获更多的圆。最后,保留N个最可能的圆。如果有两个相同颜色的接触圆,则连接的组件将无法正常工作。Hough算法将识别两个接触圆。我明确地问过,触摸圆是否总是不同的颜色,OP没有回答,这就是为什么我建议了一个更简单的解决方案。如果真的有相同颜色的重叠圆,你可以检查每个连接的组件的凸面缺陷的数量。我明白你的观点,并同意简单的愿望,但是ming表示,至少有一些要求尚未说明(这是常见的),我认为最好提供一种将圆作为圆的解决方案。对此无可争辩:)如果您在图像+8个镜像图像上操作,则会出现问题,除非边界上的圆正好是半圆。最好编写一种检测圆弧的算法。