使用opencv进行连接线检测

我正在尝试转换看起来像的图像:

变成一个看起来像的东西(我画了这个):

我想在图像中检测和绘制线条,粗/细部分被平滑以创建稍微均匀的宽度,如果图像中的像素相互接触,线条会重叠。(如果放大原始图像,您甚至可以看到细小的部分确实有像素连接它们)

我遇到的两个问题是:

  1. 原始图像中的像素的薄部分最终被分解。因此,转换后的图像的像素不会重叠它们在原始图像中的位置,从而在线条中产生间隙。
  2. 一些线足够接近,如果我扩大事物以尝试修复#1,检测到的线最终会在它们不应该重叠的地方相互重叠。

我目前正试图与 Canny/Hough 搞混以使其正常工作,但到目前为止还没有成功。

edges = cv2.Canny(img, 50, 200, None, 3)

hough_img = np.copy(img) * 0  # creating a blank to draw lines on
lines = cv2.HoughLinesP(img, rho=1, theta=np.pi / 180, threshold=35, lines=np.array([]),
                        minLineLength=3, maxLineGap=2)
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(hough_img, (x1, y1), (x2, y2), (255, 0, 0), 2)

回答

也许scikit-imagemedial_axis() 是你需要的:

from skimage.io import imread
from skimage.morphology import medial_axis

# Load image
im = imread('https://i.stack.imgur.com/cqgfc.png',as_gray=True)
skel= medial_axis(im)


或者,正如 Bart 在评论中的亲切建议,您可以使用skeletonize()

from skimage.io import imread, imsave
from skimage.morphology import skeletonize
from skimage.filters import threshold_otsu

# Load image
im = imread('https://i.stack.imgur.com/cqgfc.png',as_gray=True)

# Get Otsu threshold
t = threshold_otsu(im)

# Binarize and skeletonize
r = skeletonize(im>threshold_otsu(im))*255

# Save
imsave('result.png',r)

  • 或者`skimage.morphology.skeletonize` 用于具有较少分支的骨架。

以上是使用opencv进行连接线检测的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>