-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridandscramble.R
78 lines (58 loc) · 2.45 KB
/
gridandscramble.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
gridandscramble <- function(importpath, exportpath, gridsize = 10) {
## this function will take an object image on a white background, grayscale it, scramble it
## after, it will add a grid of a desired block size (default is 10) to the object as often used in fMRI paradigms
## to localise LOC.
if (!require('imager')) install.packages('imager'); library('imager')
if (!require('purrr')) install.packages('purrr'); library('purrr')
if (!require('magick')) install.packages('magick'); library('magick')
## define scrambling function - from imager package creator barthelme' - with the possibility to edit the blocksize
scramble <- function(im,axis="x", blocksize = -1)
{
imsplit(im,axis, nb = blocksize) %>% { .[sample(length(.))] } %>% imappend(axis)
}
# read the image names
imfiles <- list.files(importpath, pattern = ".jpg|.png", full.names = TRUE, recursive = FALSE)
# print names
print(imfiles)
# read the images
theobjs <- map(imfiles, image_read)
print("IMAGE LIST IS LOADED")
# grayscale images
grims <- map(theobjs, ~image_convert(., colorspace = "gray"))
# convert in cimg for scrambling
grimsimgr <- map(grims, magick2cimg)
# scramble
scrlist <- map(grimsimgr, ~scramble(., blocksize = gridsize) %>%
scramble(axis = "y", blocksize = gridsize) %>%
scramble(blocksize = gridsize) %>%
scramble(axis = "y", blocksize = gridsize))
# convert back to magick format
scrimgs <- map(scrlist, cimg2magick)
print("IMAGE LIST IS SCRAMBLED")
# remove the cimg list and clean ram
rm(scrlist) %>% gc()
# add borders to both lists
bdgrims <- map(grims, ~image_border(., color = "black", geometry = "1x1"))
bdscrimgs <- map(scrimgs, ~image_border(., color = "black", geometry = "1x1"))
# pick up the names
imnames <- basename(imfiles)
# make the new paths
newnames_full <- paste0(exportpath, "/", imnames)
newnames_scr <- paste0(exportpath, "/scr_", imnames)
## add grid to the full cue images
for(img in seq_along(imnames)) {
theimg <- image_draw(bdgrims[[img]])
grid(nx = gridsize, ny = gridsize, lty = "solid", col = "black")
dev.off()
image_write(theimg, newnames_full[[img]],quality = 100)
}
print("IMAGE GRID ADDED TO FULL IMGS")
## add grid to the scrambled images
for(img in seq_along(imnames)) {
theimg <- image_draw(bdscrimgs[[img]])
grid(nx = gridsize, ny = gridsize, lty = "solid", col = "black")
dev.off()
image_write(theimg, newnames_scr[[img]],quality = 100)
}
print("IMAGE GRID ADDED TO SCRAMBLED IMGS")
}