我尝试在OpenCV中使用findCirclesgrid查找一个检测圆数组,但没有成功

2024-04-25 14:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检测下图中的圆圈阵列。 幸运的是,我在互联网上找到了一个python代码。然而,它不起作用,我不知道问题是什么。请让我知道好的解决方案,并介绍一个简单的方法来检测所有的圆阵列的图片

这是我在网上找到的代码

import numpy as np
import cv2 as cv

img = cv.imread('circles0.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, corners = cv.findCirclesGrid(gray, (4,9), None, flags = cv.CALIB_CB_ASYMMETRIC_GRID)
if ret == True:
    corners2 = corners
    # corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001))
    cv.drawChessboardCorners(img, (4,9), corners2, ret)
    cv.imwrite('output.jpg', img)
    cv.imshow('img', img)
    cv.waitKey(0)

enter image description here


Tags: 方法代码importimgas互联网解决方案cv
1条回答
网友
1楼 · 发布于 2024-04-25 14:33:56

您正在使用的代码是寻找一个4行9列的圆形直线网格

enter image description here

您使用的图像与该标准不匹配

在图像Hough Circle Transform中的任意位置查找圆可能是您对图像的最佳选择

试试下面的&;请注意cv2.HoughCircles()中的参数对图像非常敏感

import cv2
import numpy as np

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

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 70,
                           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,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)

输出

enter image description here

相关问题 更多 >