-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImageReconstructor.jl
227 lines (204 loc) · 7.57 KB
/
ImageReconstructor.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
include("ResourceManager.jl")
# Activate environment and download required packages.
using Pkg
Pkg.activate(@__DIR__)
# Import all necessary modules
using Colors
using Statistics
using Random
using StatsBase
mutable struct ThreadBuffer
target::Array{Float64,1}
stencil::Stencil
end
# Gets a stencil based on the input coordinates
function assignStencil!(
x::Int64,
y::Int64,
data::ResourceData,
stencilbuffer::Array{ThreadBuffer,1},
n::Int64,
)::Nothing
stencilbuffer[n].target[1] = data.averageImage[x, y, 1]
stencilbuffer[n].target[2] = data.averageImage[x, y, 2]
stencilbuffer[n].target[3] = data.averageImage[x, y, 3]
best::Float64 = msd(data.stencils[1].average, stencilbuffer[n].target)
stencilbuffer[n].stencil = data.stencils[1]
len::Int64 = length(data.stencils)
i::Int64 = 2
while i::Int64 <= len
stencil::Stencil = data.stencils[i]
this::Float64 = msd(stencil.average, stencilbuffer[n].target)
if this < best
best = this
stencilbuffer[n].stencil = stencil
end
i += 1
end
return
end
function msd2(a::Array{UInt8, 3}, b::Array{UInt8, 3}, xrange::UnitRange{Int64}, yrange::UnitRange{Int64}, zrange::UnitRange{Int64}, n::Int64)::Float64
r::Int64 = 0
for x in xrange
for y in yrange
for z in zrange
@inbounds r += abs2(a[x, y, z] - b[x, y, z])
end
end
end
return r / n
end
function msd3(a::Array{UInt8, 3}, stencil::Array{UInt8, 3}, xrange::UnitRange{Int64}, yrange::UnitRange{Int64}, zrange::UnitRange{Int64}, n::Int64)::Float64
r::Int64 = 0
sx::Int64 = 1
for x in xrange
sy::Int64 = 1
for y in yrange
sz::Int64 = 1
for z in zrange
@inbounds r += abs2(a[x, y, z] - stencil[sx, sy, sz])
sz+=1
end
sy+=1
end
sx+=1
end
return r / n
end
function genImage(data::ResourceData)::Nothing
# Variables used in the calculations
img::Array{UInt8,3} = data.groundTruth
result::Array{UInt8,3} = data.result
stencilWidth::Int64 = data.stencilWidth
stencilHeight::Int64 = data.stencilHeight
totalpixels::Int64 = stencilWidth * stencilHeight * 3
# Variables necessary for workload
width::Int64 = size(img, 1) - stencilWidth
height::Int64 = size(img, 2) - stencilHeight
thread_ops::Int64 = data.iterations ÷ Threads.nthreads()
# Create and populate buffers for each thread
stencilbuffer::Array{ThreadBuffer,1} = ThreadBuffer[]
memorybuffer::Array{Array{UInt8,3}} = Array{Float64,3}[]
for n = 1:Threads.nthreads()
push!(
stencilbuffer,
ThreadBuffer(
[0, 0, 0],
data.stencils[1],
),
)
push!(memorybuffer, zeros(UInt8, size(data.stencils[1].color)))
end
zrange::UnitRange{Int64} = 1:3
@time @inbounds Threads.@threads for n::Int64 = 1:Threads.nthreads()
for _ = 1:thread_ops
x::Int64 = rand(1:width)
y::Int64 = rand(1:height)
# Get the stencil that corresponds to that location
assignStencil!(x, y, data, stencilbuffer, n)
invertedOpacity = stencilbuffer[n].stencil.invertedOpacity
pixels = stencilbuffer[n].stencil.color
xrange::UnitRange{Int64} = x:x+stencilWidth-1
yrange::UnitRange{Int64} = y:y+stencilHeight-1
a::Int64 = 1
for i::Int64 in xrange
b::Int64 = 1
for j::Int64 in yrange
memorybuffer[n][a, b, 1] =
(result[i, j, 1] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 1]
memorybuffer[n][a, b, 2] =
(result[i, j, 2] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 2]
memorybuffer[n][a, b, 3] =
(result[i, j, 3] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 3]
b += 1
end
a += 1
end
# Check the old distance on that position of the image
distances1::Float64 =
msd2(data.groundTruth, data.result, xrange, yrange, zrange, totalpixels)
# Check the new distance on that position of the image
distances2::Float64 =
msd3(data.groundTruth, memorybuffer[n], xrange, yrange, zrange, totalpixels)
# If it improved then save it
if distances1 > distances2
data.result[xrange, yrange, zrange] = memorybuffer[n]
end
end
end
end
# Fills the image with some stencils
function initialfill(data::ResourceData)::Nothing
# Variables used in the calculations
img::Array{UInt8,3} = data.groundTruth
result::Array{UInt8,3} = data.result
stencilWidth::Int64 = data.stencilWidth
stencilHeight::Int64 = data.stencilHeight
# Variables necessary for workload
width::Int64 = size(img, 1) - stencilWidth
height::Int64 = size(img, 2) - stencilHeight
numpositions::Int64 =
(size(img, 1) * size(img, 2)) -
(stencilHeight * size(img, 1) + stencilWidth * size(img, 2)) +
stencilHeight * stencilWidth
allpositions::Array{Int64,1} = shuffle(1:numpositions)
thread_ops::Int64 = numpositions ÷ Threads.nthreads()
# Create and populate buffers for each thread
executionbuffer::Array{Array{Int64,1}} = Array{Int64,1}[]
stencilbuffer::Array{ThreadBuffer} = ThreadBuffer[]
for n = 1:Threads.nthreads()
push!(
executionbuffer,
allpositions[(1+(n-1)*thread_ops):(n*thread_ops)],
)
push!(stencilbuffer, ThreadBuffer([0, 0, 0], data.stencils[1]))
end
GC.enable(false)
@time @inbounds Threads.@threads for n::Int64 = 1:Threads.nthreads()
for pos::Int64 in executionbuffer[n]
x::Int64 = pos % width + 1
y::Int64 = pos ÷ width + 1
# Get the stencil that corresponds to that location
assignStencil!(x, y, data, stencilbuffer, n)
invertedOpacity = stencilbuffer[n].stencil.invertedOpacity
pixels = stencilbuffer[n].stencil.color
a::Int64 = 1
for i::Int64 = x:x+stencilWidth-1
b::Int64 = 1
for j::Int64 = y:y+stencilHeight-1
result[i, j, 1] =
(result[i, j, 1] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 1]
result[i, j, 2] =
(result[i, j, 2] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 2]
result[i, j, 3] =
(result[i, j, 3] * invertedOpacity[a, b]) ÷ 255 +
pixels[a, b, 3]
b += 1
end
a += 1
end
end
end
GC.enable(true)
return
end
function reconstructImage(args::ReconstructionArguments)::Nothing
println("Loading all files...")
println(string("Using ", Threads.nthreads(), " threads"))
resourceData::ResourceData = getData(args)
println("Loaded all files")
if (!resourceData.improve)
println("Initial filling of the image to remove gaps, can take a long time")
initialfill(resourceData)
end
println("starting random reconstruction of the image")
genImage(resourceData)
println("Saving result...")
saveResult(args.resultFile, resourceData.result)
println("Saved result!\n All Done")
end