Patternize实战1: 分析颜色模式的R包

--K-Means clustering

实战教程,只强调实操,目的意义请移步我上篇博文 https://www.jianshu.com/p/a01d26c61f77

一. 文件准备

1. 待分析动物照片,要求拍摄参数,拍摄条件一致。

饰纹姬蛙蝌蚪

2. ImageJ中打地标点,勾勒出动物轮廓,保存文本格式,可以比我这个更精细一些。

打地标点.png

图片准备就到这里,是不是很容易。

二. 代码实战

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

该步骤将动物各种肤色模式都提取出来了,选择你感兴趣的颜色模式进行分析,或逐一进行分析。本研究强调黑色素模式的分析,选择模式三进行后续分析。

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

颜色模式的主成分分析

彩蛋

还可以叠加出动画效果哦


陆不陆???
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,711评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,079评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,194评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,089评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,197评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,306评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,338评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,119评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,541评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,846评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,014评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,694评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,322评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,026评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,257评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,863评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,895评论 2 351