Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library('jpeg'); library('png'); library('gplots')
- file <- file.choose()
- if (length(grep(pattern = '\\.png$', x = file, ignore.case = TRUE)) > 0) {
- imgdat <- readPNG(source = file)
- } else if (length(grep(pattern = '\\.jp[e]?g$', x = file, ignore.case = TRUE)) > 0) {
- imgdat <- readJPEG(source = file)
- }
- n <- 6; xlim <- c(0, dim(imgdat)[2]); ylim <- c(0, dim(imgdat)[1])
- rb <- paste(col2hex(cname = c('red', 'orange', 'yellow', 'green', 'blue', 'purple')), '77', sep = '')
- barx <- rep(c(xlim[2], xlim[2], xlim[1], xlim[1]), times = n)
- bary <- seq(ylim[2], ylim[1], length.out = n+1)[c(0:1, 1:0) + rep(1:n, each = 4)]
- rectcoords <- cbind(matrix(barx, nc = 4, byrow = TRUE), matrix(bary, nc = 4, byrow = TRUE), 1:n)
- png(filename = 'rainbow_overlay.png', width = xlim[2], height = ylim[2])
- par(bty = 'n', mai = rep(0, 4), oma = rep(0, 4), xaxs = 'i', yaxs = 'i')
- plot(0, 0, xlim = xlim, ylim = ylim, type = 'n', asp = 1)
- rasterImage(image = imgdat, xleft = xlim[1], ybottom = ylim[1], xright = xlim[2], ytop = ylim[2])
- apply(rectcoords, MARGIN = 1, FUN = function(x) polygon(x = x[1:4], y = x[5:8], border = NA, col = rb[x[9]]))
- dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement