This error is resulting from an error check within the function.
if (length(dim(x)) > 2) {
warning("data must be grayscale image")
}
Given this condition length(dim(x))
, a single band rasterLayer class object will always return 3 representing row, column and nlayers dimensions.
Whereas this function, in theory, operates on a matrix object it is not quite as simple as it would seem. There is attribute encoding that is necessary for the function to run. So, it is not as simple as just coercing a raster to a matrix. You can see this attribution in the example data where the class is matrix but there are additional attributes included. The function at hand requires these attributes to run and I do not see a function in the package that calculates/adds these attributes for a matrix object.
library(wvtools)
data(camphora)
class(camphora)
attributes(camphora)
I dropped these attributes in the camphora object and found that you receive an error. You can brute force the function by manually adding the require attributes. You could also just use a Guassian kernel for smoothing within the raster focal function. Something that I notice in comparing results is that the noise.filter function forces an 8-bit output.
library(raster)
library(spatialEco)
library(wvtool)
This coerces the camphora matrix to a raster object and then to a matrix, adding the require attributes for the noise.filter
function.
data(camphora)
r <- raster(wvtool::crop(camphora,200,200))
rmat <- as.matrix(r)
attributes(rmat) <- list( dim=c(nrow(rmat),ncol(rmat)),
bits.per.sample=8,
samples.per.pixel=1)
We then run the noise.filter and focal with gaussian.kernel functions. Since the sigma of the Gaussian kernel is not an argument in the noise.filter function it is difficult to directly compare but, the out rescaling to 8-bit is a little problematic. Not sure if this is something controllable by changing $bits.per.sample but, since this is not indicated in the functions help it is a moot point.
( r.filt <- raster(noise.filter(rmat,3,"median")) )
( r.smooth <- focal(r, w=gaussian.kernel(sigma=2, n=3),
fun=median) )
And, plot results.
par(mfrow=c(2,2))
plot(r, main="original")
plot(r.filt, main="wvtool")
plot(r.smooth, main="gauss")
raster
package? You've not told us.wvtool
uses objects that are not compatible withraster
package objects, being matrices with image processing metadata. The function documentation is not clear and does not have examples. If you can't find better documentation then there's probably better noise filtering procedures.DEM
object is. You think its a raster,noise.filter
thinks its not good enough for itself. How can anyone really tell what's going on without some idea of where yourDEM
has come from? Please make your questions as reproducible.noise.filter
. I'm still looking for another function that allows me to do noise filtering.