I have several DEMs obtained from NASA-SRTM (https://www2.jpl.nasa.gov/srtm/).

I try reduce the noise infor these applying the function noise.filter ofwvtool package and receive the next message:

> library(raster)
> DEM <- raster("S04W075.hgt")
> DEMnoise <- noise.filter(DEM, n= 5, method = "gaussian")
Warning message:
In noise.filter(DEM, n = 5, method = "gaussian") :
  data must be grayscale image
> DEMnoise
[1] "data must be grayscale image"

I have to process several rasters, so I need the solution in R.

  • 1
    What is DEM? Is it a raster object as created by the raster package? You've not told us. wvtool uses objects that are not compatible with raster 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.
    – Spacedman
    Commented Oct 18, 2019 at 17:24
  • DEM is an acronym for a Digital Elevation Model, is a raster with topography data. That's right, I can't find a noise filtering procedures for a raster, therefore this query.
    – Marco
    Commented Oct 18, 2019 at 17:55
  • 1
    I know what DEM means, I don't know what your 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 your DEM has come from? Please make your questions as reproducible.
    – Spacedman
    Commented Oct 18, 2019 at 18:09
  • Thanks for the clarification, I have modified my question. @Jeffrey Evans gave me a perfect clarification about the limitation of the function noise.filter. I'm still looking for another function that allows me to do noise filtering.
    – Marco
    Commented Oct 18, 2019 at 18:29

2 Answers 2


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.


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.


This coerces the camphora matrix to a raster object and then to a matrix, adding the require attributes for the noise.filter function.

r <- raster(wvtool::crop(camphora,200,200))
 rmat <- as.matrix(r)
   attributes(rmat) <- list( dim=c(nrow(rmat),ncol(rmat)),  

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.

 plot(r, main="original")
 plot(r.filt, main="wvtool") 
 plot(r.smooth, main="gauss") 
  • I think this package is geared towards image processing TIFFs that have all that metadata in the TIFF tags. Its very poorly documented. Could raster::focal be used for gaussian smoothing?
    – Spacedman
    Commented Oct 18, 2019 at 18:10
  • @Spacedman, agreed one of the attributes indicates that the data was processed using ImageJ. And, yes good suggestion on using focal. I have a gaussian.kernel function for just this very reason. See my modified answer. Commented Oct 18, 2019 at 19:47

You can try the function 'blur' in the package 'spatstat' to apply a kernel based blur (Gaussian or otherwise) to your image. You can perform multiple iterations or adjust the parameters of the function until you achieve the desired result.

DEM_blurred <- blur(DEM, sigma=0.5, kernel="gaussian")
#adjust sigma value to change the dimensions of your smoothing kernel


Also I recommend reading the answer by @whuber on this post:What raster smoothing/generalization tools are available?

  • Please note that the sigma is the standard deviation of the Gaussian function. This does, in fact, change the dimension of the kernel but not really following an nXn expectation. Commented Oct 18, 2019 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.