<p>If you look at <a href="https://www.w3.org/TR/SVG/paths.html" rel="nofollow noreferrer">chapter 8</a> of the <a href="https://www.w3.org/TR/SVG/" rel="nofollow noreferrer">SVG Specification</a>, it describes how to parse a <code>path</code> element. The short version is that you'll want to find the <code>d</code> attribute of the element. That element should be a string describing the curve. It will contain the following commands:</p>

<ul>
<li><code>m</code> - Moveto command</li>
<li><code>l</code> - Lineto command</li>
<li><code>c</code>, <code>s</code>, <code>q</code>, &amp; <code>a</code> - Curve commands</li>
<li><code>z</code> - close path</li>
</ul>

<p>Simple lines will just be combinations of <code>m</code> and <code>l</code> sequences. The curves will be either circular arcs, quadratic bezier or cubic beziers.</p>

<p>To convert a Bezier curve into line segments, you can do the following:</p>

<pre><code>const float kNumPolyLines = 10; // &lt;- change this to make more lines per bezier segment
const float kDeltaT = 1.0 / kNumPolyLines;
Point c0;
Point c1;
Point c2;
Point c3;
GetBezierControlPoints(&amp;c0, &amp;c1, &amp;c2, &amp;c3); // &lt;- get the 3 or 4 control points for the next bezier segment
Point prevPoint = c0;
Point currentPoint;
for (float t = kDeltaT; t &lt;= 1.0; t += kDeltaT)
{
    currentPoint = CalculateBezier(c0, c1, c2, c3, t);

    // Now you have the next line segment in your poly line. 
    // It goes from prevPoint to currentPoint. Store it or draw it, 
    //or whatever you need

    // Get ready for the next iteration
    prevPoint = currentPoint;
}
</code></pre>

<p>You can find the formula for both quadratic and cubic bezier curves <a href="https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases" rel="nofollow noreferrer">on Wikipedia</a>.</p>

