OpenCV Hough Circle Transform 无法检测到大多数圆圈
OpenCV Hough Circle Transform doesn't detect most circles
我正在尝试使用以下代码在我的图像中检测尽可能多的圆圈:
maxRadius = int(1.2*(width/16)/2)
minRadius = int(0.9*(width/16)/2)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
circles = cv.HoughCircles(image=gray,
method=cv.HOUGH_GRADIENT,
dp=1.2,
minDist=2*minRadius,
param1=70,
param2=0.9,
minRadius=minRadius,
maxRadius=maxRadius
)
虽然它对某些图像有效,但也有一些例外情况无效。
下面我们可以看到,对于代表同一类实验的两张不同图像,我的算法会产生截然不同的结果。
我该如何解决这个问题?我应该先对图像应用某种滤镜以增强对比度吗?
编辑:添加原始图像:
enter image description here
此解决方案可能适用于其他图像,也可能不适用于其他图像,但它确实适用于您发布的图像。您可能希望在 adaptiveThreshold 和 HoughCricles 参数的“最佳点”上工作,以便它也适用于其他图像。
import numpy as np
import cv2
import matplotlib.pyplot as plt
rgb = cv2.imread('/path/to/your/image/cells_0001.jpeg')
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
imh, imw = gray.shape
th = cv2.adaptiveThreshold(gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,11,2)
maxRadius = int(1.2*(imw/16)/2)
minRadius = int(0.9*(imw/16)/2)
circles = cv2.HoughCircles(image=th,
method=cv2.HOUGH_GRADIENT,
dp=1.2,
minDist=2*minRadius,
param1=70,
param2=25,
minRadius=minRadius,
maxRadius=maxRadius
)
out_img = rgb.copy()
for (x, y, r) in circles[0]:
# draw the circle in the output image
cv2.circle(out_img, (x, y), int(r), (0, 255, 0), 1)
plt.imshow(out_img)
我正在尝试使用以下代码在我的图像中检测尽可能多的圆圈:
maxRadius = int(1.2*(width/16)/2)
minRadius = int(0.9*(width/16)/2)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
circles = cv.HoughCircles(image=gray,
method=cv.HOUGH_GRADIENT,
dp=1.2,
minDist=2*minRadius,
param1=70,
param2=0.9,
minRadius=minRadius,
maxRadius=maxRadius
)
虽然它对某些图像有效,但也有一些例外情况无效。
下面我们可以看到,对于代表同一类实验的两张不同图像,我的算法会产生截然不同的结果。
我该如何解决这个问题?我应该先对图像应用某种滤镜以增强对比度吗?
编辑:添加原始图像:
enter image description here
此解决方案可能适用于其他图像,也可能不适用于其他图像,但它确实适用于您发布的图像。您可能希望在 adaptiveThreshold 和 HoughCricles 参数的“最佳点”上工作,以便它也适用于其他图像。
import numpy as np
import cv2
import matplotlib.pyplot as plt
rgb = cv2.imread('/path/to/your/image/cells_0001.jpeg')
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
imh, imw = gray.shape
th = cv2.adaptiveThreshold(gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,11,2)
maxRadius = int(1.2*(imw/16)/2)
minRadius = int(0.9*(imw/16)/2)
circles = cv2.HoughCircles(image=th,
method=cv2.HOUGH_GRADIENT,
dp=1.2,
minDist=2*minRadius,
param1=70,
param2=25,
minRadius=minRadius,
maxRadius=maxRadius
)
out_img = rgb.copy()
for (x, y, r) in circles[0]:
# draw the circle in the output image
cv2.circle(out_img, (x, y), int(r), (0, 255, 0), 1)
plt.imshow(out_img)