Skip to content

Commit 6fbd545

Browse files
author
donglaiw
committed
add pni version of affinity
1 parent f9ba705 commit 6fbd545

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

connectomics/data/utils/data_affinity.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"seg2aff_v0",
55
"seg2aff_v1",
66
"seg2aff_v2",
7+
"seg2aff_pni",
78
]
89

910

@@ -67,6 +68,46 @@ def mknhood3d_aniso(radiusxy: int=1, radiusxy_zminus1: float=1.8):
6768

6869
return np.ascontiguousarray(nhood)
6970

71+
def seg2aff_pni(img, dz=1, dy=1, dx=1, dtype='float32'):
72+
# https://github.com/torms3/DataProvider/blob/master/python/transform.py
73+
"""
74+
Transform segmentation to 3D affinity graph.
75+
76+
Args:
77+
img: 3D indexed image, with each index corresponding to each segment.
78+
79+
Returns:
80+
ret: 3D affinity graph (4D tensor), 3 channels for z, y, x direction.
81+
"""
82+
img = check_volume(img)
83+
ret = np.zeros((3,) + img.shape, dtype=dtype)
84+
85+
86+
# z-affinity.
87+
assert dz and abs(dz) < img.shape[-3]
88+
if dz > 0:
89+
ret[2,dz:,:,:] = (img[dz:,:,:]==img[:-dz,:,:]) & (img[dz:,:,:]>0)
90+
else:
91+
dz = abs(dz)
92+
ret[2,:-dz,:,:] = (img[dz:,:,:]==img[:-dz,:,:]) & (img[dz:,:,:]>0)
93+
94+
# y-affinity.
95+
assert dy and abs(dy) < img.shape[-2]
96+
if dy > 0:
97+
ret[1,:,dy:,:] = (img[:,dy:,:]==img[:,:-dy,:]) & (img[:,dy:,:]>0)
98+
else:
99+
dy = abs(dy)
100+
ret[1,:,:-dy,:] = (img[:,dy:,:]==img[:,:-dy,:]) & (img[:,dy:,:]>0)
101+
102+
# x-affinity.
103+
assert dx and abs(dx) < img.shape[-1]
104+
if dx > 0:
105+
ret[0,:,:,dx:] = (img[:,:,dx:]==img[:,:,:-dx]) & (img[:,:,dx:]>0)
106+
else:
107+
dx = abs(dx)
108+
ret[0,:,:,:-dx] = (img[:,:,dx:]==img[:,:,:-dx]) & (img[:,:,dx:]>0)
109+
110+
return ret
70111

71112
def seg_to_aff(seg, nhood=mknhood3d(1), pad='replicate'):
72113
# Constructs an affinity graph from a segmentation

connectomics/data/utils/data_segmentation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def seg2affinity(label, topt):
221221
aff_func_dict = {
222222
'v1': seg2aff_v1,
223223
'v2': seg2aff_v2,
224+
'pni': seg2aff_pni,
224225
}
225226

226227
# valid format: 2-z-y-x-version

0 commit comments

Comments
 (0)