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_Detection_Shapes - Fatal编程技术网

Python 我可以在OpenCV中做些什么来更好地识别圆?

Python 我可以在OpenCV中做些什么来更好地识别圆?,python,opencv,detection,shapes,Python,Opencv,Detection,Shapes,所以这是我的问题,我需要能够识别和计算一个项目的形状,我有一个小困难。我刚刚开始使用OpenCV,没有找到一种更好的方法来识别圆,而不使用else命令。例如,如果帧中有任何其他形状不是其他形状之一,它会将其检测为一个圆,理想情况下,我希望能够忽略帧中的这些对象。我不确定这是否会影响任何东西,但在这部分代码之前,我使用自适应阈值和高斯模糊来帮助提高处理能力 TLDR:我试图找到一种不用else语句识别圆圈的方法 def detect(self, c): # initialize the s

所以这是我的问题,我需要能够识别和计算一个项目的形状,我有一个小困难。我刚刚开始使用OpenCV,没有找到一种更好的方法来识别圆,而不使用else命令。例如,如果帧中有任何其他形状不是其他形状之一,它会将其检测为一个圆,理想情况下,我希望能够忽略帧中的这些对象。我不确定这是否会影响任何东西,但在这部分代码之前,我使用自适应阈值和高斯模糊来帮助提高处理能力

TLDR:我试图找到一种不用else语句识别圆圈的方法

def detect(self, c):
    # initialize the shape name and approximate the contour
    shape = "unidentified"
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)

    # if the shape is a triangle, it will have 3 vertices
    if len(approx) == 3:
        shape = "triangle"
        self.s2 = self.s2 + 1

    # if the shape has 4 vertices, it is either a square or
    # a rectangle
    elif len(approx) == 4:
        # compute the bounding box of the contour and use the
        # bounding box to compute the aspect ratio
        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)

        # a square will have an aspect ratio that is approximately
        # equal to one, otherwise, the shape is a rectangle
        if 0.75 <= ar <= 1.35:
            shape = "square"
            self.s3 = self.s3 + 1
        else:
            shape = "rectangle"
            self.s4 = self.s4 + 1

    # otherwise, we assume the shape is a circle
    else:
        shape = "circle"
        self.s5 += 1

我通过重新组织代码并添加

if shape == "circle":
    circles = cv2.HoughCircles(thresh,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
    if circles is not None:
        s5 += 1

到代码的新区域。谢谢大家的帮助。

你好,你试过什么?我很快查看了google,第一个结果是一篇文章,描述了如何用代码示例实现这一点。出了什么问题?不使用else语句为什么?您可以显示示例图像吗?一种常见的方法是测试是否真的存在一些类似边缘的结构,在那里你需要一个圆形轮廓。Reportgunner,问题是我不知道如何将从HoughCircles生成的轮廓与在我的图像中找到的轮廓进行比较。我有OpenCV的基本知识,目前只能理解这么多文档。Miki,我之所以需要它,是因为如果代码中有非预定义形状的东西,它将不会被检测为圆。米卡,这是一个完美的代码环境:@mika某种可以交叉检查形状的东西,这样就可以用其他东西检查一个圆。那将是理想的。