Created
August 25, 2023 12:32
-
-
Save addam/94f849cf2a3648c0c5d09291e6525d20 to your computer and use it in GitHub Desktop.
Dilate a stack of images with feather kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def feather_dilate(stack: np.ndarray, radius=20): | |
"""Feather dilate a stack of images | |
stack has dimensions: [image_id, x, y] | |
The result is similar to dilation but with feather edges""" | |
ms = max_steepness | |
msq = ms * 2**0.5 | |
for x in range(1, stack.shape[2]): | |
stack[:,:, x] = (np.maximum(stack[:,:, x], stack[:,:, x-1] - ms)) | |
for x in range(stack.shape[2]-2, -1, -1): | |
stack[:,:, x] = np.maximum(stack[:,:, x], stack[:,:, x+1] - ms) | |
for y in range(1, stack.shape[1]): | |
stack[:,y, 1:-1] = np.max([stack[:,y, 1:-1], stack[:,y-1, 1:-1] - ms, stack[:,y-1, :-2] - msq, stack[:,y-1, 2:] - msq], axis=0) | |
for y in range(stack.shape[1]-2, -1, -1): | |
stack[:,y, 1:-1] = np.max([stack[:,y, 1:-1], stack[:,y+1, 1:-1] - ms, stack[:,y+1, :-2] - msq, stack[:,y+1, 2:] - msq], axis=0) | |
# This function is helpful for training neural networks on sparse masks | |
# for that purpose, you can gradually decrease the radius during training | |
# note that this function can be found in Blender but almost nowhere else |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment