For some time I've been trying to get good registrations when there is inconsistency in sampling locations along the z-axis. This is common with high-speed OCPI recordings. We're doing a lot to handle this at the level of the microscope hardware and acquisition software, but it would be good if our registration algorithm could clean up the last bit of it. I wasn't satisfied with the results so i made a synthetic image volume and simulated the situation (script below). I'm seeing that the loss (aka mismatch) actually increases when registering these volumes. I didn't yet track down why that is, and this can probably be triggered by a simpler test case, but here's a demo that I know demonstrates the issue (note that it requires a utility package BinaryVolumes, another HolyLab repo.)
using BlockRegistration, BlockRegistrationScheduler, RegisterDeformation, RegisterMismatch
using AffineTransforms
using ImageView2, Images
using FixedSizeArrays
using JLD
using Interpolations
using BinaryVolumes
#utility function for running Apertures registration on a pair of images
function _reg_def(fixed, moving, knots, mxshift; lambda = 0.01)
λ = lambda
algorithm = Apertures[Apertures(fixed, knots, mxshift, λ, identity; pid=myid(), correctbias=false) for i = 1:1]
mon = monitor(algorithm, (), Dict{Symbol,Any}(:u=>ArrayDecl(Array{Vec{3,Float64},3}, ([convert(Int,x.len) for x in knots]...))))
mon[1][:mismatch] = 0.0
fileout = tempname()
@time driver(fileout, algorithm, moving, mon)
u = JLD.load(fileout, "u")
mm = JLD.load(fileout, "mismatch")
return u, mm
end
#add jitter in z sampling location, simulating inconsistencies in piezo position when using OCPI under certain conditions
function z_jitter{T}(img::Array{T,3}, npix::Float64)
@assert npix < 0.5 && npix >= 0.0
etp = extrapolate(interpolate(img, BSpline(Linear()),OnGrid()), Flat())
out = zeros(eltype(img), size(img))
z_def = Float64[]
for i in 1:size(img,3)
r = (rand()*2*npix)-npix
push!(z_def, r)
for y = 1:size(img,2)
for x = 1:size(img,1)
out[x,y,i] = etp[x,y,i+r] #Interpolations doesn't currently support slicing
end
end
end
return out, z_def
end
#create template ellipsoid volume
radii = [10;20;15]*2
num_pad = 20
line_spacings = [6;6;6];
vmask = ellipsoid_volume(radii; fill_val=true, num_pad = num_pad);
vbound = find_boundaries(vmask; val=true);
#draw grid lines. Make density higher on one side so that there's no
#ambiguity in orientation. Only need to do this for n-1 dimensions
sz = [size(vmask)...];
vgrid = deepcopy(vbound);
for i = 1:length(line_spacings)
line_idxs_a = floor(Int,linspace(1,sz[i]/2, floor(Int,sz[i]/line_spacings[i])));
line_idxs_b = floor(Int,linspace(sz[i]/2,sz[i], floor(Int, sz[i]/(1.5*line_spacings[i]))))[2:end];
cond = x->x==true
vgrid = draw_lines!(vgrid, vmask, i, line_idxs_a, cond; fill_val=true);
end
vgrid = map(Float64, vgrid);
fixed = vgrid
moving, z_def = z_jitter(vgrid, 0.49); #plus or minus a half frame of jitter
mxshift = (3,3,3)
gridsize = (2,2,2)
knots = map(d->linspace(1,size(fixed,d),gridsize[d]), (1:ndims(fixed)...))
u, mm = _reg_def(fixed,moving,knots,mxshift;lambda=0.0);
d = griddeformations(u, knots)
warped = warp(moving, d[1])
#mismatch is worse after registration
imshow(colorview(RGB, moving, zeroarray, fixed))
imshow(colorview(RGB, warped, zeroarray, fixed))
@show ratio(mismatch0(moving,fixed), NaN)
@show mm
For some time I've been trying to get good registrations when there is inconsistency in sampling locations along the z-axis. This is common with high-speed OCPI recordings. We're doing a lot to handle this at the level of the microscope hardware and acquisition software, but it would be good if our registration algorithm could clean up the last bit of it. I wasn't satisfied with the results so i made a synthetic image volume and simulated the situation (script below). I'm seeing that the loss (aka mismatch) actually increases when registering these volumes. I didn't yet track down why that is, and this can probably be triggered by a simpler test case, but here's a demo that I know demonstrates the issue (note that it requires a utility package
BinaryVolumes, another HolyLab repo.)