using PyPlot using NtToolBox n = 256; f0 = load_image("NtToolBox/src/data/hibiscus.png", n); figure(figsize = (5,5)) imageplot(f0) sigma = .04; using Distributions f = f0 .+ sigma.*rand(Normal(), n, n); figure(figsize = (5,5)) imageplot(clamP(f)) w = 3 w1 = 2*w + 1; # include("NtToolBox/src/ndgrid.jl") (X, Y) = meshgrid(1 : n, 1 : n) (dX, dY) = meshgrid(-w : w, -w : w) dX = reshape(dX, (1, 1, w1, w1)) dY = reshape(dY, (1, 1, w1, w1)) X = repeat(X, inner = [1, 1, w1, w1]) + repeat(dX, inner = [n, n, 1, 1]) Y = repeat(Y, inner = [1, 1, w1, w1]) + repeat(dY, inner = [n, n, 1, 1]); X[X .< 1] = 2 .- X[X .< 1] Y[Y .< 1] = 2 .- Y[Y .< 1] X[X .> n] = 2*n .- X[X .> n] Y[Y .> n] = 2*n .- Y[Y .> n]; I = X + (Y-1)*n for i in 1 : Base.div(n, w) for j in 1 : Base.div(n, w) I[i, j, :, :] = transpose(I[i, j, :, :]) end end Pi = f -> reshape(f[I], (n, n, w1*w1)) Pi(f); P = Pi(f); figure(figsize = (5,5)) for i in 1:16 x = rand(1:n) y = rand(1:n) imageplot(reshape(P[x, y, :], (w1, w1)), "", [4, 4, i]) end Pmean = f -> mean(Pi(f), 3) Pmean(f); figure(figsize = (5,5)) imageplot(Pmean(f)[:, :]) p = 100 psi = f -> f.^(1/p) ipsi = f -> f.^p figure(figsize = (5,5)) imageplot(Pmean(abs(f))[:, :] - ipsi(Pmean(psi(abs(f))))[:, :]) r = beta -> min(ceil(beta*w1*w1), w1*w1 - 1) subsample = (x, s) -> x[: , : , s] phi = (f, beta) -> subsample(sort(Pi(f), 3), Int(r(beta)) + 1) include("NtSolutions/denoisingadv_7_rankfilters/exo1.jl") ## Insert your code here. closing = f -> phi(f, 0) figure(figsize = (5,5)) imageplot(closing(f)) opening = f -> phi(f, 1) figure(figsize = (5,5)) imageplot(opening(f)) include("NtSolutions/denoisingadv_7_rankfilters/exo2.jl") ## Insert your code here. include("NtSolutions/denoisingadv_7_rankfilters/exo3.jl") ## Insert your code here. include("NtSolutions/denoisingadv_7_rankfilters/exo4.jl") ## Insert your code here. medfilt = f -> phi(f, 1/2) medfilt(f); figure(figsize = (5,5)) imageplot(medfilt(f)) include("NtSolutions/denoisingadv_7_rankfilters/exo5.jl") ## Insert your code here. figure(figsize = (5,5)) imageplot(f1)