ahb*_*ore 2 python opencv image-processing
我正在尝试转换看起来像的图像:
变成一个看起来像的东西(我画了这个):
我想在图像中检测和绘制线条,粗/细部分被平滑以创建稍微均匀的宽度,如果图像中的像素相互接触,线条会重叠。(如果放大原始图像,您甚至可以看到细小的部分确实有像素连接它们)
我遇到的两个问题是:
我目前正试图与 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)
Run Code Online (Sandbox Code Playgroud)
也许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)
Run Code Online (Sandbox Code Playgroud)
或者,正如 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)
Run Code Online (Sandbox Code Playgroud)