简体   繁体   English

使用Python / OpenCV检测开环

[英]Detect open circle with Python / OpenCV

I have a picture with random circles shown of which one circle is always open. 我有一张带有随机圆的图片,其中始终有一个圆是开放的。 The size, position and color of the circles are different each time but the background is always white. 圆圈的大小,位置和颜色每次都不同,但背景始终为白色。

I want to find the coordinates of the circle which is open programmatically. 我想找到以编程方式打开的圆的坐标。 Here is a sample picture: 这是一个示例图片:

样本图片

This picture roughly has coordinates of x:285 y:70. 该图片的坐标大致为x:285 y:70。 Here is my attempt: 这是我的尝试:

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)

Almost finished! 就快结束了!

The param2 Value determines which circle is found. param2值确定找到哪个圆。 I tweaked my code so it iterates over param2 values starting with a rather high value of 120. 我对代码进行了调整,以使其遍历param2值(从120的较高值开始)。

When it finds a circle it stops. 当找到一个圆圈时,它停止。

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

The success rate is around 80-100% 成功率在80-100%左右

How do change the output of the variable "circles" to only show one circle incase more then 1 is found and how do I remove unwanted spaces and brackets? 如何将变量“ circles”的输出更改为仅在发现一个大于1的情况下仅显示一个圆圈,以及如何删除不需要的空格和括号?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM