【发布时间】:2015-07-11 12:21:01
【问题描述】:
我有一张带有随机圆圈的图片,其中一个圆圈始终是打开的。圆圈的大小、位置和颜色每次都不同,但背景始终为白色。
我想找到以编程方式打开的圆的坐标。这是一个示例图片:
这张图片的坐标大致为 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 值确定找到哪个圆。我调整了我的代码,让它从一个相当高的值 120 开始迭代 param2 值。
当它找到一个圆圈时它会停下来。
# 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 是否检测到所有闭环和开环?
-
根据参数检测全部或部分或无,是的。
标签: php python linux opencv imagemagick