在Pavia University数据中选取100×100大小的影像和参考数据,选取30个样本作为训练样本,基于随机森林进行分类
library("R.matlab")标签:gt,fea,光谱,TrainSamp,随机,pavia,训练样本,uni,影像 From: https://blog.51cto.com/u_14682436/6064328
library("yaImpute")
library("randomForest")
PATH <- getwd();
setwd(PATH)
# 定义样本数量
Trainnum <- 30
drow <- 100
dcol <- 100
ddim1 <- 103
# 读入高光谱特征数据
pathname1 <- file.path('Data','pavia_univ.mat')
pathname2 <- file.path('Data','pavia_ugt.mat')
pavia_uni_fea <- readMat(pathname1)
pavia_uni_gt <- readMat(pathname2)
# 将list 转为矩阵
pavia_uni_fea <- array(do.call(cbind, pavia_uni_fea),dim = c(10000,ddim1))
# 对影像数据进行pca降维
pavia_uni_fea.pr <- princomp(pavia_uni_fea, scores=T, cor = TRUE)
plot(pavia_uni_fea.pr,type="line")
pca_data <- predict(pavia_uni_fea.pr)
ddim <- 10
pavia_uni_fea <- array(pca_data[,1:ddim], dim = c(100,100,ddim))
# 将参考数据展示出来
pavia_uni_gt <- array(do.call(cbind, pavia_uni_gt ),dim = c(100,100))
image(pavia_uni_gt, col = heat.colors(12))
pavia_uni_feanorm <- array(data = NA, dim = dim(pavia_uni_fea))
# 抽取训练样本
# 找到不是0的索引
pavia_no0 <- which(pavia_uni_gt>0)
# 固定她的返回结果
set.seed(100)
RandomIndex <- sample(pavia_no0, length(pavia_no0))
TrainIndex <- RandomIndex[1:Trainnum]
# 显示训练样本
TrainSamp <- array(data = 0, dim = c(1,10000))
TrainSamp[TrainIndex] <- 1
TrainSamp <- array(TrainSamp,dim = c(100,100))
image(TrainSamp)
# 将训练样本的id转成行列号
xtrain <- ceiling(TrainIndex/drow)
ytrain <- TrainIndex - ((xtrain -1)*drow)
# 这里已经将训练数据和特征定义好了
TrainFea <- array(data = NA, dim = c(Trainnum,ddim))
TrainLabel <- array(data = NA, dim = c(Trainnum,1))
for(i in 1:Trainnum){
TrainFea[i,1:ddim] <- array(data = pavia_uni_fea[TrainIndex[i]],dim = c(1,ddim))
TrainLabel[i] <- array(data = pavia_uni_gt[TrainIndex[i]],dim = c(1,1))
}
# 使用RF进行分类
pavia_uni_fea1 <- array(pavia_uni_fea, dim = c(10000,ddim))
rf <- randomForest(TrainFea, TrainLabel,ntree=500)
pred<-predict(rf,pavia_uni_fea1)
pred <- array(as.numeric(pred),dim = c(100,100))
image(pred, col = heat.colors(12))