R/rasterfilter.R

Defines functions smudge rasterfilter

Documented in rasterfilter smudge

#'
#'    rasterfilter.R
#'
#'    raster filters implemented directly
#'
#'    $Revision: 1.7 $ $Date: 2022/05/21 09:52:11 $
#'

rasterfilter <- function(X, f) {
  X <- as.im(X)
  dimX <- dim(X)
  f <- as.matrix(f)
  if(!all(dim(f) == 3))
    stop("f should be a 3 x 3 matrix or image")
  #' handle NA
  v <- as.double(X$v)
  if(hasna <- anyNA(v)) {
    isna <- is.na(v)
    v[isna] <- 0
  }
  #' compute
  z <- .C(SG_raster3filter,
          nx = as.integer(dimX[2]),
          ny = as.integer(dimX[1]),
          a  = as.double(v),
          w  = as.double(f),
          b  = as.double(numeric(prod(dimX))),
          PACKAGE="spatstat.geom")
  z <- z$b
  #' handle NA
  if(hasna)
    z[isna] <- NA
  # replace
  X[] <- z
  return(X)
}

#'  antialiasing
smudge <- function(X) {
  stopifnot(is.im(X))
  xstep <- X$xstep
  ystep <- X$ystep
  #' choose a very small bandwidth
  sigma <- min(xstep, ystep)/2
  #' match variance: 2 p step^2 = sigma^2
  px <- sigma^2/(2 * xstep^2)
  py <- sigma^2/(2 * ystep^2)
  f <- outer(c(py, 1-2*py, py), c(px, 1-2*px, px), "*")
  #' compute 
  Z <- rasterfilter(X, f)
  attr(Z, "sigma") <- sigma
  return(Z)
}

Try the spatstat.geom package in your browser

Any scripts or data that you put into this service are public.

spatstat.geom documentation built on Oct. 20, 2023, 9:06 a.m.