Reputation: 1416
I am using OpenCV canny edge detection module to find the contours of an image which gives me a B&W output image.
Here's the code for that:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(blurred, 180, 200)
Now I want to write this image to disk in SVG file format. How do I obtain the same from the cannied image?
Upvotes: 4
Views: 11771
Reputation: 21
The convert contour from this page doesn't work for me. If you want to convert pure edge into vector you'll need polyline points.
image = cv2.imread('tom.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_image, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
## converting image to .svg
width, height = image.shape[1], image.shape[0]
f = open('tom.svg', 'w+') ## saved file .svg
f.write('<svg width="'+str(width)+'" height="'+str(height)+'" xmlns="http://www.w3.org/2000/svg">')
for poly in contours:
new_str = '<polyline points="'
for x, y in poly[:,0]:
new_str = new_str + str(x)+','+str(y)+' '
new_str = new_str + '" style="fill:none;stroke:black;stroke-width:1" />'
f.write(new_str)
pass
f.write('</svg>')
f.close()
Upvotes: 1
Reputation: 136
I assume you want just the contours to be saved as SVG image.
First, you need to compute the contours in your canny image using the findContorus function of OpenCV. Please be careful here, your contours MUST be well defined.
Second, having your contours just follow this question:
Convert contour paths to svg paths
The SVG paths can be opened in any image processing software (e.g. Inkscape, Photoshop, etc)
Upvotes: 9