Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Recipes/__pycache__
28 changes: 20 additions & 8 deletions Recipes/AdjustGamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)


Expand All @@ -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)
#
40 changes: 31 additions & 9 deletions Recipes/AdjustSigmoid.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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']
Expand All @@ -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)


Expand All @@ -48,6 +73,3 @@ def run(params):

run(params)

# CHANGELOG
# v1.00 TL - Original script by Trevor Lancon (trevorl@drvtechnologies.com)
#
62 changes: 62 additions & 0 deletions Recipes/MaxMask.py
Original file line number Diff line number Diff line change
@@ -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)<t]

Requirements
------------
numpy
scikit-image

Parameters
----------
Input Image : Aivia channel
Input channel to be masked.

Input Mask : Aivia channel
Input channel to use for the mask after thresholding.

Threshold : int
Grayvalue in above which we would like to mask.

Returns
-------
Aivia channel
Result of the transform
"""

# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image']
# [INPUT Name:inputMaskImagePath Type:string DisplayName:'Input Mask']
# [INPUT Name:threshold Type:int DisplayName:'Masking Threshold' Default:128 Min:0 Max:65535]
# [OUTPUT Name:resultPath Type:string DisplayName:'Masked Image']
def run(params):
image_location = params['inputImagePath']
mask_location = params['inputMaskImagePath']
result_location = params['resultPath']
threshold = int(params['threshold'])
if not os.path.exists(image_location):
print(f'Error: {image_location} does not exist')
return;

image_data = imread(image_location)
mask_data = imread(mask_location)
output_data = np.empty_like(image_data)
output_data = np.where(mask_data<threshold, image_data, 0)
output_data = output_data.astype(image_data.dtype)
imsave(result_location, output_data)


if __name__ == '__main__':
params = {}
params['inputImagePath'] = 'test.png'
params['inputMaskImagePath'] = 'test.png'
params['resultPath'] = 'testResult.png'
params['threshold'] = 128

run(params)

79 changes: 79 additions & 0 deletions Recipes/MaxSlices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os.path
import numpy as np
from skimage.io import imread, imsave

"""
Performs a slicewise maximum 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 maximum 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:'MaximumZ']
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: 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)
105 changes: 105 additions & 0 deletions Recipes/MeijeringNeuriteness.py
Original file line number Diff line number Diff line change
@@ -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)
Loading