Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Implementing splines in a microcontroller
Date: Sun, 22 Nov 2009 21:48:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 105
Message-ID: <hecbij$fn2$1@fred.mathworks.com>
References: <he1kh0$26s$1@fred.mathworks.com> <he1o3c$mqq$1@fred.mathworks.com> <hebpdb$j29$1@fred.mathworks.com> <hebpmo$78c$1@fred.mathworks.com> <hebtrr$l6p$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1258926483 16098 172.30.248.38 (22 Nov 2009 21:48:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 22 Nov 2009 21:48:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2100208
Xref: news.mathworks.com comp.soft-sys.matlab:587077


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hebtrr$l6p$1@fred.mathworks.com>...
> x = 0:10;
> y = sin(x);
> s = spline(x,y);
> 
> plot(x,y,'o');
> hold on
> 
> for aspline = 1:length(s.breaks) - 1
>     left = s.breaks(aspline);
>     right = s.breaks(aspline+1);
>     xx = linspace(left,right,17);
>     scoefs = s.coefs(aspline,:);
>     yy = polyval(scoefs,xx - left);
>     plot(xx, yy);
> end
> 
> % Bruno

Ahh, thanks Bruno - that has helped me understand. I've solved one of the problems with my code, but unfortunately I'm still struggling : (
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.

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.

As far as I can tell I must be using cscvn incorrectly, because the coefficients are:
0	0	0	5
-0.800000000000000	1.80000000000000	0	1
0	0	0	5
0.400000000000000	-0.600000000000001	1.20000000000000	2
0	0	0	5
-0.800000000000000	0.600000000000000	1.20000000000000	3
0	0	0	5
0.800000000000000	-1.80000000000000	4.46071750965465e-17	4
0	0	0	5
-0.400000000000000	0.600000000000000	-1.20000000000000	3
0	0	0	5
0.800000000000000	-0.600000000000000	-1.20000000000000	2

when I think each row should be:
0	0	0	5

It's probably easier to demonstrate than to explain. I have uploaded the image to h**p://i50.tinypic.com/16jiqzr.jpg
and the code I'm using is below:

-------- CODE -------------------------------------
clc, clear all, close all;
imgToRead = 'Sample_BW_Line_01.jpg';
%imgToRead = 'FHV_logo_BW.gif';
DACdepth = 12;  % the bit depth of the DACs

DACres = 2^DACdepth;    % the resolution of the DACs
BW = imread(imgToRead);
BW = ~BW;
s=size(BW);
scaleFrom = length(BW);
scaleFactor = DACres/scaleFrom; % determine how much to scale the image by
                                % so that the output is the correct size
                                % for the DACs
boundaries = cell(1);

subplot(3,1,1);
imshow(BW);
for row = s(1):-1:1 % search through each row
 for col=1:s(2)   % search through each column
  if BW(row,col),   %if pixel value is 1:
   contour = bwtraceboundary(BW, [row, col], 'E', 8, Inf,'clockwise');
            % search for the boundary, starting at
            % [row,col] save the points into
            % "contour"
   if(~isempty(contour))    
    boundaries{end+1} = cscvn(contour()');
    % boundaries{end+1} = fncmb(cscvn(contour()'),[0 scaleFactor;-scaleFactor 0]);
    % convert the points to a spline, and save to
    % "boundaries". fncmb is used to rotate the image
    % to the correct orientation.
    BW(contour(:,1),contour(:,2)) = 0; % remove the edge from the image
                                       % so it isn't found again
   end
  end
 end
end
      
boundaries(1) = [];
for outline = 1:length(boundaries)
    s = boundaries{outline};
    subplot(3,1,2);
    fnplt(s), hold on;
    for aspline = 1:length(s.breaks) - 1
        left = s.breaks(aspline);
        right = s.breaks(aspline + 1);
        xx = linspace(left, right, 17);
        subplot(3,1,3);
        scoefs = s.coefs(aspline,:);
        yy = polyval(scoefs, xx - left);
        plot(xx,yy);
        hold on
    end
end
set(gca,'PlotBoxAspectRatioMode','manual');
% axis([0 4096 -4096 0]);
subplot(3,1,2);
set(gca,'PlotBoxAspectRatioMode','manual');
%axis([0 4096 -4096 0]);
clear DACdepth col row outline scaleFrom splinetoplot;
-------- CODE -------------------------------------