#' transform low brightness and low transparency to full transparency
#'
#' @param img raster object
#' @param alpha_treshold pixels with alpha under this value will have it set to 0
#' @param brightness_treshold pixels with brightness over this value will have alpha set to 0
#' @param reverse if reverse is true the darker pixels will have alpha set to 0
#' @export
make_transparent <- function(img, alpha_treshold=0,brightness_treshold=1,reverse=FALSE){
assertthat::assert_that(class(img)=="raster")
lng <- convert(img,to="long")
bri <- brightness(lng)
if (reverse)
lng[bri <= brightness_treshold,4] <- 0 else
lng[bri >= brightness_treshold,4] <- 0
lng[lng[,4] <= alpha_treshold,4] <- 0
img <- convert(lng,from="long",to="raster")
img
}
#' trim image
#'
#' @param img raster object
#' @param bottom,left,top,right where to trim
#' @param type rel (0 to 1) or abs (pixels)
#' @export
img_trim <- function(img, bottom=0,left=0,top=0,right=0,type=c("rel","abs")){
assertthat::assert_that(class(img)=="raster")
type <- match.arg(type)
img <- if(
is.character(img) && length(img) == 1) urlpath2matrix(img) else if(
length(dim(img))==3) array32matrix(img) else if(
is.raster(img)) as.matrix(img) else if(
is.matrix(img)) img
if(type =="rel"){
left <- round(left * ncol(img))
right <- round(right * ncol(img))
bottom <- round(bottom * nrow(img))
top <- round(top * nrow(img))
}
img <- img[(top+1):(nrow(img)-bottom),(left+1):(ncol(img)-right)]
img <- as.raster(img)
img
}
#' Trim automatically transparent area
#'
#' @param img raster object
#' @param alpha_treshold pixels with alpha under this value will have it set to 0
#' @param brightness_treshold pixels with brightness over this value will have alpha set to 0
#' @param reverse if reverse is true the darker pixels will have alpha set to 0
#' @export
auto_trim <- function(img, alpha_treshold=0,brightness_treshold=1,reverse=FALSE){
assertthat::assert_that(class(img)=="raster")
img <- convert(img,to="matrix")
img0 <- make_transparent(img,alpha_treshold,brightness_treshold,reverse)
lng <- rst2lng(img0)
mat0 <- matrix(lng[,4],nrow=attr(lng,"DIM")[1], attr(lng,"DIM")[2],byrow=FALSE)
empty_cols <- colSums(mat0) == 0
keep_cols <- cumsum(!empty_cols) != 0 & rev(cumsum(rev(!empty_cols) != 0))
empty_rows <- rowSums(mat0) == 0
keep_rows <- cumsum(!empty_rows) != 0 & rev(cumsum(rev(!empty_rows) != 0))
img <- img[keep_rows,keep_cols]
img <- as.raster(img)
img
}
#' Return non transparent area
#'
#' @param img raster object
#' @param type rel (0 to 1) or abs (pixels)
#' @param alpha_treshold pixels with alpha under this value will have it set to 0
#' @param brightness_treshold pixels with brightness over this value will have alpha set to 0
#' @param reverse if reverse is true the darker pixels will have alpha set to 0
#' @export
effective_area <- function(img, type=c("rel","abs"), alpha_treshold=0,brightness_treshold=1,reverse=FALSE){
assertthat::assert_that(class(img)=="raster")
type <- match.arg(type)
img0 <- make_transparent(img,alpha_treshold,brightness_treshold,reverse)
lng <- convert(img0,to="long")
if (type == "abs")
area <- sum(lng[,4]==0) else
area <- sum(lng[,4]==0)/nrow(lng)
area
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.