options(warn=-1) # turns off warnings, to turn on: "options(warn=0)" library(imager) library(png) for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_general/", f, sep="")) } for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_signal/", f, sep="")) } source("nt_toolbox/toolbox_wavelet_meshes/meshgrid.R") options(repr.plot.width=3.5, repr.plot.height=3.5) n <- 256 N <- n**2 name <- 'nt_toolbox/data/flowers.png' x0 <- load_image(name, n) options(repr.plot.width=4, repr.plot.height=4) imageplot(x0) sigma <- 0.08 y <- x0 + sigma*as.cimg(rnorm(N)) options(repr.plot.width=4, repr.plot.height=4) imageplot(clamp(y)) cconv <- function(a, b){ c <- fft(a)*fft(b) return( Re(fft(c, inverse=T )/length(c)) ) } normalize <- function(h){ h/sum(as.vector(h)) } t <- t( c(0:(n/2 - 1), -(n/2):(-1)) ) Y <- meshgrid_2d(t, t)$X ; X <- meshgrid_2d(t, t)$Y h <- function(mu){ normalize(exp(-(X**2 + Y**2)/ (2*mu**2))) } options(repr.plot.width=8, repr.plot.height=4) mu <- 10 imageplot(fftshift_2d(h(mu)), 'h', c(1,2,1)) imageplot(fftshift_2d(Re(fft(h(mu)))), 'h_hat', c(1,2,2)) options(repr.plot.width=4, repr.plot.height=4) imageplot(h(mu)) denoise <- function(x,mu){ cconv(h(mu), x) } options(repr.plot.width=4, repr.plot.height=4) imageplot(denoise(as.matrix(y), mu)) options(repr.plot.width=6, repr.plot.height=6) source("nt_solutions/denoisingsimp_2b_linear_image/exo1.R") options(repr.plot.width=6, repr.plot.height=4) source("nt_solutions/denoisingsimp_2b_linear_image/exo2.R") options(repr.plot.width=4, repr.plot.height=4) imageplot(denoise(as.matrix(y), mu)) P <- as.matrix( (1/N) * ( abs(fft(x0))**2 ) ) h_w <- Re( fft(P / (P + sigma**2), inverse=T) / length(P) ) options(repr.plot.width=4, repr.plot.height=4) u <- fftshift_2d(h_w) imageplot( u[(n/2-9):(n/2+10),(n/2-9):(n/2+10)] ) options(repr.plot.width=4, repr.plot.height=4) imageplot( cconv(as.matrix(y),as.matrix(h_w)) )