nnn nnn - 1 month ago 24
R Question

Image smoothing in R

How can I smooth this picture in R, so that only two peaks remain?

If this would be 1d data, I would do a running mean or fit a regression function to it. But I did not find very specific information about applying these methods on a 2d matrix. For example I tried to use

filter()
from the
stats
package.

I also thought about kriging, but this is more about interpolation, is it?

spectrogram

nnn nnn
Answer

The package spatstat holds a function blur() that applicates a gaussian blur. This smothes the picture in a way, that most of the noise disappears and the two main peaks are clearly distinctable.

The effect can be seen in the following picture and is quite remarkable, especially in the 3d plot.

effects of blurring

The code to generate the picture was:

library(jpeg)
library(fields)
library(spatstat)

picture <- readJPEG("~/Downloads/spectrogram.png.jpeg")
picture2 <- as.matrix(blur(as.im(picture), sigma=6))

layout(matrix(c(1:4), nrow=2))
image.plot(picture, col=gray.colors(50), main="original image", asp=1)
image.plot(picture2, col=gray.colors(50), main="blurred with sigma = 6", asp=1)
drape.plot(1:nrow(picture), 1:ncol(picture), picture, border=NA, theta=0, phi=45, main="original spectrogram")
drape.plot(1:nrow(picture), 1:ncol(picture), picture2, border=NA, theta=0, phi=45, main="blurred with sigma = 6")