Matlab中可以使用interp函数和griddata函数来实现插值,从而得到拟合曲面
* 从曲面上采样
* 离散点拟合曲面
<>1.离散点采样
为了获取离散点,首先需要从某个特定的曲面上采样。
其中利用数学的方法,就是先生成采样曲线的x-y坐标,并根据曲面方程获取对应的z值;
%我们以螺旋采样为例 %构建采样的坐标点x-y theta = 0:0.025*pi:10*pi; r7 = 0 + 0.1*theta; x =
r7.*cos(theta)+3; y = r7.*sin(theta)+3;plot(x,y,'.') [X,Y] = meshgrid(x,y);
%在对应曲面方程上采样 Z = rand*(sin(X+rand*2*pi)+sin(Y+rand*2*pi)); surf(X,Y,Z);shading
interp figure z = diag(Z); %对应x,y坐标点处的z plot3(x,y,z,'.b')
原曲面与采样点云
另一种则是利用栅格与插值的方法来获取对应的z值:
%Based surface x0 = linspace(0,2*pi,128); y0 = linspace(0,2*pi,128); [X0,Y0] =
meshgrid(x0,y0); Z0= rand*(sin(X0+rand*2*pi)+sin(Y0+rand*2*pi)); %而后生成采样栅格
theta = 0:0.025*pi:10*pi; r7 = 0 + 0.1*theta; x = r7.*cos(theta)+3; y =
r7.*sin(theta)+3;plot(x,y,'.') [X,Y] = meshgrid(x,y); %此处用重新插值的方法获得采样点的坐标
%相当与在原图上做了重新的插值,查到了螺旋线栅格化后的点 Z = interp2(X0,Y0,Z0,X,Y); surf(X,Y,Z);shading
interp figure z = diag(Z); %对应x,y坐标点处的z plot3(x,y,z,'.b')
此时的曲面和采集到的点云如下:
<>2.曲面拟合
在有了点云之后,需要将x-y-z数据变成栅格化数据才能够调用mesh,surf等函数绘制图形。
%目标曲面的大小,需要先生成一个栅格 %一般与点云大小相当 xn = linspace(0,2*pi,128); yn =
linspace(0,2*pi,128); [Xn,Yn] = meshgrid(xn,yn); %利用griddata来插值,从xyz生成栅格数据
%最后一个为插值方法,包linear cubic natural nearest和v4等方法 %v4方法耗时长但较为准确 Zn =
griddata(x,y,z,Xn,Yn,'v4'); surf(Xn,Yn,Zn);shading interp figure mesh(Xn,Yn,Zn)
hold on plot3(x,y,z,'r+','MarkerSize',3)
拟合出的曲面结果,以及包含采样点的mesh图:
ref:
https://blog.csdn.net/u012302488/article/details/51201238
<https://blog.csdn.net/u012302488/article/details/51201238>
https://blog.csdn.net/shanchuan2012/article/details/72772191
<https://blog.csdn.net/shanchuan2012/article/details/72772191>
http://muchong.com/html/201106/3290808.html
<http://muchong.com/html/201106/3290808.html>
pic from pexels.com
<https://www.pexels.com/photo/purple-volleyball-net-1552620/>
热门工具 换一换