Skip to content

Commit

Permalink
- dydx iterator: fixed edge artefact at edm smooth, draw center and l…
Browse files Browse the repository at this point in the history
…abelwise derivatives

- derivatives: use edge padding
  • Loading branch information
jeanollion committed May 27, 2024
1 parent eb8f6c6 commit 602ccc3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
7 changes: 4 additions & 3 deletions distnet_2d/data/dydx_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def _draw_centers(centerIm, labels_map_centers, labelIm, object_slices, geometri
if sl is not None:
center = labels_map_centers.get(i+1)
if not (isnan(center[0]) or isnan(center[1])):
sl = tuple( [slice(max(0, s.start - 2), min(s.stop + 2, ax - 1), s.step) for s, ax in zip(sl, shape)])
sl = tuple( [slice(max(0, s.start - 2), min(s.stop + 2, ax), s.step) for s, ax in zip(sl, shape)])
mask = labelIm_dil[sl] == i + 1
m = np.ones_like(mask)
#print(f"label: {i+1} slice: {sl}, center: {center}, sub_m {sub_m.shape}, coord: {(int(round(center[0]))-sl[0].start, int(round(center[1]))-sl[1].start)}", flush=True)
Expand All @@ -590,7 +590,7 @@ def edt_smooth(labelIm, object_slices):
w=np.ones(shape=(3, 3), dtype=np.int8)
for (i, sl) in enumerate(object_slices):
if sl is not None:
sl = tuple([slice(max(s.start*2 - 1, 0), min(s.stop*2 + 1, ax*2 - 1), s.step) for s, ax in zip(sl, shape)])
sl = tuple([slice(max(s.start*2 - 1, 0), min(s.stop*2 + 1, ax*2), s.step) for s, ax in zip(sl, shape)])
sub_labelIm = upsampled[sl]
mask = sub_labelIm == i + 1
new_mask = convolve(mask.astype(np.int8), weights=w, mode="nearest") > 4 # smooth borders
Expand All @@ -602,11 +602,12 @@ def edt_smooth(labelIm, object_slices):
edm[edm <= 0.5] = 0
return edm


def derivatives_labelwise(image, bck_value, der_y, der_x, labelIm, object_slices):
shape = labelIm.shape
for (i, sl) in enumerate(object_slices):
if sl is not None:
sl = tuple([slice(max(s.start - 1, 0), min(s.stop + 1, ax - 1), s.step) for s, ax in zip(sl, shape)])
sl = tuple([slice(max(s.start - 1, 0), min(s.stop + 1, ax), s.step) for s, ax in zip(sl, shape)])
mask = labelIm[sl] == i + 1
sub_im = np.copy(image[sl])
sub_im[np.logical_not(mask)] = bck_value # erase neighboring cells
Expand Down
13 changes: 4 additions & 9 deletions distnet_2d/utils/image_derivatives_np.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@ def der_2d(image, *axis:int):
axis = axis[0]
assert image.ndim == 2, f'image_gradients expects a 2D tensor [Y, X], not {image.shape}'
assert axis in [0, 1], "axis must be in [0, 1]"
Y, X = image.shape
if axis == 0:
dy = np.divide(image[2:] - image[:-2], 2)
zeros = np.zeros(np.stack([1, X]), image.dtype)
dy = np.concatenate([zeros, dy, zeros], axis)
return np.reshape(dy, image.shape)
image = np.pad(image, ((1, 1), (0, 0)), mode="edge")
return np.divide(image[2:] - image[:-2], 2)
else:
dx = np.divide(image[:, 2:] - image[:, :-2], 2)
zeros = np.zeros(np.stack([Y, 1]), image.dtype)
dx = np.concatenate([zeros, dx, zeros], axis)
return np.reshape(dx, image.shape)
image = np.pad(image, ((0, 0), (1, 1)), mode="edge")
return np.divide(image[:, 2:] - image[:, :-2], 2)


def gradient_magnitude_2d(image=None, dy=None, dx=None, sqrt:bool=True):
Expand Down
14 changes: 4 additions & 10 deletions distnet_2d/utils/image_derivatives_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ def der_2d(image, axis:int):
else:
tf.assert_equal(tf.rank(image), 4, message=f'image_gradients expects a 4D tensor [B, Y, X, C], not {tf.shape(image)}.')
assert axis in [1, 2], "axis must be in [1, 2]"
image_shape = tf.shape(image)
B, Y, X, C = tf.unstack(image_shape)
if axis == 1:
dy = tf.math.divide(image[:, 2:, :, :] - image[:, :-2, :, :], tf.cast(2, image.dtype))
zeros = tf.zeros(tf.stack([B, 1, X, C]), image.dtype)
dy = tf.concat([zeros, dy, zeros], 1)
return tf.reshape(dy, image_shape)
image = tf.pad(image, tf.constant([[0, 0], [1, 1,], [0, 0], [0, 0]]), mode="SYMMETRIC")
return tf.math.divide(image[:, 2:, :, :] - image[:, :-2, :, :], tf.cast(2, image.dtype))
else:
dx = tf.math.divide(image[:, :, 2:, :] - image[:, :, :-2, :], tf.cast(2, image.dtype))
zeros = tf.zeros(tf.stack([B, Y, 1, C]), image.dtype)
dx = tf.concat([zeros, dx, zeros], 2)
return tf.reshape(dx, image_shape)
image = tf.pad(image, tf.constant([[0, 0], [0, 0,], [1, 1], [0, 0]]), mode="SYMMETRIC")
return tf.math.divide(image[:, :, 2:, :] - image[:, :, :-2, :], tf.cast(2, image.dtype))


def gradient_magnitude_2d(image=None, dy=None, dx=None, sqrt:bool=True):
Expand Down

0 comments on commit 602ccc3

Please sign in to comment.