From 5a1c16c201d933c99c82b14f7df7d8ed3d5a3f06 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Tue, 10 Mar 2020 14:12:50 -0500 Subject: [PATCH 01/14] Added baseline set of recipes The recipes fail tests with some image dimensions and types, and need better docstring/requirement descriptions. --- Recipes/MaxMask.py | 47 +++++++++++++++++++ Recipes/MaxSlices.py | 55 ++++++++++++++++++++++ Recipes/MeijeringNeuriteness.py | 80 +++++++++++++++++++++++++++++++ Recipes/MinSlices.py | 53 +++++++++++++++++++++ Recipes/RGBtoLuminance.py | 63 +++++++++++++++++++++++++ Recipes/ShapeIndex.py | 69 +++++++++++++++++++++++++++ Recipes/Skeletonize.py | 79 +++++++++++++++++++++++++++++++ Recipes/SkeletonizeObjects.py | 83 +++++++++++++++++++++++++++++++++ Recipes/UnsharpMasking.py | 44 +++++++++++++++++ 9 files changed, 573 insertions(+) create mode 100644 Recipes/MaxMask.py create mode 100644 Recipes/MaxSlices.py create mode 100644 Recipes/MeijeringNeuriteness.py create mode 100644 Recipes/MinSlices.py create mode 100644 Recipes/RGBtoLuminance.py create mode 100644 Recipes/ShapeIndex.py create mode 100644 Recipes/Skeletonize.py create mode 100644 Recipes/SkeletonizeObjects.py create mode 100644 Recipes/UnsharpMasking.py diff --git a/Recipes/MaxMask.py b/Recipes/MaxMask.py new file mode 100644 index 0000000..9f001b4 --- /dev/null +++ b/Recipes/MaxMask.py @@ -0,0 +1,47 @@ +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)threshold, 1, 0) + + # 2. Skeletonize using default methods + if len(image_data.shape) == 2: + temp_array = skeletonize(temp_array) + else: + temp_array = skeletonize_3d(temp_array) + + # 3. Apply a closing if user inputs radius > 0 + if radius != 0: + if len(image_data.shape) == 2: + structure = disk(radius) + else: + structure = ball(radius) + temp_array = closing(temp_array, selem=structure) + + # 4. Put back into appropriate range for the bit depth + temp_array = np.where(temp_array.astype(image_data.dtype)>0, image_data.max(), 0) + + # 5. Finally assign the output + 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) + +# TODO: test skimage.morphology.medial_axis instead of skeletonize + +# CHANGELOG +# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) +# v1.01 TL - Removed unused skimage.exposure.rescale_intensity import \ No newline at end of file diff --git a/Recipes/SkeletonizeObjects.py b/Recipes/SkeletonizeObjects.py new file mode 100644 index 0000000..32ba194 --- /dev/null +++ b/Recipes/SkeletonizeObjects.py @@ -0,0 +1,83 @@ +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. +""" + +# [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']) + if not os.path.exists(image_location): + print(f'Error: {image_location} does not exist') + return; + + image_data = imread(image_location) + + temp_array = np.empty_like(image_data) + output_data = np.empty_like(image_data) + + # 1. Apply a mask given the input threshold + temp_array = np.where(image_data>threshold, 1, 0) + + # 2. Skeletonize using default methods + if len(image_data.shape) == 2: + temp_array = skeletonize(temp_array) + else: + temp_array = skeletonize_3d(temp_array) + + # 3. Apply a closing if user inputs radius > 0 + if radius != 0: + if len(image_data.shape) == 2: + structure = disk(radius) + else: + structure = ball(radius) + temp_array = closing(temp_array, selem=structure) + + # 4. Put back into appropriate range for the bit depth + temp_array = np.where(temp_array.astype(image_data.dtype)>0, image_data.max(), 0) + + # 5. Finally assign the output + 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) + +# TODO: test skimage.morphology.medial_axis instead of skeletonize + +# CHANGELOG +# v1.00 TL - Created as a fork from Skeletonize v1.01 \ No newline at end of file diff --git a/Recipes/UnsharpMasking.py b/Recipes/UnsharpMasking.py new file mode 100644 index 0000000..814722c --- /dev/null +++ b/Recipes/UnsharpMasking.py @@ -0,0 +1,44 @@ +import os.path +from numpy import empty_like +from skimage.io import imread, imsave +from skimage.filters import unsharp_mask + +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] +# [INPUT Name:radius Type:double DisplayName:'Radius' Default:1.0 Min:0.0 Max:100.0] +# [INPUT Name:amount Type:double DisplayName:'Amount' Default:1.0 Min:0.0 Max:100.0] +# [OUTPUT Name:resultPath Type:string DisplayName:'Sharpened'] +def run(params): + image_location = params['inputImagePath'] + result_location = params['resultPath'] + radius = float(params['radius']) + amount = float(params['amount']) + 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 = unsharp_mask(image_data, radius=radius, amount=amount, + multichannel=False, preserve_range=True).astype(image_data.dtype) + + imsave(result_location, output_data) + + +if __name__ == '__main__': + params = {} + params['inputImagePath'] = 'test.png' + params['resultPath'] = 'testResult.png' + params['radius'] = 1.0 + params['amount'] = 1.0 + + run(params) + +# TODO: Scale better to unsigned integers. W/O this the output is useless +# TODO: Double parameters can't be negative? +# TODO: Implement in 3D + +# CHANGELOG +# v0.01 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) +# \ No newline at end of file From 139ed3cdc03591da5df098558ba0c7ad7c00b45e Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Tue, 10 Mar 2020 15:17:10 -0500 Subject: [PATCH 02/14] Added .gitignore and updated AdjustGamma Updated AdjustGamma docstring and tested on TestData. Fails on 2D+T for now (known bug). Added gitignore for __pycache__. --- .gitignore | 1 + Recipes/AdjustGamma.py | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 .gitignore 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..bbe60e0 100644 --- a/Recipes/AdjustGamma.py +++ b/Recipes/AdjustGamma.py @@ -7,6 +7,22 @@ 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. + +Returns +------- +Aivia channel + Result of the transform + """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] @@ -16,16 +32,15 @@ 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) - + print(image_data.shape) output_data = empty_like(image_data) - output_data = adjust_gamma(image_data, gamma, 1) - imsave(result_location, output_data) @@ -37,8 +52,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 From aefffad4b7e27d4128446c3776fd48fc23569871 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Tue, 10 Mar 2020 15:19:50 -0500 Subject: [PATCH 03/14] Removed print from AdjustGamma --- Recipes/AdjustGamma.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Recipes/AdjustGamma.py b/Recipes/AdjustGamma.py index bbe60e0..d039eb1 100644 --- a/Recipes/AdjustGamma.py +++ b/Recipes/AdjustGamma.py @@ -38,7 +38,6 @@ def run(params): return; image_data = imread(image_location) - print(image_data.shape) output_data = empty_like(image_data) output_data = adjust_gamma(image_data, gamma, 1) imsave(result_location, output_data) From 00f0f4e6d1e1fbcabdda3495c3c6f9282fb3507d Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Tue, 10 Mar 2020 16:58:08 -0500 Subject: [PATCH 04/14] Update AdjustSigmoid.py Updated AdjustSigmoid according to #6 --- Recipes/AdjustSigmoid.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/Recipes/AdjustSigmoid.py b/Recipes/AdjustSigmoid.py index a6af93e..d75dbd7 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,24 @@ "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. + +Returns +------- +Aivia channel + Result of the transform """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] @@ -24,18 +43,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 +67,3 @@ def run(params): run(params) -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file From d75f7dea1507ab0c207dc88d45ae75982ac7f4fb Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Tue, 10 Mar 2020 17:07:00 -0500 Subject: [PATCH 05/14] Update MaxMask.py Formatted and tested according to #6 --- Recipes/MaxMask.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Recipes/MaxMask.py b/Recipes/MaxMask.py index 9f001b4..c0c7cac 100644 --- a/Recipes/MaxMask.py +++ b/Recipes/MaxMask.py @@ -7,6 +7,27 @@ the mask image is BELOW a specified threshold (t). O(x,y) = I(X,Y) * [M(X,Y) Date: Tue, 10 Mar 2020 17:09:25 -0500 Subject: [PATCH 06/14] Format AdjustGamma and AdjustSigmoid docstrings --- Recipes/AdjustGamma.py | 7 +++++-- Recipes/AdjustSigmoid.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Recipes/AdjustGamma.py b/Recipes/AdjustGamma.py index d039eb1..4e77448 100644 --- a/Recipes/AdjustGamma.py +++ b/Recipes/AdjustGamma.py @@ -16,12 +16,15 @@ Parameters ---------- Input Image : Aivia channel - Input channel to use for the transform. + Input channel to use for the transform. + +Gamma : double + Value to use for the gamma transform. Returns ------- Aivia channel - Result of the transform + Result of the transform """ diff --git a/Recipes/AdjustSigmoid.py b/Recipes/AdjustSigmoid.py index d75dbd7..b210515 100644 --- a/Recipes/AdjustSigmoid.py +++ b/Recipes/AdjustSigmoid.py @@ -26,12 +26,18 @@ Parameters ---------- Input Image : Aivia channel - Input channel to use for the transform. + 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 + Result of the transform """ # [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] From 72a6da4f3b9527fa812e42056af40786cdbad62b Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 04:53:36 -0500 Subject: [PATCH 07/14] Added MaxIP and MinIP scripts --- Recipes/MaxSlices.py | 56 +++++++++++++++++++++++++++++++------------- Recipes/MinSlices.py | 49 ++++++++++++++++++++++++++++---------- 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/Recipes/MaxSlices.py b/Recipes/MaxSlices.py index 6e40757..ad37b44 100644 --- a/Recipes/MaxSlices.py +++ b/Recipes/MaxSlices.py @@ -11,6 +11,25 @@ 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'] @@ -20,24 +39,34 @@ 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') + print(f"Error: {image_location} does not exist") return; image_data = imread(image_location) - print(f'Input data shape : {image_data.shape}') + dims = image_data.shape + output_data = np.empty_like(image_data) - if len(image_data.shape) == 2: - print(f'Error: Maximum intensity projection cannot be applied to 2D images.') + if len(dims) == 2 or (len(dims) == 3 and tCount > 1): + print('Error: Maximum intensity projection cannot be applied to 2D images.') return; - output_data = np.empty_like(image_data) - - width_eff = int(width/2) - max_slice = int(image_data.shape[0]) - 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) - + 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) @@ -48,8 +77,3 @@ def run(params): params['width'] = 2; run(params) - - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file diff --git a/Recipes/MinSlices.py b/Recipes/MinSlices.py index 45993db..3feedb7 100644 --- a/Recipes/MinSlices.py +++ b/Recipes/MinSlices.py @@ -11,6 +11,24 @@ 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'] @@ -20,22 +38,33 @@ 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(image_data.shape) == 2: - print(f'Error: Maximum intensity projection cannot be applied to 2D images.') + if len(dims) == 2 or (len(dims) == 3 and tCount > 1): + print('Error: Minimum intensity projection cannot be applied to 2D images.') return; - output_data = np.empty_like(image_data) - - width_eff = int(width/2) - max_slice = int(image_data.shape[0]) - 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) + 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) @@ -47,7 +76,3 @@ def run(params): params['width'] = 2; run(params) - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file From d5f31b077e04c0163febd88ca168f3356f1a5222 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 05:36:31 -0500 Subject: [PATCH 08/14] Update MeijeringNeuriteness.py --- Recipes/MeijeringNeuriteness.py | 73 ++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/Recipes/MeijeringNeuriteness.py b/Recipes/MeijeringNeuriteness.py index 4fdf2cd..f9413b9 100644 --- a/Recipes/MeijeringNeuriteness.py +++ b/Recipes/MeijeringNeuriteness.py @@ -2,7 +2,7 @@ import numpy as np from skimage.io import imread, imsave from skimage.filters import meijering -from skimage.exposure import rescale_intensity +from skimage.util import img_as_uint, img_as_ubyte np.seterr(divide='ignore', invalid='ignore') @@ -20,9 +20,28 @@ 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. -""" -print('Imports complete') +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] @@ -33,35 +52,45 @@ def run(params): result_location = params['resultPath'] sigma_min = float(params['sigma_min']) sigma_max = float(params['sigma_max']) - - print('inputs defined') + 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) - print('arrays set up') - - # Tried to allow user to input another double to represent the increment of - # sigma scales, but it broke everything. Here I replace that with an - # automatic increment to go from minimum to maximum sigma with 5 steps - sigmas = np.arange(sigma_min, sigma_max, round((sigma_max-sigma_min)/5,1)) + 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) - # Four corners of Meijering image are pure white and throw off the histogram - # Erase them based on the max of 1% of the image size or 4, whichever is more - crop_size = max(int(max(list(image_data.shape))/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 + # Cropping is performed in 2D to get rid of bright pixels at edges of the image. - output_data = rescale_intensity(meijering_image, out_range='uint8').astype(image_data.dtype) + 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) @@ -74,7 +103,3 @@ def run(params): params['sigma_max'] = 1.5 run(params) - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file From 66dd9edd651dff05dd70f444e91b545e6068bc54 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 05:47:38 -0500 Subject: [PATCH 09/14] Update MorphologicalTexture.py --- Recipes/MorphologicalTexture.py | 55 +++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 13 deletions(-) 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 From f4b3c598459e53b7bff5413f6784d74cfe45d2a6 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 05:50:46 -0500 Subject: [PATCH 10/14] Update RGBtoLuminance.py --- Recipes/RGBtoLuminance.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Recipes/RGBtoLuminance.py b/Recipes/RGBtoLuminance.py index ab5e2ca..1d4c4e3 100644 --- a/Recipes/RGBtoLuminance.py +++ b/Recipes/RGBtoLuminance.py @@ -12,6 +12,27 @@ 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'] @@ -57,7 +78,3 @@ def run(params): params['gray_c'] = 'testResult.png'; run(params) - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file From 899b03d0d33e5b38cd7d828ce979ab45484b3aea Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 06:15:48 -0500 Subject: [PATCH 11/14] Update ShapeIndex.py --- Recipes/ShapeIndex.py | 60 +++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/Recipes/ShapeIndex.py b/Recipes/ShapeIndex.py index 5e8f6e0..7940a13 100644 --- a/Recipes/ShapeIndex.py +++ b/Recipes/ShapeIndex.py @@ -2,7 +2,7 @@ import numpy as np from skimage.io import imread, imsave from skimage.feature import shape_index -from skimage.exposure import rescale_intensity +from skimage.util import img_as_ubyte, img_as_uint np.seterr(divide='ignore', invalid='ignore') @@ -23,6 +23,24 @@ 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'] @@ -32,26 +50,44 @@ 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) - output_data = np.empty_like(image_data) - shape_image = np.empty(image_data.shape) + dims = image_data.shape + shape_image = np.empty(image_data.shape, dtype=np.float32) + output_data = np.empty(image_data.shape) - # Returns -1:1 floats describing the shape index - if len(image_data.shape) == 2: - shape_image = shape_index(image_data, sigma=sigma, mode='reflect') + # 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: - for i in np.arange(0, int(image_data.shape[0])): - shape_image[i,:,:] = shape_index(image_data[i,:,:], sigma=sigma, mode='reflect') + 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) - # Need to rescale this shape index image to make sense in 8bit space - output_data = rescale_intensity(shape_image, out_range='uint8').astype(image_data.dtype) + 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) @@ -63,7 +99,3 @@ def run(params): params['sigma'] = 3.0; run(params) - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file From 964578c789218956e94d0397f65e23f7e1db8f1f Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 06:36:20 -0500 Subject: [PATCH 12/14] Update Skeletonize.py --- Recipes/Skeletonize.py | 76 +++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/Recipes/Skeletonize.py b/Recipes/Skeletonize.py index 1679441..c9ab276 100644 --- a/Recipes/Skeletonize.py +++ b/Recipes/Skeletonize.py @@ -18,6 +18,27 @@ 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'] @@ -29,36 +50,51 @@ def run(params): 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) - # 1. Apply a mask given the input threshold temp_array = np.where(image_data>threshold, 1, 0) - - # 2. Skeletonize using default methods - if len(image_data.shape) == 2: - temp_array = skeletonize(temp_array) - else: - temp_array = skeletonize_3d(temp_array) - - # 3. Apply a closing if user inputs radius > 0 + if radius != 0: - if len(image_data.shape) == 2: - structure = disk(radius) - else: + if zCount > 1: structure = ball(radius) - temp_array = closing(temp_array, selem=structure) - - # 4. Put back into appropriate range for the bit depth + 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) - # 5. Finally assign the output output_data = temp_array.astype(image_data.dtype) imsave(result_location, output_data) @@ -71,9 +107,3 @@ def run(params): params['radius'] = 0 run(params) - -# TODO: test skimage.morphology.medial_axis instead of skeletonize - -# CHANGELOG -# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# v1.01 TL - Removed unused skimage.exposure.rescale_intensity import \ No newline at end of file From ce97958e343de7dace9148558671828fe1dfc4b4 Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 06:42:53 -0500 Subject: [PATCH 13/14] Update SkeletonizeObjects.py --- Recipes/SkeletonizeObjects.py | 80 +++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/Recipes/SkeletonizeObjects.py b/Recipes/SkeletonizeObjects.py index 32ba194..5596bc3 100644 --- a/Recipes/SkeletonizeObjects.py +++ b/Recipes/SkeletonizeObjects.py @@ -19,6 +19,27 @@ 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'] @@ -32,36 +53,56 @@ def run(params): 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; - image_data = imread(image_location) + 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) - # 1. Apply a mask given the input threshold temp_array = np.where(image_data>threshold, 1, 0) - - # 2. Skeletonize using default methods - if len(image_data.shape) == 2: - temp_array = skeletonize(temp_array) - else: - temp_array = skeletonize_3d(temp_array) - - # 3. Apply a closing if user inputs radius > 0 + if radius != 0: - if len(image_data.shape) == 2: - structure = disk(radius) - else: + if zCount > 1: structure = ball(radius) - temp_array = closing(temp_array, selem=structure) - - # 4. Put back into appropriate range for the bit depth + 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) - # 5. Finally assign the output output_data = temp_array.astype(image_data.dtype) imsave(result_image_location, output_data) imsave(result_object_location, output_data) @@ -76,8 +117,3 @@ def run(params): params['radius'] = 0 run(params) - -# TODO: test skimage.morphology.medial_axis instead of skeletonize - -# CHANGELOG -# v1.00 TL - Created as a fork from Skeletonize v1.01 \ No newline at end of file From f0f2e0b053d3efb0c8aac21e40b8c4e39dc287ca Mon Sep 17 00:00:00 2001 From: Trevor Lancon <37873827+tlancon@users.noreply.github.com> Date: Wed, 11 Mar 2020 06:43:41 -0500 Subject: [PATCH 14/14] Delete UnsharpMasking.py --- Recipes/UnsharpMasking.py | 44 --------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 Recipes/UnsharpMasking.py diff --git a/Recipes/UnsharpMasking.py b/Recipes/UnsharpMasking.py deleted file mode 100644 index 814722c..0000000 --- a/Recipes/UnsharpMasking.py +++ /dev/null @@ -1,44 +0,0 @@ -import os.path -from numpy import empty_like -from skimage.io import imread, imsave -from skimage.filters import unsharp_mask - -# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] -# [INPUT Name:radius Type:double DisplayName:'Radius' Default:1.0 Min:0.0 Max:100.0] -# [INPUT Name:amount Type:double DisplayName:'Amount' Default:1.0 Min:0.0 Max:100.0] -# [OUTPUT Name:resultPath Type:string DisplayName:'Sharpened'] -def run(params): - image_location = params['inputImagePath'] - result_location = params['resultPath'] - radius = float(params['radius']) - amount = float(params['amount']) - 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 = unsharp_mask(image_data, radius=radius, amount=amount, - multichannel=False, preserve_range=True).astype(image_data.dtype) - - imsave(result_location, output_data) - - -if __name__ == '__main__': - params = {} - params['inputImagePath'] = 'test.png' - params['resultPath'] = 'testResult.png' - params['radius'] = 1.0 - params['amount'] = 1.0 - - run(params) - -# TODO: Scale better to unsigned integers. W/O this the output is useless -# TODO: Double parameters can't be negative? -# TODO: Implement in 3D - -# CHANGELOG -# v0.01 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com) -# \ No newline at end of file