Skip to content

Commit

Permalink
Filter Images (#52)
Browse files Browse the repository at this point in the history
* work_in_progress

* filter_images

* filter_images

* filter_images

* filter_images

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent 5b55032 commit f690dea
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
26 changes: 23 additions & 3 deletions brainglobe_registration/registration_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
from napari.utils.notifications import show_error
from napari.viewer import Viewer
from pytransform3d.rotations import active_matrix_from_angle
from qtpy.QtWidgets import QPushButton, QTabWidget
from qtpy.QtWidgets import QCheckBox, QPushButton, QTabWidget
from skimage.segmentation import find_boundaries
from skimage.transform import rescale

from brainglobe_registration.utils.utils import (
adjust_napari_image_layer,
calculate_rotated_bounding_box,
check_atlas_installed,
filter_plane,
find_layer_index,
get_image_layer_names,
open_parameter_file,
Expand Down Expand Up @@ -135,6 +136,9 @@ def __init__(self, napari_viewer: Viewer):
self._on_default_file_selection_change
)

self.filter_checkbox = QCheckBox("Filter Images")
self.filter_checkbox.setChecked(False)

self.run_button = QPushButton("Run")
self.run_button.clicked.connect(self._on_run_button_click)
self.run_button.setEnabled(False)
Expand Down Expand Up @@ -170,6 +174,9 @@ def __init__(self, napari_viewer: Viewer):
self.add_widget(
self.parameters_tab, widget_title="Advanced Settings (optional)"
)

self.add_widget(self.filter_checkbox, collapsible=False)

self.add_widget(self.run_button, collapsible=False)

self.layout().itemAt(1).widget().collapse(animate=False)
Expand Down Expand Up @@ -271,9 +278,22 @@ def _on_run_button_click(self):

current_atlas_slice = self._viewer.dims.current_step[0]

if self.filter_checkbox.isChecked():
atlas_layer = filter_plane(
self._atlas_data_layer.data[
current_atlas_slice, :, :
].compute()
)
moving_image_layer = filter_plane(self._moving_image.data)
else:
atlas_layer = self._atlas_data_layer.data[
current_atlas_slice, :, :
]
moving_image_layer = self._moving_image.data

result, parameters, registered_annotation_image = run_registration(
self._atlas_data_layer.data[current_atlas_slice, :, :],
self._moving_image.data,
atlas_layer,
moving_image_layer,
self._atlas_annotations_layer.data[current_atlas_slice, :, :],
self.transform_selections,
)
Expand Down
79 changes: 79 additions & 0 deletions brainglobe_registration/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from brainglobe_utils.qtpy.dialog import display_info
from pytransform3d.rotations import active_matrix_from_angle
from qtpy.QtWidgets import QWidget
from scipy.ndimage import gaussian_filter
from skimage import morphology


def adjust_napari_image_layer(
Expand Down Expand Up @@ -175,3 +177,80 @@ def check_atlas_installed(parent_widget: QWidget):
"brainglobe-atlasapi</a> or <a href='https://brainglobe.info/"
"tutorials/manage-atlases-in-GUI.html'>brainrender-napari</a>",
)


def filter_plane(img_plane):
"""
Apply a set of filter to the plane (typically to avoid overfitting details
in the image during registration)
The filter is composed of a despeckle filter using opening and a pseudo
flatfield filter
Originally from: [https://github.com/brainglobe/brainreg/blob/main
/brainreg/core/utils/preprocess.py]
Parameters
----------
img_plane : np.array
A 2D array to filter
Returns
-------
np.array
Filtered image
"""

img_plane = despeckle_by_opening(img_plane)
img_plane = pseudo_flatfield(img_plane)
return img_plane


def despeckle_by_opening(img_plane, radius=2):
"""
Despeckle the image plane using a grayscale opening operation
Originally from: [https://github.com/brainglobe/brainreg/blob/main
/brainreg/core/utils/preprocess.py]
Parameters
----------
img_plane : np.array
The image to filter
radius: int
The radius of the opening kernel
Returns
-------
np.array
The despeckled image
"""
kernel = morphology.disk(radius)
morphology.opening(img_plane, out=img_plane, footprint=kernel)
return img_plane


def pseudo_flatfield(img_plane, sigma=5):
"""
Pseudo flat field filter implementation using a de-trending by a
heavily gaussian filtered copy of the image.
Originally from: [https://github.com/brainglobe/brainreg/blob/main
/brainreg/core/utils/preprocess.py]
Parameters
----------
img_plane : np.array
The image to filter
sigma : int
The sigma of the gaussian filter applied to the
image used for de-trending
Returns
-------
np.array
The pseudo flat field filtered image
"""
filtered_img = gaussian_filter(img_plane, sigma)
return img_plane / (filtered_img + 1)

0 comments on commit f690dea

Please sign in to comment.