-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMorphology.py
62 lines (53 loc) · 1.76 KB
/
Morphology.py
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
##############################################################
#
# Morphology:
# Contains morphological functions, typically
# to manipulate object support during phase
# retrieval.
#
# Siddharth Maddali
# Argonne National Laboratory
# Nov 2020
# 6xlq96aeq@relay.firefox.com
#
##############################################################
import numpy as np
import tensorflow as tf
from scipy.ndimage.morphology import binary_erosion
try:
from pyfftw.interfaces.numpy_fft import fftshift
except:
from numpy.fft import fftshift
class Mixin:
# CPU method for binary erosion
def __CPUErosion__( self, num_erosions=1 ):
temp = np.absolute( fftshift( self._support ) ).astype( bool )
eroded = binary_erosion( temp, structure=self.BEStruct, iterations=num_erosions )
self._support = fftshift( eroded.astype( complex ) )
return
# GPU method for binary erosion, wraps CPU method in Tensorflow
def __GPUErosion__( self, num_erosions=1, kernel_size=[ 1, 3, 3, 2, 1 ] ):
# self._support = tf.py_function(
# func=self.BinaryErosionCPU,
# inp=[ num_erosions ],
# Tout=tf.complex64
# )
sup_rankraised = tf.cast(
tf.expand_dims(
tf.expand_dims( self._support, axis=-1 ),
axis=0
),
dtype=tf.float32
)
for n in range( num_erosions ):
sup_rankraised = -tf.nn.max_pool3d(
-sup_rankraised,
ksize=kernel_size,
strides=1,
padding='SAME'
)
self._support = tf.cast(
tf.squeeze( sup_rankraised ),
dtype=tf.complex64
)
return