| 1 |
--- |
| 2 |
kernelspec: |
| 3 |
display_name: IDL [conda env:gdl] * |
| 4 |
language: IDL |
| 5 |
name: conda-env-gdl-idl |
| 6 |
--- |
| 7 |
|
| 8 |
## GDL demo notebook |
| 9 |
|
| 10 |
+++ |
| 11 |
|
| 12 |
Demonstration of GDL [(gnudatalanguage)](https://github.com/gnudatalanguage/gdl) |
| 13 |
|
| 14 |
This notebook creates a Shepp-Logan phantom, projects it and then performs an FBP reconstruction. |
| 15 |
|
| 16 |
```{code-cell} |
| 17 |
;; L.A. Shepp and B.F. Logan, “The Fourier reconstruction of a head section,” |
| 18 |
;; IEEE Trans. Nucl. Sci. 21(3), 21–43 (1974). |
| 19 |
function shepplogan, size = size |
| 20 |
|
| 21 |
if NOT keyword_set(size) then size = 256 |
| 22 |
|
| 23 |
phantom = fltarr(size,size) |
| 24 |
|
| 25 |
tmp = (findgen(size)-((size-1)/2.0)) / (size/2.0) |
| 26 |
xcor = rebin(tmp,size,size) |
| 27 |
ycor = rebin(transpose(tmp),size,size) |
| 28 |
tmp = fltarr(size,size) |
| 29 |
|
| 30 |
aa={cx: 0.0, cy: 0.0, maj:0.69, min:0.92, theta: 0.0, val: 2.0 } |
| 31 |
bb={cx: 0.0, cy:-0.0184, maj:0.6624, min:0.874, theta: 0.0, val:-0.98} |
| 32 |
cc={cx: 0.22, cy: 0.0, maj:0.11, min:0.31, theta:-18.0, val:-0.02} |
| 33 |
dd={cx:-0.22, cy: 0.0, maj:0.16, min:0.41, theta: 18.0, val:-0.02} |
| 34 |
ee={cx: 0.0, cy: 0.35, maj:0.21, min:0.25, theta: 0.0, val:-0.01} |
| 35 |
ff={cx: 0.0, cy: 0.1, maj:0.046, min:0.046, theta: 0.0, val:-0.01} |
| 36 |
gg={cx: 0.0, cy:-0.1, maj:0.046, min:0.046, theta: 0.0, val:-0.01} |
| 37 |
hh={cx:-0.08, cy:-0.605, maj:0.046, min:0.023, theta: 0.0, val:-0.01} |
| 38 |
ii={cx: 0.0, cy:-0.605, maj:0.023, min:0.023, theta: 0.0, val:-0.01} |
| 39 |
jj={cx: 0.06, cy:-0.605, maj:0.023, min:0.046, theta: 0.0, val:-0.01} |
| 40 |
|
| 41 |
list = [aa,bb,cc,dd,ee,ff,gg,hh,ii,jj] |
| 42 |
for n = 0, n_elements(list)-1 do begin |
| 43 |
|
| 44 |
tmp = ((xcor-list[n].cx) / list[n].maj)^2 $ |
| 45 |
+ ((ycor-list[n].cy) / list[n].min)^2 |
| 46 |
|
| 47 |
if list[n].theta NE 0 then begin |
| 48 |
nx = (size-1) * (list[n].cx + 1) / 2 |
| 49 |
ny = (size-1) * (list[n].cy + 1) / 2 |
| 50 |
tmp = rot(tmp, -list[n].theta, 1, nx, ny, /interp, /pivot) |
| 51 |
endif |
| 52 |
|
| 53 |
phantom[where(tmp LE 1.0)] += list[n].val |
| 54 |
|
| 55 |
endfor |
| 56 |
|
| 57 |
return, phantom < 1.1 |
| 58 |
|
| 59 |
end |
| 60 |
``` |
| 61 |
|
| 62 |
```{code-cell} |
| 63 |
;; Enable inline plotting |
| 64 |
!inline=1 |
| 65 |
``` |
| 66 |
|
| 67 |
```{code-cell} |
| 68 |
phantom = shepplogan() |
| 69 |
window, 0, xsize=256, ysize=256 |
| 70 |
tvscl, phantom > 0.95 |
| 71 |
``` |
| 72 |
|
| 73 |
Now we define simple forward and backprojection functions: |
| 74 |
|
| 75 |
```{code-cell} |
| 76 |
function proj, image, angle |
| 77 |
|
| 78 |
sino_size_x = max(size(image,/dim)) |
| 79 |
sino_size_y = n_elements(angle) |
| 80 |
|
| 81 |
sino = fltarr(sino_size_x, sino_size_y) |
| 82 |
|
| 83 |
for aa=0, n_elements(angle)-1 do begin |
| 84 |
|
| 85 |
sino[*,aa] = total(rot(image, angle[aa], /interp), 2) |
| 86 |
|
| 87 |
endfor |
| 88 |
|
| 89 |
return, sino |
| 90 |
|
| 91 |
end |
| 92 |
``` |
| 93 |
|
| 94 |
```{code-cell} |
| 95 |
function back, sino, angle |
| 96 |
|
| 97 |
image_size = n_elements(sino[*,0]) |
| 98 |
image = fltarr(image_size,image_size) |
| 99 |
|
| 100 |
for aa=0, n_elements(angle)-1 do begin |
| 101 |
|
| 102 |
image += rot(rebin(sino[*,aa],[image_size,image_size]), -angle[aa], /interp) |
| 103 |
|
| 104 |
endfor |
| 105 |
|
| 106 |
return, image / n_elements(angle) |
| 107 |
|
| 108 |
end |
| 109 |
``` |
| 110 |
|
| 111 |
This allows us to create a sinogram: |
| 112 |
|
| 113 |
```{code-cell} |
| 114 |
angles = findgen(360) |
| 115 |
sino = proj(phantom, angles) |
| 116 |
window, 0, xsize=256, ysize=360 |
| 117 |
tvscl, sino |
| 118 |
``` |
| 119 |
|
| 120 |
On this we can apply a simple FBP reconstruction: |
| 121 |
|
| 122 |
```{code-cell} |
| 123 |
;; G.L. Zeng, “Revisit of the Ramp Filter,” |
| 124 |
;; IEEE Trans. Nucl. Sci. 62(1), 131–136 (2015). |
| 125 |
function fbp, sino, angles |
| 126 |
|
| 127 |
ntot = n_elements(sino[*,0]) |
| 128 |
nang = n_elements(sino[0,*]) |
| 129 |
npos = ntot / 2 + 1 ; integer division needed ! |
| 130 |
nneg = ntot - npos |
| 131 |
|
| 132 |
freq = findgen(ntot) |
| 133 |
freq[npos:ntot-1] = REVERSE(freq[1:nneg]) |
| 134 |
freq[0] = 1 |
| 135 |
|
| 136 |
filter = -1 / (!pi * freq)^2 |
| 137 |
filter[where(freq mod 2 EQ 0)] *= -0.0 |
| 138 |
filter[0] = 0.25 |
| 139 |
|
| 140 |
filter = abs(fft(filter)) * ntot |
| 141 |
filter[ntot/4:ntot/4+ntot/2-1] *= 0.0 |
| 142 |
filter = rebin(filter,ntot,nang) |
| 143 |
|
| 144 |
;; apply filter to the sinogram |
| 145 |
fsino = fft(sino, dim=1) |
| 146 |
fsino *= filter |
| 147 |
fsino = fft(fsino, dim=1, /overwrite, /inverse) |
| 148 |
fsino = !pi * real_part(fsino) |
| 149 |
|
| 150 |
;; backproject the filtered sinogram |
| 151 |
return, back(fsino, angles) |
| 152 |
|
| 153 |
end |
| 154 |
``` |
| 155 |
|
| 156 |
```{code-cell} |
| 157 |
reconstruction = fbp(sino, angles) |
| 158 |
window, 0, xsize=512, ysize=256 |
| 159 |
|
| 160 |
tvscl, [phantom, reconstruction] > 0.8 |
| 161 |
``` |
| 162 |
|
| 163 |
### The end |