尝试在openCV(Python)中使用HoughCircles检测所有圆 [英] Trying to detect all the circles with HoughCircles in openCV (python)

查看:105
本文介绍了尝试在openCV(Python)中使用HoughCircles检测所有圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程: https://imgur.com/BrPB5Ox

绘制的圆形: https://imgur.com/dT7k29E

我的代码:

  import cv2将numpy导入为npimg = cv2.imread('Photos/board.jpg')输出= img.copy()灰色= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#检测图像中的圆圈圈子= cv2.HoughCircles(灰色,cv2.HOUGH_GRADIENT,1.2,100)#确保至少找到了一些圈子如果圈子不是无:#将圆的(x,y)坐标和半径转换为整数circle = np.round(circles [0,:]).astype("int")#遍历圆的(x,y)坐标和半径对于(x,y,r)以圆圈为单位:#在输出图像中绘制圆,然后绘制一个矩形#对应于圆心cv2.circle(输出,(x,y),r,(0,255,0),4)cv2.rectangle(输出,(x-5,y-5),(x + 5,y + 5),(0,128,255),-1)#显示输出图像cv2.imshow(输出",np.hstack([img,输出]))cv2.waitKey(0) 

非常感谢.

解决方案

首先,您不能期望 HoughCircles 检测到不同类型情况下的所有圆圈.它不是AI.根据获得期望的结果,它具有不同的参数.您可以在此处进行了解,以了解有关这些参数的更多信息./p>

HoughCircles 是基于轮廓的函数,因此您应确保正确检测到轮廓.在您的示例中,我确信由于光照问题会出现不良的轮廓结果.金属材料会在图像处理过程中引起光爆炸,严重影响轮廓的寻找.

您应该做什么:

  • 解决照明问题
  • 确保使用 HoughCircle 参数以获得所需的输出
  • 代替使用 HoughCircle ,您可以检测每个轮廓及其质心(时刻可帮助您找到他们的重心).然后,如果等高点等于该点的一个圆,则可以测量到该质心的每个轮廓点的长度.

I am following this tutorial: https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

I was playing around with the parameters ( even those you don't see in the code ex: param2) of HoughCircles and it seems very innacurate, in my project, the disks you see on the picture will be placed on random spots and i need to be able to detect them and their color.

Currently i am only able to detect few circles, and sometimes some random circles are drawn where there is no circles so i am a bit confused.

Is this the best way to do circle detection with openCV or is there a more accurate way of doing it ? Also why is my code not detecting every circles ?

Initial board : https://imgur.com/BrPB5Ox

Circle drawn : https://imgur.com/dT7k29E

My code :

import cv2
import numpy as np


img = cv2.imread('Photos/board.jpg')
output = img.copy()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        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([img, output]))
    cv2.waitKey(0)

Thanks a lot.

解决方案

First of all you can not expect HoughCircles to detect all circles in different type of situations. It is not an AI. It has different parameters according to get desired results. You can check here to learn more about those parameters.

HoughCircles is a contour based function so you should be sure the contours are being detected properly. In your example I am sure bad contour results will come up because of the lighting problem. Metal materials cause light explosion in image processing and this affects finding contours badly.

What you should do:

  • Solve the lighting problem
  • Be sure about the HoughCircle parameters to get desired output
  • Instead of using HoughCircle you can detect each contour and their mass center ( moments help you to find their mass center). Then you can measure each length of contour points to that mass center if all equal then its a circle.

这篇关于尝试在openCV(Python)中使用HoughCircles检测所有圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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