pythonopencvsvg

How to convert edged image in OpenCV to svg file?


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?


Solution

  • 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)