Reputation: 399
I'm trying to convert an image that looks like:
Into one that looks something like (I drew this):
I want to detect and draw lines in the image where the thick/thin sections are smoothed out to create a somewhat even width and where the lines are overlapping if the pixels in the image are touching. (if you zoom in on the original image you can see even the thin segments do have pixels connecting them)
The two problems I'm running into are:
I'm currently trying to mess with Canny/Hough to get this working, but thus far have been unsuccessful.
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)
Upvotes: 2
Views: 2040
Reputation: 207808
Maybe scikit-image
medial_axis() is what you need:
from skimage.io import imread
from skimage.morphology import medial_axis
# Load image
im = imread('https://i.sstatic.net/cqgfc.png',as_gray=True)
skel= medial_axis(im)
Or, as kindly suggested by Bart in the comments, you can use skeletonize()
:
from skimage.io import imread, imsave
from skimage.morphology import skeletonize
from skimage.filters import threshold_otsu
# Load image
im = imread('https://i.sstatic.net/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)
Upvotes: 6