检测openCV中的圆圈

检测openCV中的圆圈

本文介绍了检测openCV中的圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为 HoughCircles 函数选择正确参数时遇到问题.我尝试从视频中检测圆圈.这个圆圈是我做的,尺寸几乎一样.问题是相机在移动.

当我改变 maxRadius 时,它仍然以某种方式检测到更大的圆圈(见右图).我也尝试更改 param1, param2 但仍然没有成功.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)模糊 = cv2.medianBlur(gray, 25)#cv2.bilateralFilter(gray,10,50,50)最小距离 = 100参数 1 = 500param2 = 200#较小的值->更多虚假圈子最小半径 = 5最大半径 = 10圆圈 = cv2.HoughCircles(模糊,cv2.HOUGH_GRADIENT,1,minDist,param1,param2,minRadius,maxRadius)如果圆圈不是无:圆圈 = np.uint16(np.around(circles))对于我在圈子[0,:]:cv2.circle(blurred,(i[0], i[1]), i[2], (0, 255, 0), 2)

也许我使用了错误的功能?

解决方案

您代码中的主要问题是 HoughCircles 函数的第 5 个参数.

根据

I have a problem with choosing right parameters for HoughCircles function. I try to detect circles from video. This circles are made by me, and has almost the same dimension. Problem is that camera is in move.

When I change maxRadius it still detect bigger circles somehow (see the right picture). I also tried to change param1, param2 but still no success.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  blurred = cv2.medianBlur(gray, 25)#cv2.bilateralFilter(gray,10,50,50)


  minDist = 100
  param1 = 500
  param2 = 200#smaller value-> more false circles
  minRadius = 5
  maxRadius = 10
  circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1, param2, minRadius, maxRadius)

  if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(blurred,(i[0], i[1]), i[2], (0, 255, 0), 2)

Maybe Im using wrong function?

解决方案

The main problem in your code is 5th argument to HoughCircles function.

According to documentation the argument list is:

cv2.HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) → circles

That means the 5th argument applies circles (it gives an option getting the output by reference, instead of using the returned value).

Because you are not passing circles argument, you must pass named arguments for all arguments after the 4th argument (like param1=param1, param2=param2....).

Parameter tuning issues:

  • Reduce the value of param1.param1 is the higher threshold passed to the Canny.
    In your case value should be about 30.
  • Reduce the value of param2The documentation not so clear, but setting the value around 50 works.
  • Increase maxRadius value - radius 10 is much smaller than the radius of your circles.

Here is the code:

import numpy as np
import cv2

img = cv2.imread('circles.png')

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

blurred = cv2.medianBlur(gray, 25) #cv2.bilateralFilter(gray,10,50,50)

minDist = 100
param1 = 30 #500
param2 = 50 #200 #smaller value-> more false circles
minRadius = 5
maxRadius = 100 #10

# docstring of HoughCircles: HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)

# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Result:

这篇关于检测openCV中的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:19