forked from agiovann/CalBlitz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.jl
219 lines (184 loc) · 5.21 KB
/
sandbox.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
# """
# T Macrina
# 151111
# Functions to improve the CalBlitz Python alignments using Julia
# """
using ImageRegistration
using Images
using ImageView
using FixedPointNumbers
using HDF5
include("moviecontrols.jl")
fn = "images/k26_v1_176um_target_pursuit_002_013.h5"
fn = "images/M_FLUO.h5"
fn = "images/demoMovie.h5"
fn = "demoMovie_mc.h5"
fn = "demoMovie.tif"
function gray16_to_int16(a)
return round(Int16, convert(Array{Float64}, data(a))*2^16)
end
function load_movie(fn, signed=false)
println(fn)
if fn[end-1:end] == "h5"
return h5read(fn, "mov")
else
return convert(Array{Float64}, data(Images.load(fn)))
end
end
function load_movie_slice(fn, slice)
return h5read(fn, "mov", slice)
end
function normalize(mov)
mov = mov .+ minimum(mov)
return mov ./ maximum(mov)
end
function view_movie(mov)
opts = Dict(:pixelspacing => [1,1], :xy => ["y","x"])
img = Image(mov, timedim=3)
return view(img; opts...)
end
function save_movie(fn, mov)
# assert(typeof(mov) == Array{Float64,3})
f = h5open(fn, "w")
@time f["mov", "chunk", (32,32,1)] = mov
close(f)
end
function median3d{T}(v::AbstractArray{T})
m = zeros(T, size(v)[1:2]...)
for i=1:size(v,1), j=1:size(v,2)
m[i,j] = median(v[i,j,:])
end
return m
end
function create_params(mesh_dist, block_size, search_r, min_r)
return Dict( "mesh_dist" => mesh_dist,
"block_size" => block_size,
"search_r" => search_r,
"min_r" => min_r)
end
function align(mov)
template = generate_template(mov, 10)
params = create_params(16, 8, 8, 0)
return align_stack(mov, template, params)
end
function generate_random_template(mov, n=4)
indices = randperm(size(mov,3))[1:n]
sample = mov[:,:,indices]
sample_template = median3d(sample)
params = create_params(16, 8, 8, 0)
template, offsets, matches = align_stack(sample, sample_template, params)
return template[:,:,1]
end
function generate_template(mov, n=4)
sample = mov[:,:,1:n:end]
sample_template = median3d(sample)
params = create_params(16, 8, 8, 0)
template, offsets, matches = align_stack(sample, sample_template, params)
return template[:,:,1]
end
function align_stack{T}(mov::Array{T}, template, params)
p = 3
matches = []
frames = []
offsets = []
for k in 1:size(mov, 3)
# mesh, blockmatches = blockmatch(mov[:,:,k], template, params=params)
# tform = calculate_translation(blockmatches)
blockmatches, max_r, xc = get_max_xc_vector(mov[p:end-p,p:end-p,k], template)
tform = eye(3)
tform[3,1:2] = blockmatches - [p p]
frame, offset = imwarp(mov[:,:,k], tform)
push!(frames, frame)
push!(offsets, offset)
push!(matches, blockmatches)
end
maxn, maxm = maximum(hcat(map(collect, map(size, frames))...), 2)
mini, minj = minimum(hcat(offsets...), 2)
# n, m = maxn-mini, maxm-minj
n, m = size(mov[:,:,1])
new_mov = zeros(T, maxn, maxm, length(frames))
# println((maxn, maxm))
# println((mini, minj))
# println((n, m))
# println((i,j))
bb = BoundingBox(0, 0, n-1, m-1)
print(bb)
for (k, (frame, offset)) in enumerate(zip(frames, offsets))
new_mov[:,:,k] = rescopeimage(frame, offset, bb)
end
return new_mov, offsets, matches
end
"""
Determine maximum dimensions of list of dimensions with different sizes
"""
function find_largest_dimensions(dims)
maxn, maxm = dims[1]
for dim in dims
n, m = dim
if n > maxn; maxn = n; end
if m > maxm; maxm = m; end
end
return maxn, maxm
end
"""
`RESCOPE` - Crop/pad an image to fill a bounding box
new_img = rescope(img, offset, boundingbox)
Args:
* img: 2D or 3D array
* offset: 2-element array, specifying i & j offset from global origin
* bb: bounding box object in the global reference space
Returns:
* new_img: original img, cropped &/or extended with rows and columns of zeros
"""
function rescopeimage{T}(img::Array{T}, offset, bb)
z = zeros(T, bb.h+1, bb.w+1)
imgbb = BoundingBox(offset..., size(img,1)-1, size(img,2)-1)
xbb = imgbb - bb
if !isnan(xbb.i) & !isnan(xbb.j) & !isnan(xbb.h) & !isnan(xbb.h)
crop_img = xbb.i-offset[1]+1 : xbb.i-offset[1]+1+xbb.h,
xbb.j-offset[2]+1 : xbb.j-offset[2]+1+xbb.w
crop_z = xbb.i-bb.i+1:xbb.i-bb.i+1+xbb.h, xbb.j-bb.j+1:xbb.j-bb.j+1+xbb.w
z[crop_z...] = img[crop_img...]
end
return z
end
function compare_movies(movA, movB)
movC = stack_movies(movA, movB)
return view_movie(movC)
end
function stack_movies(movA, movB)
nA, mA, lA = size(movA)
nB, mB, lB = size(movB)
assert(lA == lB)
n, m = min([nA, mA], [nB, mB])
border = ones(n, 10, lA)
return cat(2, movA[1:n,1:m,:], border, movB[1:n,1:m,:])
end
function inspect_alignment(mov, fps=10)
e = Condition()
imgc, img2 = view_movie(mov[:,:,1:100])
state = imgc.navigationstate
set_fps!(state, fps)
errors = 0
c = canvas(imgc)
win = Tk.toplevel(c)
bind(win, "<KP_Enter>", path->count())
bind(win, "<Delete>", path->reset())
bind(win, "<Destroy>", path->end_count())
function count()
errors += 1
println(errors)
end
function reset()
errors = 0
println(errors)
end
function end_count()
notify(e)
bind(win, "<KP_Enter>", path->path)
bind(win, "<Delete>", path->path)
bind(win, "<Destroy>", path->path)
end
wait(e)
return errors
end