更新:使用轮廓更新我的解决方案。你可以找到解决方案
在下面使用霍夫圆。
使用轮廓法。
我今天再次尝试寻找轮廓来标记管道。结果我得到了轮廓。我已经根据轮廓长度和面积过滤了结果。但是您可以根据您拥有的图像应用更多约束。似乎我已经过度拟合了这张图片的解决方案,但这是我唯一可以访问的图片。您还可以使用 laplacian/canny 代替自适应阈值。希望这会有所帮助:)
import cv2 as cv2
img_color = cv2.imread('yNxlz.jpg')
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(img_gray, (5, 5), 0)
thresh = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY_INV,11,2)
contours,hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours
contour_list = []
for contour in contours:
approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)
area = cv2.contourArea(contour)
# Filter based on length and area
if (7 < len(approx) < 18) & (900 >area > 200):
# print area
contour_list.append(contour)
cv2.drawContours(img_color, contour_list, -1, (255,0,0), 2)
cv2.imshow('Objects Detected',img_color)
cv2.waitKey(5000)
霍夫圆法
我尝试拍摄您的图像并应用霍夫圆(opencv)。我没有Java设置,因此我使用了python。这是我得到的代码和对应的结果。
在此之前,有一些技巧可以对此进行微调。
- 重要的是预处理,一个简单的 Gaussianblur 让我得到了很好的改进,所以使用高斯过滤器大小。
- 既然您已经知道管道半径/直径,请利用该信息。也就是说,在 Houghcircles 中使用 minradius 和 maxradius 参数。
- 如果您知道管道之间的最小距离,您也可以使用 mindist param。
- 如果您知道可能存在管道的区域,则可以忽略在该区域以外检测到的误报管道。
希望这会有所帮助:)
我使用的代码
import cv2 as cv2
img_color = cv2.imread('yNxlz.jpg')
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (7, 7), 0)
#Hough circle
circles = cv2.HoughCircles(img_gray, cv2.cv.CV_HOUGH_GRADIENT, 1, minDist=15,
param1=50, param2=18, minRadius=12, maxRadius=22)
if circles is not None:
for i in circles[0, :]:
# draw the outer circle
cv2.circle(img_color, (i[0], i[1]), i[2], (0, 255, 0), 2)
# draw the center of the circle
cv2.circle(img_color, (i[0], i[1]), 2, (0, 0, 255), 3)
cv2.imwrite('with_circles.png', img_color)
cv2.imshow('circles', img_color)
cv2.waitKey(5000)
这是我得到的结果。