-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.R
More file actions
96 lines (67 loc) · 2.9 KB
/
Copy pathProcess.R
File metadata and controls
96 lines (67 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
library(EBImage)
library(tidyverse)
################################################################################
# 20231116 MPM First basic version
# 20231117 Replaced ggplot to make final images, created a list/object for output
#
#
################################################################################
version=0.3
########################### INSTRUCTIONS ################################################
# Just edit the input and output directories. Make sure images are JPGs and have
# the JPG extension
indir <- 'input'
outdir <- 'data/'
######################## End Edit ###################
# Main Function -----------------------------------------------------------
ProcessImage <- function(imgname,name,dir){
#Filter
img <- readImage(paste0(dir,imgname))
#Need to resize thelong edge of the image if too large.
if(dim(img)[1]>3000| dim(img)[2]>3000){
if(dim(img)[1] > dim(img)[2]){
img <- resize(img,w=3000)
} else {
img <- resize(img,h=3000)
}
}
#Perhaps Brush size should be dependent on image size, given we resize should be fine.
brushsize=5
w = makeBrush(size = brushsize, shape = 'disc', sigma = 2)
img <- filter2(img, w)
imgDm <- dim(img)
imgRGB <- data.frame(
x = rep(1:imgDm[1], rep = imgDm[2]),
y = rep(imgDm[2]:1, each=imgDm[1]),
R = as.vector(img[,,1]),
G = as.vector(img[,,2]),
B = as.vector(img[,,3])
)
kMeans <- kmeans(imgRGB[, c("G", "B")], centers = 4, nstart=5)
imgRGB$color <- plyr::mapvalues(kMeans$cluster,from=order(rowMeans(kMeans$centers)),to=c('red','green','blue','black'))
pct <- imgRGB %>% filter(color !='black') %>% group_by(color) %>% summarise(n = n()) %>% mutate(pct = n/sum(n))
img <- Image(matrix(imgRGB$color,nrow = max(imgRGB$x),ncol=max(imgRGB$y)))
return(list(name = name,data=imgRGB,img=img,pct=pct))
}
dir.create(outdir)
images <- list.files(indir,full.names = F)
r <- map(images,~ProcessImage(.x, name=gsub('.JPG|.jpg','',.x), dir=indir))
# Create Summary Barplot --------------------------------------------------
PctSumm <- map(r, \(d){
d$pct$name <- d$name
d$pct
}) %>% bind_rows() %>% select(-n) %>% mutate(pct=pct*100,
damage=plyr::mapvalues(color, from=c('red','green','blue'),to=c('severe','moderate','normal'))
) %>%
mutate(damage = factor(damage, levels=c('severe','moderate','normal')))
bg1 <- ggplot(PctSumm,aes(x=name,y=pct,fill=damage)) + geom_bar(stat="identity") + theme_bw() +
scale_fill_manual(values = c('red','green','blue'))
ggsave(paste0(outdir,'PctSummary.png'),bg1)
# Write Summary File ------------------------------------------------------
PctSumm %>%
pivot_wider(values_from=pct,names_from = name) %>%
write_csv(paste0(outdir,'PrecentSummary.csv'))
# Write out image files. -------------------------------------------------
walk(r, \(d){
writeImage(d$img,files=paste0(outdir,'/',d$name,'.jpg'))
})