Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions active_plugins/gaussian_byslice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
Gaussian_BySlice
==============

**Gaussian_BySlice** will blur an image and remove noise. Filtering an
image with a Gaussian filter can be helpful if the foreground signal is
noisy or near the noise floor.

|

============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES NO
============ ============ ===============
"""

import numpy
import skimage.filters
from cellprofiler_core.image import Image
from cellprofiler_core.module import ImageProcessing
from cellprofiler_core.setting import Binary
from cellprofiler_core.setting.text import Integer


class Gaussian_BySlice(ImageProcessing):
category = "Advanced"

module_name = "Gaussian_BySlice"

variable_revision_number = 2

def create_settings(self):
super(Gaussian_BySlice, self).create_settings()

self.sigma = Integer(
text="Sigma",
value=1,
doc="Standard deviation of the kernel to be used for blurring. Larger sigmas induce more blurring.",
)

self.by_slice = Binary(
text="Process by slice",
value=False,
doc="If enabled, for 3D images, the Gaussian filter is applied to each Z-plane independently. Setting ignored if images are not 3D.",
)

def run(self, workspace):
x_name = self.x_name.value

y_name = self.y_name.value

images = workspace.image_set

x = images.get_image(x_name)

dimensions = x.dimensions

x_data = x.pixel_data

sigma = numpy.divide(self.sigma.value, x.spacing)

if self.by_slice.value and x.volumetric:
y_data = numpy.zeros_like(x_data, dtype=float)
for index, plane in enumerate(x_data):
y_data[index] = skimage.filters.gaussian(plane, sigma=self.sigma.value)
else:
y_data = skimage.filters.gaussian(x_data, sigma=sigma)

y = Image(dimensions=dimensions, image=y_data, parent_image=x)

images.add(y_name, y)

if self.show_window:
workspace.display_data.x_data = x_data

workspace.display_data.y_data = y_data

workspace.display_data.dimensions = dimensions

def settings(self):
__settings__ = super(Gaussian_BySlice, self).settings()

return __settings__ + [self.sigma, self.by_slice]

def visible_settings(self):
__settings__ = super(Gaussian_BySlice, self).visible_settings()

__settings__ += [self.sigma, self.by_slice]

return __settings__

def upgrade_settings(self, setting_values, variable_revision_number, module_name):
"""Adjust the setting_values for older save file versions

setting_values - a list of strings representing the settings for
this module.
variable_revision_number - the variable revision number of the module
that saved the settings
module_name - the name of the module that saved the settings

returns the modified settings, revision number
"""
if variable_revision_number == 1:
setting_values = setting_values + ["No"]
variable_revision_number = 2

return setting_values, variable_revision_number
136 changes: 136 additions & 0 deletions active_plugins/masktopbottomz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#################################
#
# Imports from useful Python libraries
#
#################################

import numpy

#################################
#
# Imports from CellProfiler
#
##################################

__doc__ = """\
MaskTopAndBottomZ
=================

Set planes to ignore/set to all masked in a volumetric binary mask

============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES YES
============ ============ ===============


What do I need as input?
^^^^^^^^^^^^^^^^^^^^^^^^

A binary image

What do I get as output?
^^^^^^^^^^^^^^^^^^^^^^^^

A binary image, with the top and/or bottom all masked out


"""

from cellprofiler_core.image import Image
from cellprofiler_core.module import ImageProcessing
from cellprofiler_core.setting.text import Integer



class MaskTopBottomZ(ImageProcessing):

module_name = "MaskTopAndBottomZ"

variable_revision_number = 1

def create_settings(self):

super(MaskTopBottomZ, self).create_settings()

self.x_name.doc = """\
This is the image that the module operates on. You can choose any image
that is made available by a prior module.

**ImageTemplate** will do something to this image.
"""


self.bottom_remove = Integer(
text="Planes from the bottom to remove?",
value=0,
doc="""\
Planes to remove from the lowest-number-Z-plane side of the image
""",
)

self.top_remove = Integer(
text="Planes from the top to remove?",
value=0,
doc="""\
Planes to remove from the highest-number-Z-plane side of the image
""",
)

def settings(self):

settings = super(MaskTopBottomZ, self).settings()

return settings + [self.bottom_remove, self.top_remove]


def visible_settings(self):

visible_settings = super(MaskTopBottomZ, self).visible_settings()

visible_settings += [self.bottom_remove, self.top_remove]

return visible_settings


def run(self, workspace):

try:
binary_image = workspace.image_set.get_image(
self.x_name.value, must_be_binary=True
)
binary_pixels = binary_image.pixel_data
except ValueError:
binary_image = workspace.image_set.get_image(
self.x_name.value, must_be_grayscale=True
)
binary_pixels = binary_image.pixel_data
binary_pixels = binary_pixels > 0.5

if not binary_image.volumetric:
raise Exception("This module can only be used for volumetric images")

number_planes = binary_pixels.shape[0]

if self.bottom_remove.value + self.top_remove.value > number_planes:
raise Exception("You are removing more planes than the image has")

binary_pixels[:self.bottom_remove.value,:,:] = False

binary_pixels[-self.top_remove.value:,:,:] = False

y = Image(dimensions=binary_image.dimensions, image=binary_pixels, parent_image=binary_image)

workspace.image_set.add(self.y_name.value, y)

if self.show_window:
workspace.display_data.x_data = binary_image.pixel_data

workspace.display_data.y_data = binary_pixels

workspace.display_data.dimensions = binary_image.dimensions

def volumetric(self):
return True

Loading