我想检测图像中的圆圈,如下所示:
所以,我尝试在
python中使用
cv2.findContours
和 cv2.Houghcircles
,但出现了一些问题。
两种方法都是在图像处理后应用(Canny、阈值等)
首先,如果我使用
cv2.findCountours
,它无法检测外圆,因为它们重叠并检测为单个轮廓。
这是我的Python代码。
contours, hier = cv2.findContours(input, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
c= (random.randint(0,255),random.randint(0,255),random.randint(0,255))
if area>10000 and area<100000: # Filtering the target size
(x, y), radius = cv2.minEnclosingCircle(contour)
center= (int(x), int(y))
radius= int(radius)
cv2.circle(color, center, radius, c,5)
其次,如果我使用
cv2.Houghcircles
(分割半径范围来检测同心圆),结果会更好,但结果仍然很差。
for maxR in range(100,200,10): # minimum 100 to maximum 200 by step 10
circles = cv2.HoughCircles(input, cv2.HOUGH_GRADIENT, 1, 100, param1=100,param2=22,minRadius=minR,maxRadius=maxR)
minR+=10
# Check to see if there is any detection
if circles is not None:
# If there are some detections, convert radius and x,y(center) coordinates to integer
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
c= (random.randint(0,255),random.randint(0,255),random.randint(0,255))
cv2.circle(view, (x, y), r, c, 3) # draw circumference
cv2.rectangle(view, (x - 2, y - 2), (x + 2, y + 2), c, 5) # draw center
有没有更好的方法来检测图像中的同心圆并测量同心圆的中心坐标和半径?
我没有做太多分析或测试,但认为以下方法可能会产生一些合理的结果。
制作一个覆盖整个图像的“种子”点网格 - 我认为网格线应该小于您希望找到的最小圆的半径,以便我们保证每个圆中都有一个种子点
迭代网格点,对于每个网格点,从该位置开始用红色填充
删除图像中所有非红色的内容,然后检查红色区域的面积和圆形度,如果足够圆形且尺寸合适,则添加到列表中。
根据每个种子点是在单元格内还是在背景内,您会得到这样的结果:
我想您必须尝试洪水填充参数和圆度测量(可从
findContours()
获得)并过滤结果列表,因为您可能会两次检测到某些圆。您还需要避免从较暗的单元格边界开始进行洪水填充,因此也许您希望首先减少到 3 种颜色,这样您就只能得到浅白色背景、较深的灰色单元格边界和最暗的灰色边界。