好了,通过您在问题中提供的代码,我们获得了除我们想要获得的那些行之外的其他行.因此,我们可以将其放大并填充线条.
img = cv2.imread('lines.png')
kernel = np.array([[0, -1, 0],
[1, 0, 1],
[0, -1, 0]])
dst = cv2.filter2D(img, -1, kernel)
kernel = np.ones((5, 5), np.uint8)
dilated = cv2.dilate(dst, kernel, iterations = 1)
然后,我们需要以45度去除线条上方的点,因此我们为此使用了形态学开口,并对图像进行阈值处理以将所有线条转换为像素值= 255.
kernel = np.ones((7, 7), np.uint8)
opening = cv2.morphologyEx(dilated, cv2.MORPH_OPEN, kernel)
_,thresh = cv2.threshold(opening,10,255,cv2.THRESH_BINARY)
然后使用原始图像的cv2.bitwise_and和获得的阈值的cv2.bitwise_not,我们获得了线.
res = cv2.bitwise_and(img, cv2.bitwise_not(thresh))
我们获得了线条,但需要删除中间的圆圈.为此,我们在原始图像上使用cv2.erode仅获得中间圆,对其设置阈值,然后再次使用cv2.bitwise_and和cv2.bitwise_not将其从res中删除.
kernel = np.ones((7, 7), np.uint8)
other = cv2.erode(img, kernel, iterations = 1)
_,thresh = cv2.threshold(other,10,255,cv2.THRESH_BINARY)
result = cv2.bitwise_and(res, cv2.bitwise_not(thresh))
cv2.imshow("Image", result)
cv2.waitKey(0)
cv2.destroyAllWindows()