![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230813/image.jpeg)
spline interpolation image way to connect ?
3 views (last 30 days)
Show older comments
hi i try to make spline interpolation between two curves but i get bad results any help ?
i want to connect blue and red curves. i try to remove the unique values get bad result of yellow line
![spline.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230812/spline.jpeg)
z=zeros(1000,1000);
figure(1)
imshow(z);
vv =[801 928
748 923
687 902
657 854
651 814
657 785
671 730
700 695];
hold on,plot(vv(:,1),vv(:,2),'r'),plot(vv(:,1),vv(:,2),'r*')
vf =[800 328
801 306
805 256
798 223];
plot(vf(:,1),vf(:,2),'b'),plot(vf(:,1),vf(:,2),'b*')
%% point vector
xxa=[vv(:,1);vf(:,1)];
yya=[vv(:,2);vf(:,2)];
[w1,id2,idx]=unique(xxa,'stable');
xxas=xxa(id2);yyas=yya(id2);
dataPoints=[xxa,yya];
plot(dataPoints(:,1),dataPoints(:,2),'ko')
plot(dataPoints(:,1),dataPoints(:,2),'ko')
distF=[0 ;sqrt(sum(diff(dataPoints).^2,2))];
distFSum=cumsum(distF);
figure(2),plot(distF);grid on;title('distance between points')
figure(3),plot(distFSum);grid on;title('sum distance between points')
%% spline
sx=spline(xxas,distFSum(id2),linspace(min(xxa),max(xxa),100));
sy=spline(yyas,distFSum(id2),linspace(min(yya),max(yya),100));
figure(1)
plot(sx,sy,'y','LineWidth',2)
0 Comments
Accepted Answer
John D'Errico
on 22 Jul 2019
Edited: John D'Errico
on 22 Jul 2019
Your problem is you have a vague idea of what you wanted to do. But you got it completely wrong. Absolutely, completely, so.
You computed a chordal distance between the points. That part was right. But then you fit it as spline(x,dist) and spline(y,dist)? WRONG.
The distance becomes the INDEPENDENT variable. So you fit x and y as x(dist), y(dist).
And next, why in the name of god and little green apples do you think you needed to use unique here???????????????????????????????????????????
Yes, there are two points with the same value of x. And so what? You are not fitting this as a function of x. You cannot do that, because there is no single valued relationship as a function of x. The fit is in terms of the connect the dots distance. You deleted one of the points with a replicated x. But that is simply silly.
vv =[801 928
748 923
687 902
657 854
651 814
657 785
671 730
700 695];
vf =[800 328
801 306
805 256
798 223];
%% point vector
xxa=[vv(:,1);vf(:,1)];
yya=[vv(:,2);vf(:,2)];
distF=[0 ;sqrt(sum(diff([xxa,yya]).^2,2))];
distFSum=cumsum(distF);
t = linspace(min(distFSum),max(distFSum),100);
sx=spline(distFSum,xxa,t);
sy=spline(distFSum,yya,t);
plot(sx,sy,'y','LineWidth',2)
hold on
plot(xxa,yya,'ko')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230813/image.jpeg)
Basically what you did was take a few things that you saw in some disjoint places, tried to put it all together without understanding WHY those things were done.
More Answers (1)
darova
on 23 Jul 2019
When you have a curve something like this and want to get smooth red curve
![Untitle1d.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230931/Untitle1d.png)
You do
x = [x1 x2 ... xn] % all X data
y = [y1 y2 ... yn] % all Y data
xr = linspace(x4,x5,10); % i want 10 points for red curve (between 4 and 5 vertices)
yr = spline(x,y,xr);
You just have to switch X and Y for your case (because your curve is vertical)
0 Comments
See Also
Categories
Find more on Splines in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!