src/PET/ImGenerate.jl

Sun, 21 Apr 2024 19:05:44 +0300

author
Neil Dizon <neil.dizon@helsinki.fi>
date
Sun, 21 Apr 2024 19:05:44 +0300
changeset 21
97737e4e7197
parent 19
7c4dfeb48dd9
child 22
a30a8745a83b
permissions
-rw-r--r--

included zero dual in main module

8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
1 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
2 # Image generation
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
3 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
4
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
5 module ImGenerate
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
6
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
7 using ColorTypes: Gray
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
8 import TestImages
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
9 # We don't really *directly* depend on QuartzImageIO. The import here is
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
10 # merely a workaround to suppress warnings when loading TestImages.
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
11 # Something is broken in those packages.
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
12 import QuartzImageIO
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
13
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
14 using AlgTools.Util
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
15 using AlgTools.Comms
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
16 using ImageTools.Translate
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
17
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
18 using ..OpticalFlow: Image, DisplacementConstant, DisplacementFull
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
19 using ..Radon
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
20
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
21 # Added for reproducibility
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
22 import StableRNGs: StableRNG, Random
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
23 const rng = StableRNG(9182737465)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
24
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
25 # Added for PET
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
26 import PoissonRandom: pois_rand
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
27 import Random: shuffle
18
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
28 import Images: center, warp, imresize
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
29 import CoordinateTransformations: recenter
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
30 import Rotations: RotMatrix
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
31 import Interpolations: Flat
18
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
32 import MAT: matread
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
33
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
34
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
35 ##############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
36 # Our exports
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
37 ##############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
38
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
39 export ImGen,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
40 OnlineData,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
41 imgen_square,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
42 imgen_shake,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
43 PetOnlineData,
18
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
44 imgen_shepplogan_radon,
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
45 imgen_brainphantom_radon
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
46
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
47 ##################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
48 # Data structures
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
49 ##################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
50
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
51 struct ImGen
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
52 f :: Function
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
53 dim :: Tuple{Int64,Int64}
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
54 Λ :: Float64
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
55 dynrange :: Float64
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
56 name :: String
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
57 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
58
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
59 struct OnlineData{DisplacementT}
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
60 b_true :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
61 b_noisy :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
62 v :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
63 v_true :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
64 v_cumul_true :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
65 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
66
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
67 struct PetOnlineData{DisplacementT}
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
68 b_true :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
69 sinogram_true :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
70 sinogram_noisy :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
71 v :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
72 v_true :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
73 v_cumul_true :: DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
74 theta :: DisplacementT # theta = thetaknown, theta_cumul
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
75 S :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
76 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
77
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
78 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
79 # Shake generation
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
80 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
81
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
82 function make_const_v(displ, sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
83 v = zeros(2, sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
84 v[1, :, :] .= displ[1]
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
85 v[2, :, :] .= displ[2]
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
86 return v
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
87 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
88
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
89 function shake(params)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
90 if !haskey(params, :shaketype) || params.shaketype == :gaussian
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
91 return () -> params.shake.*randn(rng,2)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
92 elseif params.shaketype == :disk
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
93 return () -> begin
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
94 θ = 2π*rand(rng,Float64)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
95 r = params.shake*√(rand(rng,Float64))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
96 return [r*cos(θ), r*sin(θ)]
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
97 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
98 elseif params.shaketype == :circle
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
99 return () -> begin
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
100 θ = 2π*rand(rng,Float64)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
101 r = params.shake
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
102 return [r*cos(θ), r*sin(θ)]
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
103 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
104 else
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
105 error("Unknown shaketype $(params.shaketype)")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
106 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
107 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
108
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
109 pixelwise = (shakefn, sz) -> () -> make_const_u(shakefn(), sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
110
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
111
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
112 function rotatebytheta(params)
14
c286925c0f35 Change rand to randn in PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 8
diff changeset
113 r = params.rotation_factor*randn(rng)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
114 return r
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
115 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
116
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
117 function generate_radonmask(params)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
118 imdim = params.radondims
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
119 sino_sparse = params.sino_sparsity
19
7c4dfeb48dd9 clarified sino_sparsity
Neil Dizon <neil.dizon@helsinki.fi>
parents: 18
diff changeset
120 numzero = Int64(round(sino_sparse*imdim[1]*imdim[2]))
7c4dfeb48dd9 clarified sino_sparsity
Neil Dizon <neil.dizon@helsinki.fi>
parents: 18
diff changeset
121 numone = imdim[1]*imdim[2]-numzero
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
122 A = shuffle(rng,reshape([ones(numone); zeros(numzero)],(imdim[1],imdim[2])))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
123 return A
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
124 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
125
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
126 ################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
127 # Moving square
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
128 ################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
129
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
130 function generate_square(sz,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
131 :: Type{DisplacementT},
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
132 datachannel :: Channel{OnlineData{DisplacementT}},
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
133 params) where DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
134
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
135 if false
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
136 v₀ = make_const_v(0.1.*(-1, 1), sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
137 nextv = () -> v₀
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
138 elseif DisplacementT == DisplacementFull
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
139 nextv = pixelwise(shake(params), sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
140 elseif DisplacementT == DisplacementConstant
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
141 nextv = shake(params)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
142 else
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
143 @error "Invalid DisplacementT"
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
144 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
145
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
146 # Constant linear displacement everywhere has Jacobian determinant one
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
147 # (modulo the boundaries which we ignore here)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
148 m = round(Int, sz[1]/5)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
149 b_orig = zeros(sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
150 b_orig[sz[1].-(2*m:3*m), 2*m:3*m] .= 1
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
151
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
152 v_true = nextv()
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
153 v_cumul = copy(v_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
154
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
155 while true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
156 # Flow original data and add noise
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
157 b_true = zeros(sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
158 translate_image!(b_true, b_orig, v_cumul; threads=true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
159 b = b_true .+ params.noise_level.*randn(rng,sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
160 v = v_true.*(1.0 .+ params.shake_noise_level.*randn(rng,size(v_true)...))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
161 # Pass true data to iteration routine
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
162 data = OnlineData{DisplacementT}(b_true, b, v, v_true, v_cumul)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
163 if !put_unless_closed!(datachannel, data)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
164 return
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
165 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
166 # Next step shake
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
167 v_true = nextv()
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
168 v_cumul .+= v_true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
169 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
170 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
171
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
172 function imgen_square(sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
173 return ImGen(curry(generate_square, sz), sz, 1, 1, "square$(sz[1])x$(sz[2])")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
174 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
175
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
176 ################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
177 # Shake a photo
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
178 ################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
179
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
180 function generate_shake_image(im, sz,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
181 :: Type{DisplacementConstant},
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
182 datachannel :: Channel{OnlineData{DisplacementConstant}},
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
183 params :: NamedTuple)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
184
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
185 # Restart the seed to enable comparison across predictors
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
186 Random.seed!(rng,314159)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
187
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
188 nextv = shake(params)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
189 v_true = nextv()
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
190 v_cumul = copy(v_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
191
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
192 while true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
193 # Extract subwindow of original image and add noise
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
194 b_true = zeros(sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
195 extract_subimage!(b_true, im, v_cumul; threads=true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
196 b = b_true .+ params.noise_level.*randn(rng,sz...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
197 v = v_true.*(1.0 .+ params.shake_noise_level.*randn(rng,size(v_true)...))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
198 # Pass data to iteration routine
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
199 data = OnlineData{DisplacementConstant}(b_true, b, v, v_true, v_cumul)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
200 if !put_unless_closed!(datachannel, data)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
201 return
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
202 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
203 # Next step shake
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
204 v_true = nextv()
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
205 v_cumul .+= v_true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
206 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
207 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
208
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
209 function imgen_shake(imname, sz)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
210 im = Float64.(Gray.(TestImages.testimage(imname)))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
211 dynrange = maximum(im)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
212 return ImGen(curry(generate_shake_image, im, sz), sz, 1, dynrange,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
213 "$(imname)$(sz[1])x$(sz[2])")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
214 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
215
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
216
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
217
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
218 ########################################################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
219 # PETscan
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
220 ########################################################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
221 function generate_radon(im, sz,
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
222 :: Type{DisplacementConstant},
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
223 datachannel :: Channel{PetOnlineData{DisplacementConstant}},
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
224 params :: NamedTuple)
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
225
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
226 # Set up counter and zero factor for stabilisation
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
227 @assert(params.maxiter ≥ maximum(params.stable_interval))
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
228 indx = 1
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
229 zero_factor = indx in params.stable_interval ? 0.0 : 1.0
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
230
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
231 # Restart the seed to enable comparison across predictors
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
232 Random.seed!(rng,9182737465)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
233
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
234 nextv = shake(params)
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
235 v_true = zero_factor.*nextv()
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
236 v_cumul = copy(v_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
237
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
238 S_true = generate_radonmask(params)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
239
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
240 theta_true = zero_factor*rotatebytheta(params)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
241 theta_cumul = copy(theta_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
242
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
243 while true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
244 # Define the transformation matrix
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
245 center_point = (sz[1]/2 + v_true[1], sz[2]/2 + v_true[2])
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
246 tform = recenter(RotMatrix(theta_cumul), center_point)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
247
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
248 # Apply the transformation to the image using warp
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
249 b_true = copy(warp(im, tform, axes(im), fillvalue=Flat()))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
250
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
251 v = v_true.*(1.0 .+ params.shake_noise_level.*randn(rng,size(v_true)...))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
252 theta = theta_true*(1.0 + params.rotation_noise_level.*randn(rng))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
253
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
254 # Generate the true and noisy sinograms
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
255 sinogram_true = zeros(params.radondims...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
256 sinogram_true .*= params.scale
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
257 radon!(sinogram_true, b_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
258 sinogram_noisy = copy(sinogram_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
259
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
260 for i=1:params.radondims[1], j=1:params.radondims[2]
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
261 sinogram_noisy[i, j] += pois_rand(rng,params.noise_level)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
262 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
263
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
264 # Pass data to iteration routine
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
265 data = PetOnlineData{DisplacementConstant}(b_true, sinogram_true, sinogram_noisy, v, v_true, v_cumul, [theta, theta_cumul], S_true)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
266 if !put_unless_closed!(datachannel, data)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
267 return
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
268 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
269
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
270 # Next step shake
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
271 v_true = zero_factor.*nextv()
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
272 v_cumul .+= v_true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
273 # Next theta
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
274 theta_true = zero_factor*rotatebytheta(params)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
275 theta_cumul += theta_true
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
276 # Next sinogram mask
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
277 S_true = generate_radonmask(params)
15
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
278 # Update indx and zero factor
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
279 indx += 1
befb8d5125cd added stable interval for PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 14
diff changeset
280 zero_factor = indx in params.stable_interval ? 0.0 : 1.0
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
281 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
282 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
283
14
c286925c0f35 Change rand to randn in PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 8
diff changeset
284 function imgen_shepplogan_radon(sz)
c286925c0f35 Change rand to randn in PET
Neil Dizon <neil.dizon@helsinki.fi>
parents: 8
diff changeset
285 im = convert(Array{Float64},TestImages.shepp_logan(sz[1], highContrast=true))
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
286 dynrange = maximum(im)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
287 return ImGen(curry(generate_radon, im, sz), sz, 1, dynrange, "shepplogan$(sz[1])x$(sz[2])")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
288 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
289
18
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
290 function imgen_brainphantom_radon(sz)
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
291 data = matread("src/PET/phantom_slice.mat")
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
292 im = normalise(imresize(convert(Array{Float64},data["square_data"]),sz))
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
293 dynrange = maximum(im)
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
294 return ImGen(curry(generate_radon, im, sz), sz, 1, dynrange, "brainphantom$(sz[1])x$(sz[2])")
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
295 end
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
296
18
e6a2ebaf467d added data generation for new phantom
Neil Dizon <neil.dizon@helsinki.fi>
parents: 15
diff changeset
297 normalise = (data) -> data./maximum(data)
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
298 end # Module

mercurial