Commit 8370600f authored by Monnet Jean-Matthieu's avatar Monnet Jean-Matthieu

padding transfered to demFiltering

parent f393c481
......@@ -40,6 +40,7 @@ createDisk <- function(width=5)
#' @param nlFilter string. type of non-linear filter to apply: "None", "Closing" or "Median"
#' @param nlSize numeric. kernel width in pixel for non-linear filtering
#' @param sigmap numeric or matrix. if a single number is provided, sigmap is the standard deviation of the Gaussian filter in pixel, 0 corresponds to no smoothing. In case of matrix, the first column corresponds to the standard deviation of the filter, and the second to thresholds for image values (e.g. a filter of standard deviation specified in line \code{i} is applied to pixels in image which values are between thresholds indicated in lines \code{i} and \code{i+1}). Threshold values should be ordered in increasing order.
#' @param padding boolean. Whether image should be padded by duplicating edge values before filtering to avoid border effects
#' @examples
#' data(chmchablais3)
#'
......@@ -65,7 +66,7 @@ createDisk <- function(width=5)
#' @seealso \code{\link{maximaDetection}}, filters of imager package: \code{\link[imager]{mclosing}}, \code{\link[imager]{medianblur}}, \code{\link[imager]{deriche}}
#' @return A list of two cimg or a RasterStack objects: image after non-linear filter and image after both filters
#' @export
demFiltering <- function(dem, nlFilter="Closing", nlSize=5, sigmap=0.3)
demFiltering <- function(dem, nlFilter="Closing", nlSize=5, sigmap=0.3, padding=TRUE)
{
# convert rasterLayer to cimg object if necessary
if (class(dem)[1] =="RasterLayer")
......@@ -75,6 +76,21 @@ demFiltering <- function(dem, nlFilter="Closing", nlSize=5, sigmap=0.3)
dem.c <- dem
}
#
if (padding)
{
# padding number of cells is maximum of half width of non linear filter or ceiling value of three times sigmap
if (!is.null(dim(sigmap)))
{dummy <- max(sigmap[,1])} else {dummy <- sigmap}
border.size <- max((nlSize-1)/2+1, ceiling(dummy*3))
# convert to matrix
dem.c <- as.matrix(dem.c)
# duplicate columns
dem.c <- dem.c[,c(rep.int(1,border.size), 1:ncol(dem.c), rep.int(ncol(dem.c), border.size))]
# duplicate rows
dem.c <- dem.c[c(rep.int(1,border.size), 1:nrow(dem.c), rep.int(nrow(dem.c), border.size)),]
dem.c <- imager::as.cimg(dem.c)
}
#
# non-linear filtering
dem.nl <- dem.c
if (nlFilter=="Closing")
......@@ -116,6 +132,14 @@ demFiltering <- function(dem, nlFilter="Closing", nlSize=5, sigmap=0.3)
dem.gs <- dem.nl
}
}
# remove padding
if (padding)
{
dem.nl <- as.matrix(dem.nl)
dem.nl <- imager::as.cimg(dem.nl[(border.size+1):(nrow(dem.nl)-border.size), (border.size+1):(ncol(dem.nl)-border.size)])
dem.gs <- as.matrix(dem.gs)
dem.gs <- imager::as.cimg(dem.gs[(border.size+1):(nrow(dem.gs)-border.size), (border.size+1):(ncol(dem.gs)-border.size)])
}
# convert cimg objects to rasterLayer if necessary
if (class(dem)[1] =="RasterLayer")
{
......@@ -371,7 +395,7 @@ rasterZonalStats <- function(segms,dem.nl,fun=max)
dummy <- stats::aggregate(as.numeric(dem.nl), by=list(as.numeric(segms)), FUN=fun)
dem.wh <- dummy$x[match(as.numeric(segms), dummy$Group.1)]
#
dem.wh <- imager::as.cimg(matrix(dem.wh,ncol(segms)))
dem.wh <- imager::as.cimg(matrix(dem.wh,nrow(segms)))
#
# convert to raster if necessary
if (israster)
......@@ -521,12 +545,6 @@ treeSegmentation <- function(dem, nlFilter="Closing", nlSize=5, sigma=0.3, dmin=
temp <- demFiltering(dem, nlFilter=nlFilter, nlSize=nlSize, sigmap=sigma)
dem.nl <- temp$non.linear.image
dem.gs <- temp$smoothed.image
# replace borders in smoothed image by borders in non.linear.image to avoid border effect
border.size <- (nlSize-1)/2+1
# row borders
dem.gs[c(1:border.size, (nrow(dem)-border.size+1):nrow(dem)), ] <- dem.nl[c(1:border.size, (nrow(dem)-border.size+1):nrow(dem)), ]
# col borders
dem.gs[, c(1:border.size, (ncol(dem)-border.size+1):ncol(dem))] <- dem.nl[, c(1:border.size, (ncol(dem)-border.size+1):ncol(dem))]
#
# maxima detection
maxi <- maximaDetection(dem.gs,raster::res(dem)[1])
......
......@@ -4,7 +4,8 @@
\alias{demFiltering}
\title{Image pre-processing (non-linear filtering and Gaussian smoothing)}
\usage{
demFiltering(dem, nlFilter = "Closing", nlSize = 5, sigmap = 0.3)
demFiltering(dem, nlFilter = "Closing", nlSize = 5, sigmap = 0.3,
padding = TRUE)
}
\arguments{
\item{dem}{cimg object (e.g. obtained with \code{\link[imager]{as.cimg}}) or Raster Layer object (e.g. obtained with \code{\link[raster]{raster}})}
......@@ -14,6 +15,8 @@ demFiltering(dem, nlFilter = "Closing", nlSize = 5, sigmap = 0.3)
\item{nlSize}{numeric. kernel width in pixel for non-linear filtering}
\item{sigmap}{numeric or matrix. if a single number is provided, sigmap is the standard deviation of the Gaussian filter in pixel, 0 corresponds to no smoothing. In case of matrix, the first column corresponds to the standard deviation of the filter, and the second to thresholds for image values (e.g. a filter of standard deviation specified in line \code{i} is applied to pixels in image which values are between thresholds indicated in lines \code{i} and \code{i+1}). Threshold values should be ordered in increasing order.}
\item{padding}{boolean. Whether image should be padded by duplicating edge values before filtering to avoid border effects}
}
\value{
A list of two cimg or a RasterStack objects: image after non-linear filter and image after both filters
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment