-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpix_per_colors.R
78 lines (71 loc) · 2.12 KB
/
pix_per_colors.R
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
## --- load packages
# if the packages are not yet installed, use install.packages()
library('png')
library('neatStats')
## --- settings
# set path to images whose pixels per colors is to be counted
original_path = path_neat('') # sets path to script's folder, see ?path_neat
# original_path = 'give/path/here' # custom path
## --- execute
if (exists("imgs_data")) {
rm(imgs_data)
}
setwd(original_path)
file_names = list.files(pattern = "\\.png$") # list all png files at path
for (f_name in file_names) {
cat("reading:", f_name, fill = T)
img = readPNG(f_name)
r_img = as.raster(img)
tab <- table(r_img)
tab <- data.frame(color = names(tab), count = as.integer(tab))
RGB <- t(col2rgb(tab$color))
tab <- cbind(tab, RGB)
main_colors <- tab[order(-tab$count),]
main_colors$color = as.character(main_colors$color)
if (length(main_colors$color) != 2) {
stop(
"The number of different colors in this image is not 2 but ",
length(main_colors$color),
'!'
)
}
if (!((
"#FFFFFF" %in% main_colors$color |
"#FFFFFFFF" %in% main_colors$color
) &
(
"#000000" %in% main_colors$color |
"#000000FF" %in% main_colors$color
)
)) {
stop("#FFFFFF/#000000 not in ", main_colors$color[1:2])
}
c_b = main_colors$count[main_colors$color == "#000000" |
"#000000FF" == main_colors$color]
c_w = main_colors$count[main_colors$color == "#FFFFFF" |
"#FFFFFFFF" == main_colors$color]
if ((c_b + c_w) != 90000) {
stop("c_b + c_w ", c_b + c_w)
}
thisimg = data.frame(
file = f_name,
black = c_b,
white = c_w,
stringsAsFactors = FALSE
)
if (exists("imgs_data")) {
imgs_data = rbind(imgs_data, thisimg)
} else {
imgs_data = thisimg
}
}
imgs_data$SUM = imgs_data$black + imgs_data$white
print(imgs_data)
## if you want to write table to txt file
# write.table(
# imgs_data,
# file = "color_ratios.txt",
# sep = "\t",
# quote = F,
# row.names = F
# )