roiSelectionWidget.py 7.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/


__authors__ = ["J. Garriga"]
__license__ = "MIT"
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
29
__date__ = "06/12/2019"
30

31
# import os
32

Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
33
import logging
34
import numpy
35
36

from silx.gui import qt
37
from silx.gui.colors import Colormap
38
39
40
41
from silx.gui.plot.StackView import StackViewMainWindow
from silx.gui.plot.items.roi import RectangleROI
from silx.gui.plot.tools.roi import RegionOfInterestManager, RegionOfInterestTableWidget

42
import darfix
43
from darfix.core.roi import apply_3D_ROI
44
45


Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
46
47
48
_logger = logging.getLogger(__file__)


49
50
51
52
class ROISelectionWidget(qt.QWidget):
    """
    Widget that allows the user to pick a ROI in any image of the dataset.
    """
53
    signalComputed = qt.Signal(list, list)
54

55
56
57
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

58
59
        self.roi = None

60
61
        self.setLayout(qt.QVBoxLayout())
        self._sv = StackViewMainWindow()
62
63
64
65
66
67
68
69
70
        _buttons = qt.QDialogButtonBox(parent=self)
        self._okB = _buttons.addButton(_buttons.Ok)
        self._applyB = _buttons.addButton(_buttons.Apply)
        self._resetB = _buttons.addButton(_buttons.Reset)

        self._applyB.clicked.connect(self.applyRoi)
        self._okB.clicked.connect(self.apply)
        self._resetB.clicked.connect(self.resetStack)

71
72
73
74
        self._sv.setColormap(Colormap(name=darfix.config.DEFAULT_COLORMAP_NAME,
                                      normalization='linear',
                                      vmin=None,
                                      vmax=None))
75
        self.layout().addWidget(self._sv)
76
        self.layout().addWidget(_buttons)
77
78
79
80
81
82
83
84

        plot = self._sv.getPlot()
        self._roiManager = RegionOfInterestManager(plot)
        self._roiTable = RegionOfInterestTableWidget()
        self._roiTable.setRegionOfInterestManager(self._roiManager)

        self._roi = RectangleROI()
        self._roi.setLabel('ROI')
85
86
        self._roi.setGeometry(origin=(0, 0), size=(10, 10))
        self._roi.setEditable(True)
87
        self._roiManager.addRoi(self._roi)
88
        self._roiTable.setColumnHidden(4, True)
89
90
91
92
93
94
95
96

        # Add the region of interest table and the buttons to a dock widget
        widget = qt.QWidget()
        layout = qt.QVBoxLayout()
        widget.setLayout(layout)
        layout.addWidget(self._roiTable)

        def roiDockVisibilityChanged(visible):
97
            """Handle change of visibility of the roi dock widget.
98
99
100
101
102
103
104
105
106
107
108
109
110
111

            If dock becomes hidden, ROI interaction is stopped.
            """
            if not visible:
                self._roiManager.stop()

        dock = qt.QDockWidget('Image ROI')
        dock.setWidget(widget)
        dock.visibilityChanged.connect(roiDockVisibilityChanged)
        plot.addTabbedDockWidget(dock)

    def setDataset(self, dataset):
        """
        Dataset setter. Saves the dataset and updates the stack with the dataset
112
        data.
113

114
        :param Dataset dataset: dataset to be used in the widget.
115
116
117
118
119
120
        """
        self.dataset = dataset
        self.setStack(dataset.data)

    def setStack(self, *arg, **kwargs):
        """
121
        Sets the data passed as arguments in the stack.
122
        Mantains the current frame showed in the view.
123
        """
124
        first_frame_shape = self.dataset.data[0].shape
125
126
        self.setRoi(center=(first_frame_shape[1] / 2, first_frame_shape[0] / 2),
                    size=(first_frame_shape[1] / 5, first_frame_shape[0] / 5))
127
        nframe = self._sv.getFrameNumber()
128
        self._sv.setStack(*arg, **kwargs)
129
        self._sv.setFrameNumber(nframe)
130

131
132
    def setRoi(self, roi=None, origin=None, size=None, center=None):
        """
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
133
        Sets a region of interest of the stack of images.
134
135
136

        :param RectangleROI roi: A region of interest.
        :param Tuple origin: If a roi is not provided, used as an origin for the roi
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
137
138
        :param Tuple size: If a roi is not provided, used as a size for the roi.
        :param Tuple center: If a roi is not provided, used as a center for the roi.
139
        """
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
140
141
        if roi is not None and (size is not None or center is not None or origin is not None):
            _logger.warning("Only using provided roi, the rest of parameters are omitted")
142
143
144
145
146
147
148
149
150
151
152
153
154

        if roi is not None:
            self._roi = roi
        else:
            self._roi.setGeometry(origin=origin, size=size, center=center)

    def getRoi(self):
        """
        Returns the roi selected in the stackview.

        :rtype: silx.gui.plot.items.roi.RectangleROI
        """
        return self._roi
155
156

    def applyRoi(self):
157
        """
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
158
159
160
        Function to apply the region of interest at the data of the dataset
        and show the new data in the stack. Dataset data is not yet replaced.
        A new roi is created in the middle of the new stack.
161
        """
162
163
        self.roi = RectangleROI()
        self.roi.setGeometry(origin=self.getRoi().getOrigin(), size=self.getRoi().getSize())
164
        frames = apply_3D_ROI(self._sv.getStack()[0], size=numpy.flip(self.roi.getSize()),
165
                              center=numpy.flip(self.roi.getCenter()))
166
167
168
169
170

        self.setStack(frames)
        self.resetROI()

    def apply(self):
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
171
172
173
174
175
        """
        Function that replaces the dataset data with the data shown in the stack of images.
        If the stack has a roi applied, it applies the same roi to the dark frames of the dataset.
        Signal emitted with the roi parameters.
        """
176
177
178
179
180
181
182
183
184
185
        self.dataset.data = self._sv.getStack()[0]
        if self.roi:
            if len(self.dataset.dark_frames):
                self.dataset.dark_frames = apply_3D_ROI(self.dataset.dark_frames,
                                                        self.roi.getSize() / 2, numpy.flip(self.roi.getCenter()))
            self.signalComputed.emit(self.roi.getOrigin().tolist(), self.roi.getSize().tolist())
        else:
            self.signalComputed.emit([], [])

    def resetROI(self):
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
186
187
188
        """
        Sets the region of interest in the middle of the stack, with size 1/5 of the image.
        """
189
        frame_shape = numpy.array(self._sv.getStack()[0][0].shape)
190
191
        center = numpy.flip(frame_shape) / 2
        size = numpy.flip(frame_shape) / 5
192
193
194
        self.setRoi(center=center, size=size)

    def resetStack(self):
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
195
196
197
        """
        Restores stack with the dataset data.
        """
198
        self.roi = None
199
200
201
        self.setStack(self.dataset.data)

    def clearStack(self):
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
202
203
204
        """
        Clears stack.
        """
205
206
207
        self._okB.setEnabled(False)
        self._sv.setStack(None)
        self._roi.setGeometry(origin=(0, 0), size=(10, 10))