1
|
|
|
# Copyright 2014 Diamond Light Source Ltd. |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
|
15
|
|
|
""" |
16
|
|
|
.. module:: phase_unwrapping |
17
|
|
|
:platform: Unix |
18
|
|
|
:synopsis: A plugin for unwrapping phase-retrieved images. |
19
|
|
|
|
20
|
|
|
.. moduleauthor:: Nghia Vo <[email protected]> |
21
|
|
|
""" |
22
|
|
|
from savu.plugins.utils import register_plugin |
23
|
|
|
from savu.plugins.driver.cpu_plugin import CpuPlugin |
24
|
|
|
from savu.plugins.plugin import Plugin |
25
|
|
|
|
26
|
|
|
import numpy as np |
27
|
|
|
import pyfftw.interfaces.scipy_fftpack as fft |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
@register_plugin |
31
|
|
|
class PhaseUnwrapping(Plugin, CpuPlugin): |
32
|
|
|
|
33
|
|
|
def __init__(self): |
34
|
|
|
super(PhaseUnwrapping, self).__init__("PhaseUnwrapping") |
35
|
|
|
|
36
|
|
|
def nInput_datasets(self): |
37
|
|
|
return 1 |
38
|
|
|
|
39
|
|
|
def nOutput_datasets(self): |
40
|
|
|
return 1 |
41
|
|
|
|
42
|
|
View Code Duplication |
def setup(self): |
|
|
|
|
43
|
|
|
in_dataset, out_dataset = self.get_datasets() |
44
|
|
|
out_dataset[0].create_dataset(in_dataset[0]) |
45
|
|
|
in_pData, out_pData = self.get_plugin_datasets() |
46
|
|
|
self.pattern = self.parameters['pattern'] |
47
|
|
|
if self.pattern == "PROJECTION": |
48
|
|
|
in_pData[0].plugin_data_setup(self.pattern, 'single') |
49
|
|
|
out_pData[0].plugin_data_setup(self.pattern, 'single') |
50
|
|
|
else: |
51
|
|
|
in_pData[0].plugin_data_setup('SINOGRAM', 'single') |
52
|
|
|
out_pData[0].plugin_data_setup('SINOGRAM', 'single') |
53
|
|
|
|
54
|
|
|
def wrap_to_pi(self, mat): |
55
|
|
|
return (mat + np.pi) % (2 * np.pi) - np.pi |
56
|
|
|
|
57
|
|
|
def make_window(self, height, width): |
58
|
|
|
wid_cen = width // 2 |
59
|
|
|
hei_cen = height // 2 |
60
|
|
|
ulist = (1.0 * np.arange(0, width) - wid_cen) / wid_cen |
61
|
|
|
vlist = (1.0 * np.arange(0, height) - hei_cen) / hei_cen |
62
|
|
|
u, v = np.meshgrid(ulist, vlist) |
63
|
|
|
window = u ** 2 + v ** 2 |
64
|
|
|
window1 = np.copy(window) |
65
|
|
|
window1[hei_cen, wid_cen] = 1.0 |
66
|
|
|
return window, window1 |
67
|
|
|
|
68
|
|
|
def forward_operator(self, mat, window): |
69
|
|
|
mat_res = fft.ifft2(fft.ifftshift(fft.fftshift( |
70
|
|
|
fft.fft2(mat)) * window)) |
71
|
|
|
return mat_res |
72
|
|
|
|
73
|
|
|
def backward_operator(self, mat, window1): |
74
|
|
|
mat_res = fft.ifft2(fft.ifftshift(fft.fftshift( |
75
|
|
|
fft.fft2(mat)) / window1)) |
76
|
|
|
return mat_res |
77
|
|
|
|
78
|
|
|
def double_image(self, mat): |
79
|
|
|
mat1 = np.hstack((mat, np.fliplr(mat))) |
80
|
|
|
mat2 = np.vstack((np.flipud(mat1), mat1)) |
81
|
|
|
return mat2 |
82
|
|
|
|
83
|
|
|
def phase_unwrap_based_fft(self, mat, window, window1): |
84
|
|
|
height, width = mat.shape |
85
|
|
|
mat2 = self.double_image(mat) |
86
|
|
|
mat_unwrap = np.real( |
87
|
|
|
self.backward_operator(np.imag(self.forward_operator( |
88
|
|
|
np.exp(mat2 * 1j), window) * np.exp(-1j * mat2)), window1)) |
89
|
|
|
mat_unwrap = mat_unwrap[height:, 0:width] |
90
|
|
|
return mat_unwrap |
91
|
|
|
|
92
|
|
|
def phase_unwrap_iterative(self, mat_wrap, window, window1, n_iter): |
93
|
|
|
mat_unwrap = self.phase_unwrap_based_fft(mat_wrap, window, window1) |
94
|
|
|
for i in range(n_iter): |
95
|
|
|
mat_wrap1 = self.wrap_to_pi(mat_unwrap) |
96
|
|
|
mat_diff = mat_wrap - mat_wrap1 |
97
|
|
|
nmean = np.mean(mat_diff) |
98
|
|
|
mat_diff = self.wrap_to_pi(mat_diff - nmean) |
99
|
|
|
phase_diff = self.phase_unwrap_based_fft(mat_diff, window, |
100
|
|
|
window1) |
101
|
|
|
mat_unwrap = mat_unwrap + phase_diff |
102
|
|
|
return mat_unwrap |
103
|
|
|
|
104
|
|
|
def pre_process(self): |
105
|
|
|
inData = self.get_in_datasets()[0] |
106
|
|
|
self.data_size = inData.get_shape() |
107
|
|
|
(self.depth, self.height, self.width) = self.data_size[:3] |
108
|
|
|
if self.pattern == "PROJECTION": |
109
|
|
|
self.window, self.window1 = self.make_window(2 * self.height, |
110
|
|
|
2 * self.width) |
111
|
|
|
else: |
112
|
|
|
self.window, self.window1 = self.make_window(2 * self.depth, |
113
|
|
|
2 * self.width) |
114
|
|
|
|
115
|
|
|
self.n_iter = self.parameters['n_iterations'] |
116
|
|
|
|
117
|
|
|
def process_frames(self, data): |
118
|
|
|
mat_unwrap = self.phase_unwrap_iterative(data[0], self.window, |
119
|
|
|
self.window1, self.n_iter) |
120
|
|
|
return mat_unwrap |
121
|
|
|
|