https://github.com/cran/spatstat
Raw File
Tip revision: f6c1811e0b8755c851c04178019e0c07db04e697 authored by Adrian Baddeley on 22 December 2016, 09:53:41 UTC
version 1.48-0
Tip revision: f6c1811
levelset.R
# levelset.R
#
#  $Revision: 1.5 $  $Date: 2015/01/15 07:10:37 $
#
# level set of an image

levelset <- function(X, thresh, compare="<=") {
  # force X and thresh to be evaluated in this frame
  verifyclass(X, "im")
  thresh <- thresh
  switch(compare,
         "<"  = { A <- eval.im(X < thresh) },
         ">"  = { A <- eval.im(X > thresh) },
         "<=" = { A <- eval.im(X <= thresh) },
         ">=" = { A <- eval.im(X >= thresh) },
         "==" = { A <- eval.im(X == thresh) },
         "!=" = { A <- eval.im(X != thresh) },
         stop(paste("unrecognised comparison operator", sQuote(compare))))
  W <- as.owin(eval.im(ifelse1NA(A)))
  return(W)
}

# compute owin containing all pixels where image expression is TRUE

solutionset <- function(..., envir) {
  if(missing(envir))
    envir <- parent.frame()
  A <- try(eval.im(..., envir=envir), silent=TRUE)
  if(inherits(A, "try-error"))
    A <- try(eval(..., envir=envir), silent=TRUE)
  if(inherits(A, "try-error"))
    stop("Unable to evaluate expression")
  if(!is.im(A))
    stop("Evaluating the expression did not yield a pixel image")
  if(A$type != "logical")
    stop("Evaluating the expression did not yield a logical-valued image")
  W <- as.owin(eval.im(ifelse1NA(A)))
  return(W)
}


back to top