Php 使用Python/OpenCV检测开放圆

Php 使用Python/OpenCV检测开放圆,php,python,linux,opencv,imagemagick,Php,Python,Linux,Opencv,Imagemagick,我有一张随机圆圈的图片,其中一个圆圈总是打开的。圆圈的大小、位置和颜色每次都不同,但背景始终为白色 我想找到以编程方式打开的圆的坐标。以下是一张示例图片: 这张照片的坐标大约是x:285Y:70。以下是我的尝试: import numpy as np import argparse import cv2 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required = True, help = "Path t

我有一张随机圆圈的图片,其中一个圆圈总是打开的。圆圈的大小、位置和颜色每次都不同,但背景始终为白色

我想找到以编程方式打开的圆的坐标。以下是一张示例图片:

这张照片的坐标大约是x:285Y:70。以下是我的尝试:

import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
black = cv2.imread('black.png')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(black,contours,-1,(250,250,250),2)

newblack = cv2.cvtColor(black, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(newblack, cv2.cv.CV_HOUGH_GRADIENT, 1, 1,
              param1=42,
              param2=35,
              minRadius=15,
              maxRadius=50)

if circles is not None:
        circles = np.round(circles[0, :]).astype("int")

        for (x, y, r) in circles:
                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)

        cv2.imshow("output", np.hstack([image, output]))
        print circles
        cv2.waitKey(0)
快完成了

param2值确定找到哪个圆。我调整了我的代码,使其在param2值上迭代,从一个相当高的值120开始

当它找到一个圆时,它就停止了

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

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

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

str = 120

def findcircle( str ):
    circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 1,
    param1=42,
    param2=str,
    minRadius=10,
    maxRadius=100)

    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        print circles
        sys.exit()

    while circles is None:
        str = str-1
        findcircle(str)

findcircle(str)
成功率约为80-100%


如何更改变量“circles”的输出,使其在找到多个1的情况下仅显示一个圆,以及如何删除不需要的空格和括号?

您是否可以告诉我们您到目前为止在这方面做了哪些工作?OpenCV的文档提供了什么?尝试了cv2.findContours和cv2.HoughCircles,到目前为止两者的组合都没有成功。必须有一个更简单的方法来找到一个开放/部分圆?这就是我到目前为止所做的。你的HoughCircle是否检测到所有封闭和开放圆?取决于它检测到的参数全部或部分或无,是的。感谢你提供答案。我已经回滚了问题编辑,因为我们没有将问题标记为问题中已解决的问题,而且删除似乎删除了许多问题的原始上下文。你能在答案的主体部分解释一下它是如何解决问题的吗?可以再编辑一些问题,但要尽量使问题与这个答案匹配,就像你一个接一个地问他们一样。这使它更好地为未来的读者。谢谢
# import the necessary packages
import numpy as np
import argparse
import cv2
import sys

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

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

str = 120

def findcircle( str ):
    circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 1,
    param1=42,
    param2=str,
    minRadius=10,
    maxRadius=100)

    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        index = [2]
        new_circles = np.delete(circles[0], index)
        print new_circles
        sys.exit()

    while circles is None:
        str = str-1
        findcircle(str)

findcircle(str)