Patternize實戰(zhàn)1: 分析顏色模式的R包

--K-Means clustering

實戰(zhàn)教程,只強調(diào)實操,目的意義請移步我上篇博文 http://www.itdecent.cn/p/a01d26c61f77

一. 文件準備

1. 待分析動物照片,要求拍攝參數(shù),拍攝條件一致。

飾紋姬蛙蝌蚪

2. ImageJ中打地標點,勾勒出動物輪廓,保存文本格式,可以比我這個更精細一些。

打地標點.png

圖片準備就到這里,是不是很容易。

二. 代碼實戰(zhàn)

1. 分析環(huán)境的準備

#所需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)

# 選取一個動物圖片作為目標,將在后期分析中以卡通形象出現(xiàn)。
target <- imageList5[['5_14']]

# 讀取動物輪廓地標文件
outline <- read.table('C:\\Users\\HP\\Desktop\\ps\\5_14.txt', h= F)

3. 比對顏色模式

# 對顏色模式進行比對,此處參數(shù)K值的調(diào)整可以改變提取顏色模式的數(shù)量
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. 保存提取的圖片的信息,避免后續(xù)重新讀取圖片


 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')
圖片.png

該步驟將動物各種膚色模式都提取出來了,選擇你感興趣的顏色模式進行分析,或逐一進行分析。本研究強調(diào)黑色素模式的分析,選擇模式三進行后續(xù)分析。

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)
圖片.png

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')

顏色模式的主成分分析

彩蛋

還可以疊加出動畫效果哦


陸不陸???
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容