简体   繁体   English

通过2D表面投影分析3D点云

[英]Analysis of a 3D point cloud by projection in a 2D surface

I have a 3D point cloud (XYZ) where the Z can be position or energy. 我有一个3D点云(XYZ),其中Z可以是位置或能量。 I want to project them on a 2D surface in a n -by- m grid (in my problem n = m ) in a manner that each grid cell has a value of the maximum difference of Z , in case of Z being position, or a value of summation over Z , in case of Z being energy. 我希望将它们投影在n- by- m网格中的2D表面上(在我的问题n = m ),其方式是每个网格单元具有Z的最大差值,如果Z是位置,或者求和的超过一个值Z ,在的情况下, Z是能量。

For example, in a range of 0 <= (x,y) <= 20 , there are 500 points. 例如,在0 <= (x,y) <= 20 ,有500个点。 Let's say the xy-plane has n -by- m partitions, eg 4 -by- 4 ; 假设xy平面具有n -by- m分区,例如4- by- 4 ; by which I mean in both x and y directions we have 4 partitions with an interval of 5 (to make it 20 at maximum. Now, each of these cells should have a value of the summation, or maximum difference, of the Z value of those points which are in the corresponding column in the defined xy-plane. 我的意思是在xy方向上,我们有4个间隔为5分区(使其最大为20现在,这些单元格中的每一个都应该具有Z值的总和或最大差值的值。那些位于定义的xy平面中相应列中的点。

I made a simple array of XYZ just for a test as follows, where in this case, Z denotes the energy of the each point. 我制作了一个简单的XYZ阵列,仅用于测试,如下所示,在这种情况下, Z表示每个点的能量。

n=1;
for i=1:2*round(random('Uniform',1,5))
    for j=1:2*round(random('Uniform',1,5))
        table(n,:)=[i,j,random('normal',1,1)];
        n=n+1;
    end
end

How can this be done without loops? 如何在没有循环的情况下完成?

The accumarray function is quite suited for this kind of task. accumarray功能非常适合这种任务。 First I define example data: 首先我定义示例数据:

table = [ 20*rand(1000,1) 30*rand(1000,1) 40*rand(1000,1)]; % random data
x_partition = 0:2:20; % partition of x axis
y_partition = 0:5:30; % partition of y axis

I'm assuming that 我在假设

  • The three columns of table represent x, y, z respectively table的三列分别代表x,y,z
  • No point has x lower than that of first edge of your grid or greater than last edge, and the same for y. 没有点的x低于网格的第一个边缘或大于最后一个边缘,并且y的值相同。 That is, the grid covers all points. 也就是说,网格覆盖所有点。
  • If a bin contains no values the result should be NaN (if you want some other fill value, just change last argument of accumarray ). 如果bin不包含值,则结果应为NaN (如果您想要其他填充值,只需更改accumarray最后一个参数)。

Then: 然后:

L = size(table,1);
M = length(x_partition);
N = length(y_partition);
[~, ii] = max(repmat(table(:,1),1,M) <= repmat(x_partition,L,1),[],2);
[~, jj] = max(repmat(table(:,2),1,N) <= repmat(y_partition,L,1),[],2);
ii = ii-1; % by assumption, all values in ii will be at least 2, so we subtract 1
jj = jj-1; % same for jj
result_maxdif = accumarray([ii jj], table(:,3), [M-1 N-1], @(v) max(v)-min(v), NaN);
result_sum = accumarray([ii jj], table(:,3), [M-1 N-1], @sum, NaN);

Notes to the code: 代码注释:

  • The key is obtaining ii and jj , which give the indices of the x and y bins in which each point lies. 关键是获得iijj ,它们给出每个点所在的x和y区的索引。 I use repmat to do that. 我使用repmat来做到这一点。 It would have been better to use bsxfun , but it doesn't support the multiple-output version of @max . 最好使用bsxfun ,但它不支持@max的多输出版本。
  • The result has size (M-1) x (N-1) (numbers of bins in each dimension) 结果的大小(M-1)x(N-1)(每个维度中的箱数)

Remarks: 备注:

  1. all this can be almost one-liner via python pandas and cutting methods. 这一切几乎可以通过python pandas和切割方法进行单线程。
  2. I've rewritten your random cloud initialization 我已经重写了你的随机云初始化

What you can do is 你能做的是

  1. layout an xy grid via meshgrid , 通过meshgrid布局xy网格,
  2. project the cloud on xy (simple marginalization) 在xy上投射云(简单边缘化)
  3. find the nearest grid point via a kd-tree search, ie label your data associating to each cloud point a grid node 通过kd-tree搜索找到最近的网格点,即将与每个云点相关联的数据标记为网格节点
  4. group data by label and evaluate your local statistic (via accumarray ). 按标签分组数据并评估您当地的统计数据(通过accumarray )。

Here's a working example: 这是一个有效的例子:

 samples = 500;
 %data extrema
 xl = 0; xr = 1; yl = 0; yr = 1;

 % # grid points
 sz = 20;
 % # new random cloud    
 table = [random('Uniform',xl,xr,[samples,1]) , random('Uniform',yr,yl,[samples,1]), random('normal',1,1,[samples,1])];

 figure; scatter3(table(:,1),table(:,2),table(:,3));

 % # grid construction
 xx = linspace(xl,xr,sz); yy = linspace(yl,yr,sz);
 [X,Y] = meshgrid(xx,yy);
 grid_centers = [X(:),Y(:)];

 x = table(:,1); y = table(:,2); 

 % # kd-tree
 kdtreeobj = KDTreeSearcher(grid_centers);
 clss = kdtreeobj.knnsearch([x,y]); % # classification

 % # defintion of local statistic
 local_stat = @(x)sum(x) % # for total energy
 % local_stat = @(x)max(x)-min(x) % # for position off-set

 % # data_grouping
 class_stat = accumarray(clss,table(:,3),[],local_stat );       
 class_stat_M  = reshape(class_stat , size(X)); % # 2D reshaping

 figure; contourf(xx,yy,class_stat_M,20); 

在此输入图像描述在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM