# How to compute the density of a 3D point cloud?

64 ビュー (過去 30 日間)
Ali 2020 年 7 月 12 日
コメント済み: Ali 2020 年 7 月 12 日
I'm trying to write a program that operates on 3D point clouds (.ply, .pcd), I need to know the density of the given point cloud file and compare it with a threshold or certain percentage to decide for some operations. How can I do that?
I have found this for 3D point clouds on the web:
Two methods can be used to compute the density:
• either 'Precise': the density is estimated by counting for each point the number of neighbors N (inside a sphere of radius R)
• or 'Approximate': the density is simply estimated by determining the distance to the nearest neighbor (which is generally much faster). This distance is considered as being equivalent to the above spherical neighborhood radius R (and N = 1).
However I'm not sure how to put this into matlab code, I guess I should use pcfitplane for sphere fitting and findNearestNeighbors. Would appreciate a step by step example.

サインインしてコメントする。

### 採用された回答

Thiago Henrique Gomes Lobato 2020 年 7 月 12 日

You can just directly apply the definition you gave considering that the density is N/Volume. The easiest is the second one, which would be:
K = 1;
for idx=1:length(AllPoints)
[~,r] = findNearestNeighbors(AllPoints,AllPoints(idx,:),K);
density(idx) = 1/(4*pi*r.^3/3);
end
The first one is a little more complicated but at the same time not so much:
R = 1; % depends on your data
for idx=1:length(AllPoints)
Distances = sqrt( sum( (AllPoints-AllPoints(idx,:)).^2 ,2) );
Ninside = length( find(Distances<=R) );
density(idx) = Ninside/(4*pi*R.^3/3);
end

#### 6 件のコメント

Ali 2020 年 7 月 12 日
@Thiago I stumbled upon this information on the same page explaining the methods above:
The density output can be:
• the number of neighbors N (only available in 'Precise' mode)
• a surface density: number of neighbors divided by the neighborhood surface = N / (Pi.R2)
• a volume density: number of neighbors divided by the neighborhood volume = N / (4/3.Pi.R3)
It made wonder, shouldn't we reorder the (4*pi*R.^3/3) part for the volume density? Like (4/3*pi*R.^3) ?
Also from the same page:
Notes:
• a point with no neighbor in the spherical neighborhood will have an invalid (NaN) density.
• the central point is always used for computing the density (even when the output is the 'number of neighbors' as we consider the number of neighbors of the 'position in space' of each point). Therefore the density will be actually equal to kNN + 1 where kNN is the number of neighbors of each point)
Thiago Henrique Gomes Lobato 2020 年 7 月 12 日
4*pi*R.^3/3 is the same as 4/3*pi*R.^3 since exponent has priority over division in matlab. For the notes the first point is important only when there's a limit to define neighborhood, and the second is already taken into account for all methods.
Ali 2020 年 7 月 12 日
I did try my suggested order and indeed your statement is true, no difference in results. I appreciate your time and help.

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!