library(sp)
library(spatstat)
library(raster)
library(rgeos)
library(rgl)
library(rgdal)
library(ggplot2)
library(arules)
library(rLiDAR)
main=readLAS("main.las")
m=as.data.frame(main)
qmain=kmeans(m[,1:2],30,iter.max = 30)
m$color=qmain$cluster
l=list()
for(i in 1:30){
l[[i]]=m[m$color==i,]
}
Another method that we can use to delete the ground points is to use k-mean clustering on the height coordinate. Let’s focus on the fisrt cluster from the previous section. We are going to cluster it by using k-mean algorithm on its z-coordinate. We choose the number of clusters to be \(10\).
ww=l[[1]]
qmain=kmeans(ww[,3],10,iter.max = 30)
ww$color=qmain$cluster
plot3d(ww$X,ww$Y,ww$Z,aspect = F,col = ww$color,xlab = "X",ylab = "Y",zlab = "Z")
You must enable Javascript to view this page properly.
As you can see, the first three clusters (Blue, yellow and red clusters) contain the ground set points. We can delete these clusters from the data set. The following function would do the job.
delg=function(ww,nclusters=10,ndeletes=2){
qmain=kmeans(ww[,3],10,iter.max = 30)
ind=order(qmain$centers)
q=qmain$cluster
for(i in 1:ndeletes){
ww=ww[q!=ind[i],]
q=q[q!=ind[i]]
}
ww
}
ww=delg(l[[1]],ndeletes = 3)
open3d()
## wgl
## 1
mfrow3d(1,2)
plot3d(l[[1]]$X,l[[1]]$Y,l[[1]]$Z,aspect = F,col = "red",xlab = "X",ylab = "Y",zlab = "Z")
plot3d(ww$X,ww$Y,ww$Z,aspect = F,col = "red",xlab = "X",ylab = "Y",zlab = "Z")
dim(l[[1]])[1]-dim(ww)[1]
## [1] 14950
You must enable Javascript to view this page properly.
The left side plot is the original one and the right side plot is the data set after deleting process.
We need to use both algorithm at the same time to make the process faster and more effective. Moreover, if we choose the initial cluster numbers to be big enough, the algorithm works more accurate.