R/thresh2.R

Defines functions thresh2

Documented in thresh2

#' Adaptive Threshold with a Moving Disc Window
#' 
#' Threshold an image using a disc rather than rectangle.
#' 
#' @param x \code{Image} object or array.
#' @param width Half-width of the moving disc window.
#' @param offset Thresholding value from the averaged value.
#' @param boundary Behavior at image border, where default is to extend the 
#' image beyond the borders, which differs from the default for
#' \code{\link[EBImage]{filter2}}.
#' 
#' @details
#' 
#' This function returns the binary image generated by a local thresholding 
#' algorithm using a moving disc. 
#' 
#' @examples
#' 
#'   x <- readImage(system.file("extdata", "by_stack/file001.tif", package = "virustiter"))
#'   xb <- normalize(gblur(x[,,c(1,3)], 2))
#'   xt <- thresh2(xb, width = 12, offset = 0.05)
#'   plot(combine(xb, xt), all = TRUE)
#' 
#' @import EBImage
#' 
#' @export
#'
thresh2 <- function(x, width, offset, boundary = c("replicate", "circular"))
{
# adjust for arguments of dim(x)[3] == 1
	dm <- dim(x)
	if (is.na(dm[3]) || dm[3] == 1)
		dim(x) <- dm[1:2]
	boundary <- match.arg(boundary)
	r <- width - width%%2 + 1
	f <- makeBrush(r, shape = "disc")
	f <- f/sum(f)
	x <- x > (filter2(x, f, boundary) + offset)
	dim(x) <- dm
	return(x)
}
ornelles/virustiter documentation built on March 15, 2024, 9:28 a.m.