Skip to content

Commit

Permalink
WIP 3D cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorTatarnikov committed Apr 26, 2024
1 parent b172fd6 commit 4aa8723
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
96 changes: 96 additions & 0 deletions brainglobe_registration/registration_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from brainglobe_registration.widgets.adjust_moving_image_view import (
AdjustMovingImageView,
)
from brainglobe_registration.widgets.crop_atlas_view import CropAtlasView
from brainglobe_registration.widgets.parameter_list_view import (
RegistrationParameterListView,
)
Expand Down Expand Up @@ -110,6 +111,18 @@ def __init__(self, napari_viewer: Viewer):
self._on_sample_popup_about_to_show
)

self.crop_atlas_widget = CropAtlasView(parent=self)

self.crop_atlas_widget.crop_x_signal.connect(
self._on_crop_atlas_x_signal
)
self.crop_atlas_widget.crop_y_signal.connect(
self._on_crop_atlas_y_signal
)
self.crop_atlas_widget.crop_z_signal.connect(
self._on_crop_atlas_z_signal
)

self.adjust_moving_image_widget = AdjustMovingImageView(parent=self)
self.adjust_moving_image_widget.adjust_image_signal.connect(
self._on_adjust_moving_image
Expand Down Expand Up @@ -151,6 +164,7 @@ def __init__(self, napari_viewer: Viewer):
collapsible=False,
)
self.add_widget(self.get_atlas_widget, widget_title="Select Images")
self.add_widget(self.crop_atlas_widget, widget_title="Crop Atlas")
self.add_widget(
self.adjust_moving_image_widget, widget_title="Prepare Images"
)
Expand Down Expand Up @@ -236,6 +250,27 @@ def _on_atlas_dropdown_index_changed(self, index):
)

self._viewer.grid.enabled = True
self.crop_atlas_widget.set_ranges(*self._atlas.reference.shape[-1::-1])
self.clipping_planes = [
{"position": (0, 0, 0), "normal": (1, 0, 0), "enabled": False},
{
"position": (self._atlas.reference.shape[2], 0, 0),
"normal": (-1, 0, 0),
"enabled": False,
},
{"position": (0, 0, 0), "normal": (0, 1, 0), "enabled": False},
{
"position": (0, self._atlas.reference.shape[1], 0),
"normal": (0, -1, 0),
"enabled": False,
},
{"position": (0, 0, 0), "normal": (0, 0, 1), "enabled": False},
{
"position": (0, 0, self._atlas.reference.shape[0]),
"normal": (0, 0, -1),
"enabled": False,
},
]

def _on_sample_dropdown_index_changed(self, index):
viewer_index = find_layer_index(
Expand All @@ -262,6 +297,63 @@ def _on_adjust_moving_image_reset_button_click(self):
return
adjust_napari_image_layer(self._moving_image, 0, 0, 0)

def _on_crop_atlas_x_signal(self, start: int, end: int):
if not (self._atlas_data_layer and self._atlas_annotations_layer):
show_error(
"No atlas selected. Please select an atlas before cropping"
)
return

self.clipping_planes[4]["enabled"] = True
self.clipping_planes[4]["position"] = (0, 0, start)
self.clipping_planes[5]["enabled"] = True
self.clipping_planes[5]["position"] = (0, 0, end)

self._atlas_data_layer.experimental_clipping_planes = (
self.clipping_planes
)
self._atlas_annotations_layer.experimental_clipping_planes = (
self.clipping_planes
)

def _on_crop_atlas_y_signal(self, start: int, end: int):
if not (self._atlas_data_layer and self._atlas_annotations_layer):
show_error(
"No atlas selected. Please select an atlas before cropping"
)
return

self.clipping_planes[2]["enabled"] = True
self.clipping_planes[2]["position"] = (0, start, 0)
self.clipping_planes[3]["enabled"] = True
self.clipping_planes[3]["position"] = (0, end, 0)

self._atlas_data_layer.experimental_clipping_planes = (
self.clipping_planes
)
self._atlas_annotations_layer.experimental_clipping_planes = (
self.clipping_planes
)

def _on_crop_atlas_z_signal(self, start: int, end: int):
if not (self._atlas_data_layer and self._atlas_annotations_layer):
show_error(
"No atlas selected. Please select an atlas before cropping"
)
return

self.clipping_planes[0]["enabled"] = True
self.clipping_planes[0]["position"] = (start, 0, 0)
self.clipping_planes[1]["enabled"] = True
self.clipping_planes[1]["position"] = (end, 0, 0)

self._atlas_data_layer.experimental_clipping_planes = (
self.clipping_planes
)
self._atlas_annotations_layer.experimental_clipping_planes = (
self.clipping_planes
)

def _on_run_button_click(self):

current_atlas_slice = self._viewer.dims.current_step[0]
Expand Down Expand Up @@ -465,6 +557,10 @@ def _on_adjust_atlas_rotation(self, pitch: float, yaw: float, roll: float):
output_chunks=(2, bounding_box[1], bounding_box[2]),
)

self.crop_atlas_widget.set_ranges(
*self._atlas_data_layer.data.shape[-1::-1]
)

worker = self.compute_atlas_rotation(self._atlas_data_layer.data)
worker.returned.connect(self.set_atlas_layer_data)
worker.start()
Expand Down
58 changes: 58 additions & 0 deletions brainglobe_registration/widgets/crop_atlas_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QLabel, QVBoxLayout, QWidget
from superqt import QLabeledRangeSlider


class CropAtlasView(QWidget):
crop_z_signal = Signal(int, int)
crop_y_signal = Signal(int, int)
crop_x_signal = Signal(int, int)

def __init__(self, parent=None):
super().__init__(parent=parent)
self.setLayout(QVBoxLayout())

self.x_slider = QLabeledRangeSlider(Qt.Orientation.Horizontal)
self.y_slider = QLabeledRangeSlider(Qt.Orientation.Horizontal)
self.z_slider = QLabeledRangeSlider(Qt.Orientation.Horizontal)

self.set_ranges(100, 100, 100)

self.layout().addWidget(QLabel("Crop X"))
self.layout().addWidget(self.x_slider)
self.layout().addWidget(QLabel("Crop Y"))
self.layout().addWidget(self.y_slider)
self.layout().addWidget(QLabel("Crop Z"))
self.layout().addWidget(self.z_slider)

self.x_slider.sliderMoved.connect(self._on_crop_x)
self.y_slider.sliderMoved.connect(self._on_crop_y)
self.z_slider.sliderMoved.connect(self._on_crop_z)
# Emits twice, but if not connected, editing text doesn't emit signal
# via sliderMoved
self.x_slider.editingFinished.connect(self._on_crop_x)
self.y_slider.editingFinished.connect(self._on_crop_y)
self.z_slider.editingFinished.connect(self._on_crop_z)

def set_ranges(self, x_max, y_max, z_max):
self.x_slider.setRange(0, x_max - 1)
self.x_slider.setValue((0, x_max - 1))
self.y_slider.setRange(0, y_max - 1)
self.y_slider.setValue((0, y_max - 1))
self.z_slider.setRange(0, z_max - 1)
self.z_slider.setValue((0, z_max - 1))

def _on_crop_x(self):
self.crop_x_signal.emit(
self.x_slider.value()[0], self.x_slider.value()[1]
)

def _on_crop_y(self):
self.crop_y_signal.emit(
self.y_slider.value()[0], self.y_slider.value()[1]
)

def _on_crop_z(self):
self.crop_z_signal.emit(
self.z_slider.value()[0], self.z_slider.value()[1]
)

0 comments on commit 4aa8723

Please sign in to comment.