[英]Analysis of a 3D point cloud by projection in a 2D surface
我有一個3D點雲(XYZ),其中Z
可以是位置或能量。 我希望將它們投影在n- by- m網格中的2D表面上(在我的問題n = m
),其方式是每個網格單元具有Z
的最大差值,如果Z
是位置,或者求和的超過一個值Z
,在的情況下, Z
是能量。
例如,在0 <= (x,y) <= 20
,有500個點。 假設xy平面具有n -by- m分區,例如4- by- 4 ; 我的意思是在x
和y
方向上,我們有4個間隔為5
分區(使其最大為20
現在,這些單元格中的每一個都應該具有Z
值的總和或最大差值的值。那些位於定義的xy平面中相應列中的點。
我制作了一個簡單的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
如何在沒有循環的情況下完成?
accumarray
功能非常適合這種任務。 首先我定義示例數據:
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
我在假設
table
的三列分別代表x,y,z NaN
(如果您想要其他填充值,只需更改accumarray
最后一個參數)。 然后:
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);
代碼注釋:
ii
和jj
,它們給出每個點所在的x和y區的索引。 我使用repmat
來做到這一點。 最好使用bsxfun
,但它不支持@max
的多輸出版本。 備注:
你能做的是
meshgrid
布局xy網格, kd-tree
搜索找到最近的網格點,即將與每個雲點相關聯的數據標記為網格節點 accumarray
)。 這是一個有效的例子:
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.