使用openCV和HoughCircles无助地迷失了方向 [英] Helplessly lost with openCV and HoughCircles

查看:100
本文介绍了使用openCV和HoughCircles无助地迷失了方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在此处检测到该黑色圆圈.应该不太困难,但是由于某些原因,根据参数,我到处只能得到0个圆或大约500个圆.但是没有中间立场.感觉像我尝试了几个小时的争论,但绝对没有成功.使用HoughCircles和黑白图片是否有问题?肉眼看来,这项任务很简单,但是由于某种原因,这对计算机来说很难吗?

I'm trying to detect this black circle here. Shouldn't be too difficult but for some reason I just get 0 circles or approximately 500 circles everywhere, depending on the arguments. But there is no middle ground. Feels like I have tried to play with the arguments for hours, but absolutely no success. Is there a problem using HoughCircles and black or white picture? The task seems simple to a human eye, but is this difficult to the computer for some reason?

这是我的代码:

import numpy as np
import cv2

image = cv2.imread('temp.png')
output = image.copy()
blurred = cv2.blur(image,(10,10))

gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)


circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 20, 100, 600, 10, 100)


if circles is not None:

    circles = np.round(circles[0, :]).astype("int")
        print len(circles)

    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)

    show the output image
cv2.imshow("output", np.hstack([output]))
cv2.waitKey(0)

推荐答案

您的方法几乎没有小错误.

There are few minor mistakes in your approach.

这是我在文档中使用的代码:

Here is the code I used from the documentation:

img = cv2.imread('temp.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cimg1 = cimg.copy() 

circles = cv2.HoughCircles img,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,255,255),3)

cv2.imshow('detected circles.jpg',cimg)

joint = np.hstack([cimg1, cimg])  #---Posting the original image along with the image having the detected circle
cv2.imshow('detected circle and output', joint )

这篇关于使用openCV和HoughCircles无助地迷失了方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