Nan值来自griddata

时间:2016-06-05 16:03:34

标签: matlab interpolation

代码:

x=xyz_camera_frame(:,1);
y=xyz_camera_frame(:,2);
z=xyz_camera_frame(:,3);
xq=linspace(min(x),max(x),100);
yq=linspace(min(y),max(y),100);
zq=griddata(x,y,z,xq,yq);

问题:

griddata返回的一些值为Nan。我不明白,我在用于插值的数据范围内使用了所有x和y。我提供了以下数据。

数据:

>>xyz_camera_frame
-558.336553688600   828.056210369240    -1837.47937368187
-550.892426227711   794.936769199689    -1808.72682002260
-603.846962906934   851.372915856983    -1712.09366057117
-818.811157160022   884.767235971338    -1558.25014602362
-1012.30463037728   831.185172081933    -1427.70165589871
-1045.82571004598   692.455128071284    -1399.30798613536
-992.467127527465   608.779584169659    -1394.52549384845
-1081.69261657566   561.817230061316    -1340.99127748195
-1285.64088734587   450.170033074173    -1138.19479622127
-1285.64088734587   450.170033074173    -1138.19479622127
-1442.20131036315   339.085936148270    -925.183326861918
-1528.67244791715   159.820175944104    -804.659778016503
-1500.11791629949   -87.9237558678489   -808.071629700352
-1455.92921560688   -48.0074731557390   -860.016984208648
-1521.01192278841   -136.929311517256   -659.967982600971
-1565.91773124737   -297.661714131866   -346.577836677670
-1566.45934705874   -337.020087203977   -309.538723290191
-1745.00981140219   -235.550194307685   -455.832248827105
-1834.08270382282   644.019464202153    -1183.08785373590
-1696.77284981365   737.809996970051    -1515.67442101056
739.194190935881    1663.82013156789    -2471.68463176660
-721.669766608237   2445.65386536664    -3539.59304186409
82.8520290560576    2818.78604007141    -3389.08120001113
-629.351172568747   2128.70093285375    -3192.07545943683
-837.020672380509   1894.66259228118    -2991.05779359167
-985.624586756649   1639.63742841083    -2845.92385466952
-1804.89642560898   1549.58412361620    -2158.95908581209
-1659.24846065565   1469.47555756921    -2202.10672095917
-2201.47663125592   863.075386146258    -1513.62346475461
-2650.05442315804   460.898429832640    -774.869210275370
-2665.94697165560   566.948145814364    -684.863017871965
-1022.23879277860   2670.60414151831    -2826.15153823890
-2139.00577921475   1916.48630564150    -2128.33892716129
-580.817281550792   540.043524610704    -1821.51585236142
-1228.31732819579   1972.60682551667    -1942.10992500455
-1702.07940137072   1646.02189901725    -2083.20279568629

更新(@Suever的回答):

x=xyz_camera_frame(:,1);
y=xyz_camera_frame(:,2);
z=xyz_camera_frame(:,3);
% get the points 
inds = convhull(x, y);
[xq, yq] = meshgrid(linspace(min(x), max(x), 100), linspace(min(y), max(y), 100));
touse = inpolygon(xq, yq, x(inds), y(inds));
xq = xq(touse);
yq = yq(touse);
zq=griddata(x,y,z,xq,yq);

这里zq是1-d向量,而不是稍后给出此错误的矩阵

K>> mesh(xq,yq,zq);
Error using mesh (line 58)
Z must be a matrix, not a scalar or vector.

1 个答案:

答案 0 :(得分:3)

griddata使用数据的凸包作为插值有效的区域(无需外推)。任何超出此区域的查询点都将获得NaN值。

因此,基于您提供的示例数据,我们可以计算凸包(有效插值区域)。我们可以将其与原始数据的位置一起可视化。

inds = convhull(xyz_camera_frame(:,1), xyz_camera_frame(:,2));
plot(xyz_camera_frame(inds,1), xyz_camera_frame(inds,2), 'r');

enter image description here

此红色区域之外的任何查询点都将为您提供NaN值。现在让我们绘制你实际查询的点数。

xq = linspace(min(x),max(x),100);
yq = linspace(min(y),max(y),100);

[xq, yq] = meshgrid(xq, yq);

plot(xq, yq, 'g.')

enter image description here

您会看到许多查询点都位于凸包之外。所有这些点在您的结果中都是NaN

在结果中没有NaN值的唯一方法是凸包是否绑定了所有数据点(即,您在最小值定义的边界框的所有角上都有输入数据)和最大x和y值)。

<强>更新

如果你只想要凸包内的点数据,你真的有两个选择。

  1. 使用inpolygon检查凸包内部的哪些点,并仅使用这些点。

    inds = convhull(x, y);
    
    [xq, yq] = meshgrid(linspace(min(x), max(x), 100), linspace(min(y), max(y), 100));
    
    touse = inpolygon(xq, yq, x(inds), y(inds));
    
    xq = xq(touse);
    yq = yq(touse);
    
    zq = griddata(x, y, z, xq, yq);
    
  2. 插入后删除NaN(可能更快)

    zq = griddata(x, y, z, xq, yq);
    
    toremove = isnan(zq);
    xq(toremove) = [];
    yq(toremove) = [];
    zq(toremove) = [];