CC BY-SA 3.0
2017-04-17T06:46:11.477Z
Thank you! Could you also tell me if this is possible to do in MATLAB? If so, how?
MrWarlock616
CC BY-SA 3.0
2017-04-17T16:11:46.790Z
I haven't used MATLAB in like 20 years, so I'm not really the one to ask. However, you can probably ask MATLAB questions on [Stack Overflow](http://stackoverflow.com/).
user1118321
CC BY-SA 3.0
2017-04-17T17:11:15.853Z
Ok. What platform is the SVG DOM interface implemented on? I can't decide what to use for this project.

Also I don't think what you're suggesting will work for the kind of path I have. I have many curves and control points in one single d attribute.
MrWarlock616
CC BY-SA 3.0
2017-04-17T17:19:07.407Z
Sorry, I mean many curves and move to commands in a single d atrribute
MrWarlock616
CC BY-SA 3.0
2017-04-18T02:02:34.350Z
It should work just fine for many curves and move to commands. Each bezier can be handled as above to turn it into poly lines. they will connect with the next one in the sequence. As for which platform to use, that's not something I can answer. I've done SVG parsing using a standard XML parser in Objective-C (Apple's NSXMLParser), but it could be done in any language for which you have an XML parsing library. Good luck!
user1118321
CC BY-SA 3.0
2017-04-18T03:28:55.323Z
I see. I am just going to try to get contour segments from a raster image and not bother with this. This seems way too complicated
MrWarlock616
CC BY-SA 3.0
2017-04-19T21:10:26.157Z
@MrWarlock616 its far  less complicated than implementing your own tracer. is just text processing. but then you most likely will not need to implement this as such applications allready exist, like say inkscape.
joojaa
CC BY-SA 3.0
2017-04-20T17:23:56.820Z
@joojaa It's not that simple, really, think about it. I basically have to implement the SVG DOM in Matlab.
MrWarlock616
CC BY-SA 3.0
2017-04-20T17:36:01.670Z
@MrWarlock616 you dont need a DOM  (or a SAX)  to do this. It makes it is slightly more fragile without one but not really a big deal. In any case matlab has a XML DOM implementation (called **xmlread**) so your complaint is totally irrelevant. See [docs](https://se.mathworks.com/help/matlab/ref/xmlread.html)
joojaa
CC BY-SA 3.0
2017-04-20T17:57:15.193Z
@joojaa I've seen the docs. I could retrieve the d attribute using xmlread and all that but I don't have the time to implement a parser for the d attribute. I just care about the point coordinates since I'm trying to replicate the results of a paper.
MrWarlock616
CC BY-SA 3.0
2017-04-17T06:12:54.330Z
2017-04-17T06:12:54.330Z
user1118321
<p>I am trying to implement a shape matching algorithm for vector images discussed in a paper.</p>

<p>The input to the algorithm is a model of 2D shapes deﬁned by contour segments. According to the paper, such segments can be deﬁned as polylines, obtained for example by vectorizing the output of an edge detector like Canny.</p>

<p>I am trying to vectorize a tiff image using Inkscape's bitmap tracing feature. The svg image I got from it has a path node with many attribute values, but that is not what the algorithm wants:</p>

<p><a href="https://i.sstatic.net/SVWx7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SVWx7.png" alt="output of canny edge detector"></a> <a href="https://i.sstatic.net/7f7RQ.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7f7RQ.jpg" alt="enter image description here"></a></p>

<p>How can I vectorize to polylines instead of paths?</p>

CC BY-SA 3.0
2017-04-13T05:34:17.240Z
Why not just take the curves that Inkscape outputs and convert them to poly lines? It's fairly simple to convert a Bezier curve to a poly line.
user1118321
CC BY-SA 3.0
2017-04-13T07:16:57.893Z
Why specifically a polyline? It's a much worse approximation to a curve than a Bezier spline. Regardless, I don't think you'll find a tool that will give you a "clean" polyline output without needing any manual tweaking.
Dan Hulme
CC BY-SA 3.0
2017-04-14T14:00:03.810Z
Section 4 of  the paper mentions that polylines can clearly define the contour segments. Those segments are used to compute the local Shape Context descriptors.
MrWarlock616
CC BY-SA 3.0
2017-04-14T14:00:29.533Z
I couldn't link the paper in my question because I am new. Here it is:

https://www.cg.tuwien.ac.at/research/publications/2014/Guerrero-2014-TPS/
MrWarlock616
CC BY-SA 3.0
2017-04-17T05:50:49.660Z
@user1118321 No it's not? Can you elaborate on how to do that? How do you even parse the svg path attribute?
MrWarlock616
CC BY-SA 3.0
2017-04-17T06:13:26.903Z
I've added an answer explaining how it would work.
user1118321
<vector-graphics>
Vectorizing to polylines instead of paths
MrWarlock616
# Vectorizing to polylines instead of paths

(Asked by: username_0 on 2017-04-13)

I am trying to implement a shape matching algorithm for vector images discussed in a paper.

The input to the algorithm is a model of 2D shapes deﬁned by contour segments. According to the paper, such segments can be deﬁned as polylines, obtained for example by vectorizing the output of an edge detector like Canny.

I am trying to vectorize a tiff image using Inkscape's bitmap tracing feature. The svg image I got from it has a path node with many attribute values, but that is not what the algorithm wants:

[![output of canny edge detector](https://i.sstatic.net/SVWx7.png)](https://i.sstatic.net/SVWx7.png) [![enter image description here](https://i.sstatic.net/7f7RQ.jpg)](https://i.sstatic.net/7f7RQ.jpg)

How can I vectorize to polylines instead of paths?

**Tags:** vector-graphics

> **Comments:**
> **username_1**: Why not just take the curves that Inkscape outputs and convert them to poly lines? It's fairly simple to convert a Bezier curve to a poly line.  
> **username_2**: Why specifically a polyline? It's a much worse approximation to a curve than a Bezier spline. Regardless, I don't think you'll find a tool that will give you a "clean" polyline output without needing any manual tweaking.  
> **username_0**: Section 4 of  the paper mentions that polylines can clearly define the contour segments. Those segments are used to compute the local Shape Context descriptors.  
> **username_0**: I couldn't link the paper in my question because I am new. Here it is:  https://www.cg.tuwien.ac.at/research/publications/2014/Guerrero-2014-TPS/  
> **username_0**: @username_1 No it's not? Can you elaborate on how to do that? How do you even parse the svg path attribute?  
> **username_1**: I've added an answer explaining how it would work.  

---

## 1 answers

### Answer by username_1 on 2017-04-17. Score: 2

If you look at [chapter 8](https://www.w3.org/TR/SVG/paths.html) of the [SVG Specification](https://www.w3.org/TR/SVG/), it describes how to parse a `path` element. The short version is that you'll want to find the `d` attribute of the element. That element should be a string describing the curve. It will contain the following commands:

* `m` - Moveto command
* `l` - Lineto command
* `c`, `s`, `q`, & `a` - Curve commands
* `z` - close path

Simple lines will just be combinations of `m` and `l` sequences. The curves will be either circular arcs, quadratic bezier or cubic beziers.

To convert a Bezier curve into line segments, you can do the following:

```
const float kNumPolyLines = 10; // <- change this to make more lines per bezier segment
const float kDeltaT = 1.0 / kNumPolyLines;
Point c0;
Point c1;
Point c2;
Point c3;
GetBezierControlPoints(&c0, &c1, &c2, &c3); // <- get the 3 or 4 control points for the next bezier segment
Point prevPoint = c0;
Point currentPoint;
for (float t = kDeltaT; t <= 1.0; t += kDeltaT)
{
    currentPoint = CalculateBezier(c0, c1, c2, c3, t);

    // Now you have the next line segment in your poly line. 
    // It goes from prevPoint to currentPoint. Store it or draw it, 
    //or whatever you need

    // Get ready for the next iteration
    prevPoint = currentPoint;
}

```

You can find the formula for both quadratic and cubic bezier curves [on Wikipedia](https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases).

> **Comments:**
> **username_0**: Thank you! Could you also tell me if this is possible to do in MATLAB? If so, how?  
> **username_1**: I haven't used MATLAB in like 20 years, so I'm not really the one to ask. However, you can probably ask MATLAB questions on [Stack Overflow](http://stackoverflow.com/).  
> **username_0**: Ok. What platform is the SVG DOM interface implemented on? I can't decide what to use for this project.  Also I don't think what you're suggesting will work for the kind of path I have. I have many curves and control points in one single d attribute.  
> **username_0**: Sorry, I mean many curves and move to commands in a single d atrribute  
> **username_1**: It should work just fine for many curves and move to commands. Each bezier can be handled as above to turn it into poly lines. they will connect with the next one in the sequence. As for which platform to use, that's not something I can answer. I've done SVG parsing using a standard XML parser in Objective-C (Apple's NSXMLParser), but it could be done in any language for which you have an XML parsing library. Good luck!  
> **username_0**: I see. I am just going to try to get contour segments from a raster image and not bother with this. This seems way too complicated  
> **username_3**: @username_0 its far  less complicated than implementing your own tracer. is just text processing. but then you most likely will not need to implement this as such applications allready exist, like say inkscape.  
> **username_0**: @username_3 It's not that simple, really, think about it. I basically have to implement the SVG DOM in Matlab.  
> **username_3**: @username_0 you dont need a DOM  (or a SAX)  to do this. It makes it is slightly more fragile without one but not really a big deal. In any case matlab has a XML DOM implementation (called **xmlread**) so your complaint is totally irrelevant. See [docs](https://se.mathworks.com/help/matlab/ref/xmlread.html)  
> **username_0**: @username_3 I've seen the docs. I could retrieve the d attribute using xmlread and all that but I don't have the time to implement a parser for the d attribute. I just care about the point coordinates since I'm trying to replicate the results of a paper.  

---

