使用Python / OpenCV检测空心圆

时间:2015-07-11 12:21:02

标签: php python linux opencv imagemagick

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

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

Sample picture

此图片的坐标大致为x:285 y: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%

如何更改变量" circle"的输出?只显示一个圆圈,然后找到1个,如何删除不需要的空格和括号?

1 个答案:

答案 0 :(得分:0)

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