--K-Means clustering
实战教程,只强调实操,目的意义请移步我上篇博文 https://www.jianshu.com/p/a01d26c61f77
一. 文件准备
1. 待分析动物照片,要求拍摄参数,拍摄条件一致。
2. ImageJ中打地标点,勾勒出动物轮廓,保存文本格式,可以比我这个更精细一些。
图片准备就到这里,是不是很容易。
二. 代码实战
1. 分析环境的准备
#所需R包的安装
install.packages("patternize")
library(patternize)
#install.packages("rgdal","abind","raster","sp","RniftyReg")
#library(devtools)
#install_github("zarquon42b/Morpho", local=FALSE)
# The viridis package provides colour blind friendly colour schemes
# install.packages("viridis")
library(viridis)
2. 文件的读取
#图片的读取
IDList1 <- c('1_1','1_2','1_3','1_4','1_5','1_6','1_7','1_8','1_9','1_10','1_11','1_12','1_13','1_14','1_15')
IDList2 <- c('2_1','2_2','2_3','2_4','2_5','2_6','2_7','2_8','2_9','2_10','2_11','2_12','2_13','2_14','2_15')
IDList3 <- c('3_1','3_2','3_3','3_4','3_5','3_6','3_7','3_8','3_9','3_10','3_11','3_12','3_13','3_14','3_15')
IDList4 <- c('4_1','4_2','4_3','4_4','4_5','4_6','4_7','4_8','4_9','4_10','4_11','4_12','4_13','4_14','4_15')
IDList5 <- c('5_1','5_2','5_3','5_4','5_5','5_6','5_7','5_8','5_9','5_10','5_11','5_12','5_13','5_14','5_15')
prepath <- 'C:\\Users\\HP\\Desktop\\ps\\'
extension <- '.jpg'
imageList1 <- makeList(IDList1, 'image', prepath, extension)
imageList2 <- makeList(IDList2, 'image', prepath, extension)
imageList3 <- makeList(IDList3, 'image', prepath, extension)
imageList4 <- makeList(IDList4, 'image', prepath, extension)
imageList5 <- makeList(IDList5, 'image', prepath, extension)
# 选取一个动物图片作为目标,将在后期分析中以卡通形象出现。
target <- imageList5[['5_14']]
# 读取动物轮廓地标文件
outline <- read.table('C:\\Users\\HP\\Desktop\\ps\\5_14.txt', h= F)
3. 比对颜色模式
# 对颜色模式进行比对,此处参数K值的调整可以改变提取颜色模式的数量
rasterList_regK1 <- patRegK(imageList1, target, k = 5, resampleFactor = 5, crop = c(200,2800,300,1800),
plot = TRUE, useBlockPercentage = 90, maskOutline = outline, maskColor = 255)
rasterList_regK2 <- patRegK(imageList2, target, k = 5, resampleFactor = 5, crop = c(200,2800,300,1800),
plot = TRUE, useBlockPercentage = 90, maskOutline = outline, maskColor = 255)
rasterList_regK3 <- patRegK(imageList3, target, k = 5, resampleFactor = 5, crop = c(200,2800,300,1800),
plot = TRUE, useBlockPercentage = 90, maskOutline = outline, maskColor = 255)
rasterList_regK4 <- patRegK(imageList4, target, k = 5, resampleFactor = 5, crop = c(200,2800,300,1800),
plot = TRUE, useBlockPercentage = 90, maskOutline = outline, maskColor = 255)
rasterList_regK5 <- patRegK(imageList5, target, k = 5, resampleFactor = 5, crop = c(200,2800,300,1800),
plot = TRUE, useBlockPercentage = 90, maskOutline = outline, maskColor = 255)
顺利运行,你会看到分析的图片被读入,这一步花费时间可能较长,请耐心等待。
4. 保存提取的图片的信息,避免后续重新读取图片
save(rasterList_regK1, file = 'C:\\Users\\HP\\Desktop\\ps\\rasterList_regK1.rda')
save(rasterList_regK2, file = 'C:\\Users\\HP\\Desktop\\ps\\rasterList_regK2.rda')
save(rasterList_regK3, file = 'C:\\Users\\HP\\Desktop\\ps\\rasterList_regK3.rda')
save(rasterList_regK4, file = 'C:\\Users\\HP\\Desktop\\ps\\rasterList_regK4.rda')
save(rasterList_regK5, file = 'C:\\Users\\HP\\Desktop\\ps\\rasterList_regK5.rda')
5. 颜色模式的汇总
summedRaster_K1 <- sumRaster(rasterList_regK1, IDList1, type = 'k')
summedRaster_K2 <- sumRaster(rasterList_regK2, IDList2, type = 'k')
summedRaster_K3 <- sumRaster(rasterList_regK3, IDList3, type = 'k')
summedRaster_K4 <- sumRaster(rasterList_regK4, IDList4, type = 'k')
summedRaster_K5 <- sumRaster(rasterList_regK5, IDList5, type = 'k')
6. 绘制热图,展示颜色模式的变化
plotHeat(summedRaster_K1, IDList1, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(200,2800,300,1800), flipRaster = 'y', imageList = imageList1,
cartoonOrder = 'under', cartoonFill = 'black')
plotHeat(summedRaster_K2, IDList2, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(200,2800,300,1800), flipRaster = 'y', imageList = imageList2,
cartoonOrder = 'under', cartoonFill = 'black')
plotHeat(summedRaster_K3, IDList3, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(200,2800,300,1800), flipRaster = 'y', imageList = imageList3,
cartoonOrder = 'under', cartoonFill = 'black')
plotHeat(summedRaster_K4, IDList4, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(200,2800,300,1800), flipRaster = 'y', imageList = imageList4,
cartoonOrder = 'under', cartoonFill = 'black')
plotHeat(summedRaster_K5, IDList5, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(200,2800,300,1800), flipRaster = 'y', imageList = imageList5,
cartoonOrder = 'under', cartoonFill = 'black')
该步骤将动物各种肤色模式都提取出来了,选择你感兴趣的颜色模式进行分析,或逐一进行分析。本研究强调黑色素模式的分析,选择模式三进行后续分析。
7. 选取你感兴趣的颜色模式绘制热图
colfunc <- inferno(100)
plotHeat(summedRaster_K1[[3]], IDList1, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(300,2800,300,1800), flipRaster = 'y', imageList = imageList1, cartoonOrder = 'under',
cartoonFill = 'black', colpalette = colfunc)
plotHeat(summedRaster_K2[[3]], IDList2, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(300,2800,300,1800), flipRaster = 'y', imageList = imageList2, cartoonOrder = 'under',
cartoonFill = 'black', colpalette = colfunc)
plotHeat(summedRaster_K3[[5]], IDList3, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(300,2800,300,1800), flipRaster = 'y', imageList = imageList3, cartoonOrder = 'under',
cartoonFill = 'black', colpalette = colfunc)
plotHeat(summedRaster_K4[[4]], IDList4, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(300,2800,300,1800), flipRaster = 'y', imageList = imageList4, cartoonOrder = 'under',
cartoonFill = 'black', colpalette = colfunc)
plotHeat(summedRaster_K5[[5]], IDList5, plotCartoon = TRUE, refShape = 'target', outline = outline,
crop = c(300,2800,300,1800), flipRaster = 'y', imageList = imageList5, cartoonOrder = 'under',
cartoonFill = 'black', colpalette = colfunc)
7. 对颜色模式进行k-means聚类
popList <- list(IDList1, IDList2,IDList3,IDList4,IDList5)
colList <- c("black", "gold","green","blue","red")
symbolList <- c(18,14,15,16,17)
# extract rasters for k_means cluster of interest and combine rasters
#(note that you will have to pick the correct color cluster manually (number between [[...]]))
rasterList_regK1 <- lapply(rasterList_regK1, function (x) x[[3]])
rasterList_regK2 <- lapply(rasterList_regK2, function (x) x[[3]])
rasterList_regK3 <- lapply(rasterList_regK3, function (x) x[[5]])
rasterList_regK4 <- lapply(rasterList_regK4, function (x) x[[4]])
rasterList_regK5 <- lapply(rasterList_regK5, function (x) x[[5]])
TotalList <- c(rasterList_regK1, rasterList_regK2,rasterList_regK3,rasterList_regK4,rasterList_regK5)
# Run and plot PCA
colfunc <- c("blue","lightblue","black","pink","red")
pcaOut <- patPCA(TotalList, popList, colList, symbolList = symbolList, plot = TRUE, plotType = 'points', plotChanges = TRUE, PCx = 1, PCy = 2,
plotCartoon = TRUE, refShape = 'target', outline = outline, colpalette = colfunc,
crop = c(300,2800,300,1800),flipRaster = 'y', imageList = imageList1, cartoonID = '5-14',
normalized = TRUE, cartoonFill = 'black', cartoonOrder = 'under', legendTitle = 'Predicted')
彩蛋
还可以叠加出动画效果哦