<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252</link>
    <title>MATLAB Central Newsreader - Implementing splines in a microcontroller</title>
    <description>Feed for thread: Implementing splines in a microcontroller</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 18 Nov 2009 20:13:20 -0500</pubDate>
      <title>Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#695875</link>
      <author>Tom E</author>
      <description>Hi folks,&lt;br&gt;
&lt;br&gt;
I have a reasonably simple task to do for uni:&lt;br&gt;
Take an image&lt;br&gt;
Extract the edges&lt;br&gt;
Display this using a microcontroller.&lt;br&gt;
&lt;br&gt;
Because the display method is as a vector, I have chosen to interpret the edges as splines.&lt;br&gt;
&lt;br&gt;
So far I have managed to extract the edges and convert to a series of splines.&lt;br&gt;
&lt;br&gt;
Now i've come to the problem of how to implement the resultant data in a microcontroller. I don't really understand how splines are stored in matlab. As far as I understand, splines are a series of curves, all joined together (Bezier curves, paths, however you'd like to call them).&lt;br&gt;
&lt;br&gt;
So given a spline in ppform, or B-form, how do I know what the equations are for each of the curves?&lt;br&gt;
&lt;br&gt;
I'm also open to other suggestions for how to solve my problem. If splines aren't the way to go, no worries. The only problem is that my supervisor is convinced I should implement the images as a vector.&lt;br&gt;
&lt;br&gt;
Thanks for any help you can give!&lt;br&gt;
Tom</description>
    </item>
    <item>
      <pubDate>Wed, 18 Nov 2009 21:14:20 -0500</pubDate>
      <title>Re: Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#695888</link>
      <author>Bruno Luong</author>
      <description>&quot;Tom E&quot; &amp;lt;tom.elliot@students.fhv.at&amp;gt; wrote in message &amp;lt;he1kh0$26s$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi folks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a reasonably simple task to do for uni:&lt;br&gt;
&amp;gt; Take an image&lt;br&gt;
&amp;gt; Extract the edges&lt;br&gt;
&amp;gt; Display this using a microcontroller.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Because the display method is as a vector, I have chosen to interpret the edges as splines.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So far I have managed to extract the edges and convert to a series of splines.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now i've come to the problem of how to implement the resultant data in a microcontroller. I don't really understand how splines are stored in matlab. As far as I understand, splines are a series of curves, all joined together (Bezier curves, paths, however you'd like to call them).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So given a spline in ppform, or B-form, how do I know what the equations are for each of the curves?&lt;br&gt;
&lt;br&gt;
Spline is a polynomial on each subinterval (piecewise polynomials). On each interval, the polynomial is computed with respect to the relative abscissa to its left bound  (leftbound := 0). The coefficients of the polynomials are ranged in each columns of the field COEFS (highest to lowest order - same as used by polyval/polyfit). Consecutive subintervals are separated by the subdivision of the entire interval stored in the field BREAKS.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 22 Nov 2009 16:38:03 -0500</pubDate>
      <title>Re: Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#696676</link>
      <author>Tom E</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;he1o3c$mqq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Tom E&quot; &amp;lt;tom.elliot@students.fhv.at&amp;gt; wrote in message &amp;lt;he1kh0$26s$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi folks,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have a reasonably simple task to do for uni:&lt;br&gt;
&amp;gt; &amp;gt; Take an image&lt;br&gt;
&amp;gt; &amp;gt; Extract the edges&lt;br&gt;
&amp;gt; &amp;gt; Display this using a microcontroller.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Because the display method is as a vector, I have chosen to interpret the edges as splines.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So far I have managed to extract the edges and convert to a series of splines.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Now i've come to the problem of how to implement the resultant data in a microcontroller. I don't really understand how splines are stored in matlab. As far as I understand, splines are a series of curves, all joined together (Bezier curves, paths, however you'd like to call them).&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So given a spline in ppform, or B-form, how do I know what the equations are for each of the curves?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Spline is a polynomial on each subinterval (piecewise polynomials). On each interval, the polynomial is computed with respect to the relative abscissa to its left bound  (leftbound := 0). The coefficients of the polynomials are ranged in each columns of the field COEFS (highest to lowest order - same as used by polyval/polyfit). Consecutive subintervals are separated by the subdivision of the entire interval stored in the field BREAKS.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Hi Bruno,&lt;br&gt;
&lt;br&gt;
I'm having trouble understanding your response - unfortunately I'm not as adept at MATLAB as i'd like.&lt;br&gt;
&lt;br&gt;
&amp;gt;On each interval, the polynomial is computed with respect to the relative abscissa to its left bound  (leftbound := 0).&lt;br&gt;
&lt;br&gt;
Does this mean that if the first polynomial is active from 1 - 4 and the second polynomial is active from 4 - 5, then the second polynomial must be calculated with the abscissa values of (4 - 4) and (5 - 4)?&lt;br&gt;
&lt;br&gt;
I have attempted to redraw the spline by extracting the polynomials from the BREAKS and COEFS fields of the struct, however the result is not what I would expect:&lt;br&gt;
------ CODE --------------------------------------------------&lt;br&gt;
clc&lt;br&gt;
clear all;&lt;br&gt;
close all;&lt;br&gt;
&lt;br&gt;
x = 0:10;&lt;br&gt;
y = sin(x);&lt;br&gt;
xx = 0:.25:10;&lt;br&gt;
yy1 = spline(x,y,xx);&lt;br&gt;
yy2 = cscvn([x; y]);&lt;br&gt;
subplot(3,1,1);&lt;br&gt;
plot(x,y,'o',xx,yy1)&lt;br&gt;
subplot(3,1,2);&lt;br&gt;
fnplt(yy2,'r');&lt;br&gt;
&lt;br&gt;
xvals = 0;&lt;br&gt;
lastval = 0;&lt;br&gt;
for aspline = 1:length(yy2.breaks) - 1 &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xvals = yy2.breaks(aspline);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;subplot(3,1,3);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yy2coefs = yy2.coefs(aspline,:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yy2polyval = polyval(yy2coefs,xvals - lastval);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plot(xvals, yy2polyval,'-x'), hold on;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lastval = xvals;&lt;br&gt;
end&lt;br&gt;
------ CODE --------------------------------------------------&lt;br&gt;
&lt;br&gt;
could someone please explain to me what i'm missing? I obviously don't correctly understand the way splines are stored.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Tom</description>
    </item>
    <item>
      <pubDate>Sun, 22 Nov 2009 16:43:04 -0500</pubDate>
      <title>Re: Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#696678</link>
      <author>Tom E</author>
      <description>Oh and by the way, I'm using cscvn() because I need to be able to have a 1 to many input, unlike the simple 1 to 1 function (sine) used in this demo.</description>
    </item>
    <item>
      <pubDate>Sun, 22 Nov 2009 17:54:03 -0500</pubDate>
      <title>Re: Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#696685</link>
      <author>Bruno Luong</author>
      <description>x = 0:10;&lt;br&gt;
y = sin(x);&lt;br&gt;
s = spline(x,y);&lt;br&gt;
&lt;br&gt;
plot(x,y,'o');&lt;br&gt;
hold on&lt;br&gt;
&lt;br&gt;
for aspline = 1:length(s.breaks) - 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;left = s.breaks(aspline);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;right = s.breaks(aspline+1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xx = linspace(left,right,17);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;scoefs = s.coefs(aspline,:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yy = polyval(scoefs,xx - left);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plot(xx, yy);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 22 Nov 2009 21:48:03 -0500</pubDate>
      <title>Re: Implementing splines in a microcontroller</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266252#696718</link>
      <author>Tom E</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;hebtrr$l6p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; x = 0:10;&lt;br&gt;
&amp;gt; y = sin(x);&lt;br&gt;
&amp;gt; s = spline(x,y);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; plot(x,y,'o');&lt;br&gt;
&amp;gt; hold on&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for aspline = 1:length(s.breaks) - 1&lt;br&gt;
&amp;gt;     left = s.breaks(aspline);&lt;br&gt;
&amp;gt;     right = s.breaks(aspline+1);&lt;br&gt;
&amp;gt;     xx = linspace(left,right,17);&lt;br&gt;
&amp;gt;     scoefs = s.coefs(aspline,:);&lt;br&gt;
&amp;gt;     yy = polyval(scoefs,xx - left);&lt;br&gt;
&amp;gt;     plot(xx, yy);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Bruno&lt;br&gt;
&lt;br&gt;
Ahh, thanks Bruno - that has helped me understand. I've solved one of the problems with my code, but unfortunately I'm still struggling : (&lt;br&gt;
The method i'm using to generate the spline seems to be wrong. I'm using bwtraceboundary to extract the boundary from a black and white image, and then using cscvn to convert the boundary into a spline.&lt;br&gt;
&lt;br&gt;
The problem is in the coefficients cscvn returns. I have a sample image, which is a short horizontal line in an image. I believe the coefficients cscvn returns are correct for every odd row of s.coeffs() (where s is the spline). Each even row has a strange set of coefficients.&lt;br&gt;
&lt;br&gt;
As far as I can tell I must be using cscvn incorrectly, because the coefficients are:&lt;br&gt;
0	0	0	5&lt;br&gt;
-0.800000000000000	1.80000000000000	0	1&lt;br&gt;
0	0	0	5&lt;br&gt;
0.400000000000000	-0.600000000000001	1.20000000000000	2&lt;br&gt;
0	0	0	5&lt;br&gt;
-0.800000000000000	0.600000000000000	1.20000000000000	3&lt;br&gt;
0	0	0	5&lt;br&gt;
0.800000000000000	-1.80000000000000	4.46071750965465e-17	4&lt;br&gt;
0	0	0	5&lt;br&gt;
-0.400000000000000	0.600000000000000	-1.20000000000000	3&lt;br&gt;
0	0	0	5&lt;br&gt;
0.800000000000000	-0.600000000000000	-1.20000000000000	2&lt;br&gt;
&lt;br&gt;
when I think each row should be:&lt;br&gt;
0	0	0	5&lt;br&gt;
&lt;br&gt;
It's probably easier to demonstrate than to explain. I have uploaded the image to h**p://i50.tinypic.com/16jiqzr.jpg&lt;br&gt;
and the code I'm using is below:&lt;br&gt;
&lt;br&gt;
-------- CODE -------------------------------------&lt;br&gt;
clc, clear all, close all;&lt;br&gt;
imgToRead = 'Sample_BW_Line_01.jpg';&lt;br&gt;
%imgToRead = 'FHV_logo_BW.gif';&lt;br&gt;
DACdepth = 12;  % the bit depth of the DACs&lt;br&gt;
&lt;br&gt;
DACres = 2^DACdepth;    % the resolution of the DACs&lt;br&gt;
BW = imread(imgToRead);&lt;br&gt;
BW = ~BW;&lt;br&gt;
s=size(BW);&lt;br&gt;
scaleFrom = length(BW);&lt;br&gt;
scaleFactor = DACres/scaleFrom; % determine how much to scale the image by&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% so that the output is the correct size&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% for the DACs&lt;br&gt;
boundaries = cell(1);&lt;br&gt;
&lt;br&gt;
subplot(3,1,1);&lt;br&gt;
imshow(BW);&lt;br&gt;
for row = s(1):-1:1 % search through each row&lt;br&gt;
&amp;nbsp;for col=1:s(2)   % search through each column&lt;br&gt;
&amp;nbsp;&amp;nbsp;if BW(row,col),   %if pixel value is 1:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;contour = bwtraceboundary(BW, [row, col], 'E', 8, Inf,'clockwise');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% search for the boundary, starting at&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% [row,col] save the points into&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &quot;contour&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if(~isempty(contour))    &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;boundaries{end+1} = cscvn(contour()');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% boundaries{end+1} = fncmb(cscvn(contour()'),[0 scaleFactor;-scaleFactor 0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% convert the points to a spline, and save to&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &quot;boundaries&quot;. fncmb is used to rotate the image&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% to the correct orientation.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BW(contour(:,1),contour(:,2)) = 0; % remove the edge from the image&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% so it isn't found again&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
boundaries(1) = [];&lt;br&gt;
for outline = 1:length(boundaries)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s = boundaries{outline};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;subplot(3,1,2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fnplt(s), hold on;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for aspline = 1:length(s.breaks) - 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;left = s.breaks(aspline);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;right = s.breaks(aspline + 1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xx = linspace(left, right, 17);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;subplot(3,1,3);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;scoefs = s.coefs(aspline,:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yy = polyval(scoefs, xx - left);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plot(xx,yy);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hold on&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
set(gca,'PlotBoxAspectRatioMode','manual');&lt;br&gt;
% axis([0 4096 -4096 0]);&lt;br&gt;
subplot(3,1,2);&lt;br&gt;
set(gca,'PlotBoxAspectRatioMode','manual');&lt;br&gt;
%axis([0 4096 -4096 0]);&lt;br&gt;
clear DACdepth col row outline scaleFrom splinetoplot;&lt;br&gt;
-------- CODE -------------------------------------</description>
    </item>
  </channel>
</rss>

