-
Notifications
You must be signed in to change notification settings - Fork 0
/
L0Smoothing.jl
88 lines (75 loc) · 2.48 KB
/
L0Smoothing.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Images, TestImages, ImageFiltering, MosaicViews, BenchmarkTools
using FFTW
function my_diffy(img)
d = similar(img)
d[:, 1:end-1, :] .= @views img[:, 2:end, :] .- img[:, 1:end-1, :]
d[:, end:end, :] .= @views img[:, 1:1, :] .- img[:, end:end, :]
d
end
function my_diffyt(img)
d = similar(img)
d[:, 2:end, :] .= @views img[:, 1:end-1, :] .- img[:, 2:end, :]
d[:, 1:1, :] .= @views img[:, end:end, :] .- img[:, 1:1, :]
d
end
function my_diffx(img)
d = similar(img)
d[:, :, 1:end-1] .= @views img[:, :, 2:end] .- img[:, :, 1:end-1]
d[:, :, end:end] .= @views img[:, :, 1:1] .- img[:, :, end:end]
d
end
function my_diffxt(img)
d = similar(img)
d[:, :, 2:end] .= @views img[:, :, 1:end-1] .- img[:, :, 2:end]
d[:, :, 1:1] .= @views img[:, :, end:end] .- img[:, :, 1:1]
d
end
expanded_channelview(img::AbstractArray{T}) where T<:Colorant = channelview(img)
expanded_channelview(img::AbstractArray{T}) where T<:Gray = reshape(channelview(img), 1, size(img)...)
function l0smoothing(img, λ=2e-2, κ=2.0)
S = float.(expanded_channelview(img))
βmax = 1e5
fx = [1 -1]
fy = [1, -1]
D, N, M = size(S)
sizeI2D = (N, M)
sizeI2D_t = (M, N)
otfFx = freqkernel(centered(fx), sizeI2D)
otfFy = transpose(freqkernel(centered(transpose(fy)), sizeI2D_t))
Normin1 = fft(S, (2, 3))
Denormin2 = abs.(otfFx).^2 + abs.(otfFy ).^2
if D > 1
Denormin2 = repeat(reshape(Denormin2, 1, size(Denormin2)...), inner=(1, 1, 1), outer=(3, 1, 1))
else
Denormin2 = reshape(Denormin2, 1, size(Denormin2)...)
end
β = 2*λ
while β < βmax
Denormin = 1 .+ β*Denormin2
h = my_diffx(S)
v = my_diffy(S)
if D == 1
t = (h.^2+v.^2) .< λ/β
else
t = sum((h.^2+v.^2), dims=1) .< λ/β
t = repeat(t, inner=(1, 1, 1), outer=(D, 1, 1))
end
h[t] .= 0
v[t] .= 0
Normin2 = my_diffxt(h)
Normin2 = Normin2 + my_diffyt(v)
FS = (Normin1 + β*fft(Normin2, (2, 3))) ./ Denormin
S = real(ifft(FS, (2, 3)))
β = β*κ
end
if D == 1
return colorview(Gray, S[1,:,:])
else
return colorview(RGB, S)
end
end
img1 = load("pflower.jpg")
img2 = testimage("cameraman")
S1 = @btime l0smoothing(img1)
S2 = @btime l0smoothing(img2)
mosaicview(img1,img2, S1, S2;nrow=2, ncol=2, rowmajor=true)