-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.jl
62 lines (52 loc) · 1.88 KB
/
utils.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
export cart_to_mat, mat_to_vec, remove_background, gaussf
"""
cart_to_mat(cart)
converts a list of cartesian indices to a matrix with the first dimension corresponding to the dimension direction.
"""
function cart_to_mat(cart)
all_max_vec = zeros(length(cart[1]), length(cart))
n=1
for idx in cart
all_max_vec[:,n] = [Tuple(idx)...]
n += 1
end
return all_max_vec
end
"""
mat_to_vec(mat)
converts a 2D matrix to a vector of vectors, one vector for each column.
"""
function mat_to_vec(mat)
[mat[:,n] for n=1:size(mat,2)]
end
"""
gaussf(img::AbstractArray, sigma=1.0; dtype=Float32)
performs Gaussian filtering using FFTs. `sigma` denotes the kernel sizes along the various dimensions.
#arguments
+ `img`: the image to apply the Gaussian filter to
+ ``
"""
function gaussf(img::AbstractArray{T}, sigma=1.0; dtype=Float32) where (T <: Real)
dims=1:ndims(img)
shiftdims = dims[2:end]
img = dtype.(img)
f = rfft(img, dims)
return irfft(f .* ifftshift(gaussian(eltype(img), size(f), offset=CtrRFT, sigma=size(img) ./(2π .*sigma)), shiftdims), size(img, dims[1]), dims)
end
function gaussf(img::AbstractArray, sigma=1.0)
return (ifft(fft(img) .* ifftshift(gaussian(eltype(img), size(img), sigma=size(img) ./(2π*sigma)))))
end
"""
remove_background(img, sigma=5.0)
estimates the background by applying a Gaussian filter to the data and measuring for the minimum of this filtered data.
#arguments
+ `img`: the image to remove the background from
+ `sigma`: the kernelsize for the Gaussian filter
#returns
a tuple of the background-subtracted image and the background value.
Note that no zero-clipping is performed. The result type is by defaul Float32, if the image itself is not FLoat64 or Complex.
"""
function remove_background(img, sigma=5.0; dtype=Float32)
bg = minimum(gaussf(img,sigma; dtype=dtype))
return img .- bg, bg
end