diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33ed72e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/Recipes/__pycache__ \ No newline at end of file diff --git a/Recipes/AdjustGamma.py b/Recipes/AdjustGamma.py index 3edbf71..4e77448 100644 --- a/Recipes/AdjustGamma.py +++ b/Recipes/AdjustGamma.py @@ -7,6 +7,25 @@ See: https://scikit-image.org/docs/dev/api/skimage.exposure.html#skimage.exposure.adjust_gamma Adjusts gamma of the input channel pixelwise according to O = I**gamma. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Gamma : double + Value to use for the gamma transform. + +Returns +------- +Aivia channel + Result of the transform + """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] @@ -16,16 +35,14 @@ def run(params): image_location = params['inputImagePath'] result_location = params['resultPath'] gamma = float(params['gamma']) + if not os.path.exists(image_location): print(f'Error: {image_location} does not exist') return; image_data = imread(image_location) - output_data = empty_like(image_data) - output_data = adjust_gamma(image_data, gamma, 1) - imsave(result_location, output_data) @@ -37,8 +54,3 @@ def run(params): run(params) -# TODO: why doesn't gamma default to 0.75? truncates to 0 - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file diff --git a/Recipes/AdjustSigmoid.py b/Recipes/AdjustSigmoid.py index a6af93e..b210515 100644 --- a/Recipes/AdjustSigmoid.py +++ b/Recipes/AdjustSigmoid.py @@ -1,7 +1,8 @@ import os.path import numpy as np from skimage.io import imread, imsave -from skimage.exposure import adjust_sigmoid, rescale_intensity +from skimage.exposure import adjust_sigmoid +from skimage.util import img_as_uint, img_as_ubyte np.seterr(divide='ignore', invalid='ignore') @@ -13,6 +14,30 @@ "squished" to its bounds. The gain controls the amount of "squishing". In mathematical terms, O = 1 / (1 + exp*(gain*(cutoff - I))). + +Note that this transform is prone to returning a wildly different dynamic range than +the input image and should be parameterized carefully. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Cutoff : double + Percentile of the histogram from which to center the transform. + +Gain : double + Scaling factor that controls the strength of the transform. + +Returns +------- +Aivia channel + Result of the transform """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] @@ -24,18 +49,18 @@ def run(params): result_location = params['resultPath'] cutoff = float(params['cutoff']) gain = float(params['gain']) + if not os.path.exists(image_location): print(f'Error: {image_location} does not exist') return; image_data = imread(image_location) - output_data = np.empty_like(image_data) - sigmoid_image = adjust_sigmoid(image_data, cutoff=cutoff, gain=gain, inv=False) - - output_data = rescale_intensity(sigmoid_image, out_range='uint8').astype(image_data.dtype) - + if image_data.dtype == np.uint16: + output_data = img_as_uint(sigmoid_image) + else: + output_data = img_as_ubyte(sigmoid_image) imsave(result_location, output_data) @@ -48,6 +73,3 @@ def run(params): run(params) -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file diff --git a/Recipes/MaxMask.py b/Recipes/MaxMask.py new file mode 100644 index 0000000..c0c7cac --- /dev/null +++ b/Recipes/MaxMask.py @@ -0,0 +1,62 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave + +""" +Given an input image (I) and a mask image (M), returns (O) the input image only where +the mask image is BELOW a specified threshold (t). + +O(x,y) = I(X,Y) * [M(X,Y) 1): + print('Error: Maximum intensity projection cannot be applied to 2D images.') + return; + + if tCount == 1: + max_slice = int(dims[0]) + width_eff = min(int(width/2), max_slice) + for i in np.arange(0, max_slice): + output_data[i, :, :] = np.amax( + image_data[i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=0 + ) + else: + max_slice = int(dims[1]) + width_eff = min(int(width/2), max_slice) + for i in np.arange(0, max_slice): + output_data[:, i, :, :] = np.amax( + image_data[:, i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=1 + ) + + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['width'] = 2; + + run(params) diff --git a/Recipes/MeijeringNeuriteness.py b/Recipes/MeijeringNeuriteness.py new file mode 100644 index 0000000..f9413b9 --- /dev/null +++ b/Recipes/MeijeringNeuriteness.py @@ -0,0 +1,105 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave +from skimage.filters import meijering +from skimage.util import img_as_uint, img_as_ubyte + +np.seterr(divide='ignore', invalid='ignore') + +""" +See: https://scikit-image.org/docs/dev/api/skimage.filters.html#skimage.filters.meijering + +Finds and enhances bright ridges within the image that are within a reasonable +range of the size given by the user. Returns a max projection through scale space +for 5 evenly-spaced Gaussian sigmas. + +Because np.arange() is used to contruct the array of sigma values, the largest +sigma is excluded. +https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html + +For example, if the user specifies min and max sigmas of 0.1 and 0.6, respectively, +the transform is performed for Gaussian sigma values of 0.1, 0.2, 0.3, 0.4, 0.5. +The maximum values from all of the transforms is output at every voxel. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Min Sigma : double + Mininim kernel size for Gaussian smoothing. + +Max Sigma : double + Maximum kernel size for Gaussian smoothing. + +Returns +------- +Aivia channel + Result of the transform +""" + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:sigma_min Type:double DisplayName:'Min Sigma' Default:0.5 Min:0.0 Max:25.0] +# [INPUT Name:sigma_max Type:double DisplayName:'Max Sigma' Default:1.5 Min:0.0 Max:25.0] +# [OUTPUT Name:resultPath Type:string DisplayName:'Neuriteness'] +def run(params): + image_location = params['inputImagePath'] + result_location = params['resultPath'] + sigma_min = float(params['sigma_min']) + sigma_max = float(params['sigma_max']) + tCount = int(params['TCount']) + zCount = int(params['ZCount']) + + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + image_data = imread(image_location) + dims = image_data.shape + meijering_image = np.empty_like(image_data) + output_data = np.empty_like(image_data) + + sigmas = np.arange(sigma_min, sigma_max, round((sigma_max-sigma_min)/5, 1)) + + if tCount > 1: + print('Time series are currently not supported.') + return + + meijering_image = meijering(image_data, sigmas=sigmas, black_ridges=False) + + # Cropping is performed in 2D to get rid of bright pixels at edges of the image. + + if zCount > 1: + crop_size = max(int(max(list(dims[1:]))/100), 4) + meijering_image[:, 0:crop_size, 0:crop_size] = 0 + meijering_image[: ,0:crop_size, -crop_size:] = 0 + meijering_image[: ,-crop_size:, 0:crop_size] = 0 + meijering_image[: ,-crop_size:, -crop_size:] = 0 + else: + crop_size = max(int(max(list(dims))/100), 4) + meijering_image[0:crop_size, 0:crop_size] = 0 + meijering_image[0:crop_size, -crop_size:] = 0 + meijering_image[-crop_size:, 0:crop_size] = 0 + meijering_image[-crop_size:, -crop_size:] = 0 + + if image_data.dtype == np.uint16: + output_data = img_as_uint(meijering_image) + else: + output_data = img_as_ubyte(meijering_image) + + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['sigma_min'] = 0.5 + params['sigma_max'] = 1.5 + + run(params) diff --git a/Recipes/MinSlices.py b/Recipes/MinSlices.py new file mode 100644 index 0000000..3feedb7 --- /dev/null +++ b/Recipes/MinSlices.py @@ -0,0 +1,78 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave + +""" +Performs a slicewise minimum intensity projection through Z with a given +width about a slice. + +For example, using a width value of 2 means that for every slice, every +voxel in XY will be replaced with the minimum value found within the 2 +slices before and after that slice. + +Works only in 3D. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Width : int + Number of slices around the current slice to consider for the MIP. + +Returns +------- +Aivia channel + Result of the transform +""" + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:width Type:int DisplayName:'Width' Default:3 Min:2 Max:1000] +# [OUTPUT Name:resultPath Type:string DisplayName:'MinimumZ'] +def run(params): + image_location = params['inputImagePath'] + result_location = params['resultPath'] + width = int(params['width']) + tCount = int(params['TCount']) + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + image_data = imread(image_location) + dims = image_data.shape + output_data = np.empty_like(image_data) + + if len(dims) == 2 or (len(dims) == 3 and tCount > 1): + print('Error: Minimum intensity projection cannot be applied to 2D images.') + return; + + if tCount == 1: + max_slice = int(dims[0]) + width_eff = min(int(width/2), max_slice) + for i in np.arange(0, max_slice): + output_data[i, :, :] = np.amin( + image_data[i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=0 + ) + else: + max_slice = int(dims[1]) + width_eff = min(int(width/2), max_slice) + for i in np.arange(0, max_slice): + output_data[:, i, :, :] = np.amin( + image_data[:, i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=1 + ) + + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['width'] = 2; + + run(params) diff --git a/Recipes/MorphologicalTexture.py b/Recipes/MorphologicalTexture.py index c42970f..e77e191 100644 --- a/Recipes/MorphologicalTexture.py +++ b/Recipes/MorphologicalTexture.py @@ -3,9 +3,7 @@ from skimage.io import imread, imsave from skimage.morphology import closing, opening from skimage.morphology import disk, ball -from skimage.exposure import rescale_intensity - -np.seterr(divide='ignore', invalid='ignore') +from skimage.util import img_as_uint, img_as_ubyte """ See: https://scikit-image.org/docs/dev/api/skimage.filters.html#skimage.filters.meijering @@ -24,6 +22,24 @@ The opening result is then subtracted from the closing result to create the final output. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Size : double + Size of the disk or ball morphological kernel in pixels. + +Returns +------- +Aivia channel + Result of the transform """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] @@ -33,22 +49,40 @@ def run(params): image_location = params['inputImagePath'] result_location = params['resultPath'] size = int(params['size']) + tCount = int(params['TCount']) + zCount = int(params['ZCount']) if not os.path.exists(image_location): print(f'Error: {image_location} does not exist') return; image_data = imread(image_location) - + texture_image = np.empty_like(image_data) output_data = np.empty_like(image_data) - if len(image_data.shape) == 2: + print(f"z {zCount} t {tCount} shape {image_data.shape}") + + if zCount == 1: structure = disk(size) else: structure = ball(size) - output_data = closing(image_data, selem=structure) - opening(image_data, selem=structure) - - output_data = rescale_intensity(output_data, out_range='uint8').astype(image_data.dtype) + if tCount > 1 and zCount > 1: + for t in range(0, image_data.shape[0]): + texture_image[t,:,:,:] = closing( + image_data[t,:,:,:], selem=structure) - opening(image_data[t,:,:,:], selem=structure + ) + elif tCount > 1 and zCount == 1: + for t in range(0, image_data.shape[0]): + texture_image[t,:,:] = closing( + image_data[t,:,:], selem=structure) - opening(image_data[t,:,:], selem=structure + ) + else: + texture_image = closing(image_data, selem=structure) - opening(image_data, selem=structure) + + if image_data.dtype == np.uint16: + output_data = img_as_uint(texture_image) + else: + output_data = img_as_ubyte(texture_image) imsave(result_location, output_data) @@ -60,8 +94,3 @@ def run(params): params['size'] = 3 run(params) - - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file diff --git a/Recipes/RGBtoLuminance.py b/Recipes/RGBtoLuminance.py new file mode 100644 index 0000000..1d4c4e3 --- /dev/null +++ b/Recipes/RGBtoLuminance.py @@ -0,0 +1,80 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave + +""" +Computes the luminance of an RGB image and returns that as a new channel. + +Useful when images are saved as RGB (e.g. histopathology, photographs, etc.) +and the user desires to apply a recipe or pixel classifier to only one +channel, but wishes to retain the maximum amount of information from each. + +The luminance has a 30% contribution from the red channel, 59% +contribution from the green channel, and 11% contribution from the +blue channel. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Red : Aivia channel + Red channel. + +Green : Aivia channel + Green channel. + +Blue : Aivia channel + Blue channel. + +Returns +------- +Aivia channel + Result of the transform +""" + +# [INPUT Name:red_c Type:string DisplayName:'Red Channel'] +# [INPUT Name:blue_c Type:string DisplayName:'Blue Channel'] +# [INPUT Name:green_c Type:string DisplayName:'Green Channel'] +# [OUTPUT Name:gray_c Type:string DisplayName:'Luminance'] +def run(params): + red_c = params['red_c'] + blue_c = params['blue_c'] + green_c = params['green_c'] + gray_c = params['gray_c'] + if not os.path.exists(red_c): + print(f'Error: {red_c} does not exist') + return; + if not os.path.exists(blue_c): + print(f'Error: {blue_c} does not exist') + return; + if not os.path.exists(green_c): + print(f'Error: {green_c} does not exist') + return; + + red_data = imread(red_c) + blue_data = imread(blue_c) + green_data = imread(green_c) + + gray_data = np.empty_like(red_data) + + print(f'Red: {red_data.nbytes}') + print(f'Blue: {blue_data.nbytes}') + print(f'Green: {green_data.nbytes}') + print(f'Gray: {gray_data.nbytes}') + + gray_data = (0.3*red_data + 0.59*green_data + 0.11*blue_data).astype(red_data.dtype) + + imsave(gray_c, gray_data) + + +if __name__ == '__main__': + params = {} + params['red_c'] = 'test.png' + params['blue_c'] = 'test.png' + params['green_c'] = 'test.png' + params['gray_c'] = 'testResult.png'; + + run(params) diff --git a/Recipes/ShapeIndex.py b/Recipes/ShapeIndex.py new file mode 100644 index 0000000..7940a13 --- /dev/null +++ b/Recipes/ShapeIndex.py @@ -0,0 +1,101 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave +from skimage.feature import shape_index +from skimage.util import img_as_ubyte, img_as_uint + +np.seterr(divide='ignore', invalid='ignore') + +""" +See: https://scikit-image.org/docs/dev/api/skimage.feature.html#skimage.feature.shape_index + +Computes the shape index as derived from the eigenvalues of the Hessian +and returns it as a new channel scaled to 8bit space. + +Different values indicate convexity/concavitiy and shapes: + - cups / caps + - troughs / domes + - ruts / ridges + - saddle ruts / saddle ridges + - saddles + +In Aivia, this is useful as a way to compute an extra channel to use for +training a pixel classifier. + +For more information, see Koenderink & van Doorn as linked in the skimage documentation. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Gaussian Sigma : double + Gaussian smoothing size to use to determine local shape. + +Returns +------- +Aivia channel + Result of the transform normalized to 8bit or 16bit space according to the input. +""" + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:sigma Type:double DisplayName:'Gaussian Sigma' Default:3.0 Min:0.0 Max:50.0] +# [OUTPUT Name:resultPath Type:string DisplayName:'Shape Index'] +def run(params): + image_location = params['inputImagePath'] + result_location = params['resultPath'] + sigma = float(params['sigma']) + tCount = int(params['TCount']) + zCount = int(params['ZCount']) + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + image_data = imread(image_location) + dims = image_data.shape + shape_image = np.empty(image_data.shape, dtype=np.float32) + output_data = np.empty(image_data.shape) + + # 3D+T + if tCount > 1 and zCount > 1: + print('3dt') + print(image_data.shape, shape_image.shape) + print(shape_index(image_data[0,0,:,:], sigma=sigma, mode='reflect').dtype) + for t in range(0, dims[0]): + for z in range(0, dims[1]): + shape_image[t,z,:,:] = shape_index(image_data[t,z,:,:], sigma=sigma, mode='reflect').astype(np.float32) + # 2D+T or 3D + elif (tCount > 1 and zCount == 1) or (tCount == 1 and zCount > 1): + print('2dt or 3d') + print(image_data.shape, shape_image.shape) + for t in range(0, dims[0]): + shape_image[t,:,:] = shape_index(image_data[t,:,:], sigma=sigma, mode='reflect').astype(np.float32) + # 2D + else: + print('2d') + print(image_data.shape, shape_image.shape) + shape_image = shape_index(image_data, sigma=sigma, mode='reflect') + + # NaNs are usually returned - convert these to possible pixel values + shape_image = np.nan_to_num(shape_image) + + if image_data.dtype == np.uint16: + output_data = img_as_uint(shape_image) + else: + output_data = img_as_ubyte(shape_image) + + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['sigma'] = 3.0; + + run(params) diff --git a/Recipes/Skeletonize.py b/Recipes/Skeletonize.py new file mode 100644 index 0000000..c9ab276 --- /dev/null +++ b/Recipes/Skeletonize.py @@ -0,0 +1,109 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave +from skimage.morphology import skeletonize, skeletonize_3d +from skimage.morphology import closing, disk, ball + +np.seterr(divide='ignore', invalid='ignore') + +""" +See: https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize +and https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize_3d + +Computes a skeleton of the input image based on the thinning of its binarization: + + 1. The image is binarized (thresholded) based on the user's "Threshold" input + 2. The binarized image is thinned according to the methodology explained in + the linked documentation + 3. (Optional) The skeleton image is closed based on the radius provided by the user. + If the "Radius" is 0, no closing is performed. + 4. The skeleton is converted to the bit space from the original image. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Threshold : int + Grayvalue above which to mask. + +Closing Radius : int + Size of kernel used to "fill in" concavities or connect close ends of the skeleton. + +Returns +------- +Aivia channel + Result of the transform +""" + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:threshold Type:int DisplayName:'Threshold' Default:128 Min:0 Max:65535] +# [INPUT Name:radius Type:int DisplayName:'Closing Radius' Default:0 Min:0 Max:100] +# [OUTPUT Name:resultPath Type:string DisplayName:'Skeleton'] +def run(params): + image_location = params['inputImagePath'] + result_location = params['resultPath'] + threshold = int(params['threshold']) + radius = int(params['radius']) + tCount = int(params['TCount']) + zCount = int(params['ZCount']) + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + image_data = imread(image_location) + dims = image_data.shape + temp_array = np.empty_like(image_data) + output_data = np.empty_like(image_data) + + temp_array = np.where(image_data>threshold, 1, 0) + + if radius != 0: + if zCount > 1: + structure = ball(radius) + else: + structure = disk(radius) + + # 3D+T + if tCount > 1 and zCount > 1: + for t in range(0, dims[0]): + temp_array[t, :, :, :] = skeletonize_3d(temp_array[t, :, :, :]) + if radius != 0: + temp_array[t, :, :, :] = closing(temp_array[t, :, :, :], selem=structure) + # 2D+T + elif tCount > 1 and zCount == 1: + for t in range(0, dims[0]): + temp_array[t, :, :] = skeletonize(temp_array[t, :, :]) + if radius != 0: + temp_array[t, :, :] = closing(temp_array[t, :, :], selem=structure) + # 3D + elif tCount ==1 and zCount > 1: + temp_array = skeletonize_3d(temp_array) + if radius != 0: + temp_array = closing(temp_array, selem=structure) + # 2D + else: + temp_array = skeletonize(temp_array) + if radius != 0: + temp_array = closing(temp_array, selem=structure) + + + temp_array = np.where(temp_array.astype(image_data.dtype)>0, image_data.max(), 0) + + output_data = temp_array.astype(image_data.dtype) + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['threshold'] = 128 + params['radius'] = 0 + + run(params) diff --git a/Recipes/SkeletonizeObjects.py b/Recipes/SkeletonizeObjects.py new file mode 100644 index 0000000..5596bc3 --- /dev/null +++ b/Recipes/SkeletonizeObjects.py @@ -0,0 +1,119 @@ +import os.path +import numpy as np +from skimage.io import imread, imsave +from skimage.morphology import skeletonize, skeletonize_3d +from skimage.morphology import closing, disk, ball + +np.seterr(divide='ignore', invalid='ignore') + +""" +See: https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize +and https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize_3d + +Computes a skeleton of the input image based on the thinning of its binarization: + + 1. The image is binarized (thresholded) based on the user's "Threshold" input + 2. The binarized image is thinned according to the methodology explained in + the linked documentation + 3. (Optional) The skeleton image is closed based on the radius provided by the user. + If the "Radius" is 0, no closing is performed. + 4. The skeleton is converted to the bit space from the original image. + 5. The skeleton is passed to Aivia's mesh creation engine to output the result as a mesh. + +Requirements +------------ +numpy +scikit-image + +Parameters +---------- +Input Image : Aivia channel + Input channel to use for the transform. + +Threshold : int + Grayvalue above which to mask. + +Closing Radius : int + Size of kernel used to "fill in" concavities or connect close ends of the skeleton. + +Returns +------- +Aivia channel + Result of the transform +""" + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:threshold Type:int DisplayName:'Threshold' Default:128 Min:0 Max:65535] +# [INPUT Name:radius Type:int DisplayName:'Closing Radius' Default:0 Min:0 Max:100] +# [OUTPUT Name:resultImagePath Type:string DisplayName:'Skeleton Image'] +# [OUTPUT Name:resultObjectPath Type:string DisplayName:'Skeleton Objects' Objects:3D MinSize:0.0 MaxSize:1000000000.0] +def run(params): + image_location = params['inputImagePath'] + result_image_location = params['resultImagePath'] + result_object_location = params['resultObjectPath'] + threshold = int(params['threshold']) + radius = int(params['radius']) + tCount = int(params['TCount']) + zCount = int(params['ZCount']) + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + if zCount == 1: + print('Mesh creation with Python is currently only supported in 3D.') + print('Try using Skeletonize.py instead.') + return + + image_data = imread(image_location) + dims = image_data.shape + temp_array = np.empty_like(image_data) + output_data = np.empty_like(image_data) + + temp_array = np.where(image_data>threshold, 1, 0) + + if radius != 0: + if zCount > 1: + structure = ball(radius) + else: + structure = disk(radius) + + # 3D+T + if tCount > 1 and zCount > 1: + for t in range(0, dims[0]): + temp_array[t, :, :, :] = skeletonize_3d(temp_array[t, :, :, :]) + if radius != 0: + temp_array[t, :, :, :] = closing(temp_array[t, :, :, :], selem=structure) + # 2D+T + elif tCount > 1 and zCount == 1: + for t in range(0, dims[0]): + temp_array[t, :, :] = skeletonize(temp_array[t, :, :]) + if radius != 0: + temp_array[t, :, :] = closing(temp_array[t, :, :], selem=structure) + # 3D + elif tCount ==1 and zCount > 1: + temp_array = skeletonize_3d(temp_array) + if radius != 0: + temp_array = closing(temp_array, selem=structure) + # 2D + else: + temp_array = skeletonize(temp_array) + if radius != 0: + temp_array = closing(temp_array, selem=structure) + + + temp_array = np.where(temp_array.astype(image_data.dtype)>0, image_data.max(), 0) + + output_data = temp_array.astype(image_data.dtype) + imsave(result_image_location, output_data) + imsave(result_object_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultImagePath'] = 'testResult.png' + params['resultObjectPath'] = 'testResult.png' + params['threshold'] = 128 + params['radius'] = 0 + + run(params)