有几件事情需要考虑:
- 最好用合适的方法找到管道的范围(搜索网络)。
- 在第二步中,使用 PerspectiveTransform,最好消除图像的旋转,只将管道的范围移动到下一步。
- 下一步,从现在开始,您可以使用以下算法。
这不是一个完整的算法,不适用于所有测试用例。你必须花时间。阅读并结合不同的图像处理方法,甚至可能是机器学习;更改参数以获得更好的结果。
另一点是尽量保持环境条件不变。
import sys
import cv2
import numpy as np
# Load image
im = cv2.imread(sys.path[0]+'/im.jpeg')
H, W = im.shape[:2]
# Make a copy from image
out = im.copy()
# Make a grayscale version
gry = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# Make a clean black and white version of that picture
bw = cv2.adaptiveThreshold(
gry, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 12)
bw = cv2.medianBlur(bw, 3)
bw = cv2.erode(bw, np.ones((5, 5)))
bw = cv2.medianBlur(bw, 9)
bw = cv2.dilate(bw, np.ones((5, 5)))
# Draw a rectangle around image to eliminate errors
cv2.rectangle(bw, (0, 0), (W, H), 0, thickness=17)
# Count number of pipes
cnts, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# Change channels of black/white image
bw = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR)
# Draw position of pipes
c = 0
for cnt in cnts:
c += 1
x, y, w, h = cv2.boundingRect(cnt)
cv2.circle(out, (x+w//2, y+h//2), max(w, h)//2, (c, 220, 255-c), 2)
if c >= 255:
c = 0
# Count and print number of pipes
print(len(cnts))
# Save output images
cv2.imwrite(sys.path[0]+'/im_out.jpg', np.hstack((bw, out)))