Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DCT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
graintracking
DCT
Commits
157465a7
Commit
157465a7
authored
3 years ago
by
casagran
Browse files
Options
Downloads
Patches
Plain Diff
adapt roi selection
parent
fc4836d5
No related branches found
Branches containing commit
No related tags found
1 merge request
!6
Draft: Resolve "py-bkg-rm"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
zUtil_Python/pydct/orangecontrib/pydct/widgets/roi_selection.py
+262
-0
262 additions, 0 deletions
...Python/pydct/orangecontrib/pydct/widgets/roi_selection.py
with
262 additions
and
0 deletions
zUtil_Python/pydct/orangecontrib/pydct/widgets/roi_selection.py
0 → 100644
+
262
−
0
View file @
157465a7
""""""
# coding: utf-8
# import numpy
import
logging
import
darfix
import
numpy
from
numpy
import
ndarray
from
Orange.widgets.settings
import
Setting
from
Orange.widgets.widget
import
Input
,
Output
,
OWWidget
from
silx.gui
import
qt
from
silx.gui.colors
import
Colormap
from
silx.gui.plot.items.roi
import
RectangleROI
from
silx.gui.plot.StackView
import
StackViewMainWindow
from
silx.gui.plot.tools.roi
import
RegionOfInterestManager
,
RegionOfInterestTableWidget
# from silx.gui import qt
# from darfix.gui.roiSelectionWidget import ROISelectionWidget
_logger
=
logging
.
getLogger
(
__file__
)
class
ROISelectionWidget
(
qt
.
QWidget
):
"""
Widget that allows the user to pick a ROI in any image of the dataset.
"""
sigComputed
=
qt
.
Signal
(
list
,
list
)
def
__init__
(
self
,
parent
=
None
):
qt
.
QWidget
.
__init__
(
self
,
parent
)
self
.
roi
=
None
self
.
_update_dataset
=
None
self
.
indices
=
None
self
.
bg_indices
=
None
self
.
bg_dataset
=
None
self
.
setLayout
(
qt
.
QVBoxLayout
())
self
.
_sv
=
StackViewMainWindow
()
_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
)
self
.
_sv
.
setColormap
(
Colormap
(
name
=
darfix
.
config
.
DEFAULT_COLORMAP_NAME
,
normalization
=
darfix
.
config
.
DEFAULT_COLORMAP_NORM
)
)
self
.
layout
().
addWidget
(
self
.
_sv
)
self
.
layout
().
addWidget
(
_buttons
)
plot
=
self
.
_sv
.
getPlot
()
self
.
_roiManager
=
RegionOfInterestManager
(
plot
)
self
.
_roiTable
=
RegionOfInterestTableWidget
()
self
.
_roiTable
.
setRegionOfInterestManager
(
self
.
_roiManager
)
self
.
_roi
=
RectangleROI
()
self
.
_roi
.
setLabel
(
"
ROI
"
)
self
.
_roi
.
setGeometry
(
origin
=
(
0
,
0
),
size
=
(
10
,
10
))
self
.
_roi
.
setEditable
(
True
)
self
.
_roiManager
.
addRoi
(
self
.
_roi
)
self
.
_roiTable
.
setColumnHidden
(
4
,
True
)
# 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
):
"""
Handle change of visibility of the roi dock widget.
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
,
indices
=
None
,
bg_indices
=
None
,
bg_dataset
=
None
):
"""
Dataset setter. Saves the dataset and updates the stack with the dataset
data
:param Dataset dataset: dataset
"""
self
.
dataset
=
dataset
self
.
_update_dataset
=
dataset
self
.
indices
=
indices
self
.
setStack
()
self
.
bg_indices
=
bg_indices
self
.
bg_dataset
=
bg_dataset
def
setStack
(
self
,
dataset
=
None
):
"""
Sets new data to the stack.
Mantains the current frame showed in the view.
:param Dataset dataset: if not None, data set to the stack will be from the given dataset.
"""
if
dataset
is
None
:
dataset
=
self
.
dataset
# first_frame_shape = dataset.get_data()[0].shape
first_frame_shape
=
dataset
[
0
].
shape
self
.
setRoi
(
center
=
(
first_frame_shape
[
1
]
/
2
,
first_frame_shape
[
0
]
/
2
),
size
=
(
first_frame_shape
[
1
]
/
5
,
first_frame_shape
[
0
]
/
5
),
)
nframe
=
self
.
_sv
.
getFrameNumber
()
# self._sv.setStack(dataset.get_data())
self
.
_sv
.
setStack
(
dataset
)
self
.
_sv
.
setFrameNumber
(
nframe
)
def
setRoi
(
self
,
roi
=
None
,
origin
=
None
,
size
=
None
,
center
=
None
):
# """
# Sets a region of interest of the stack of images.
#
# :param RectangleROI roi: A region of interest.
# :param Tuple origin: If a roi is not provided, used as an origin for the roi
# :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.
# """
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
"
)
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
def
applyRoi
(
self
):
"""
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.
"""
self
.
roi
=
RectangleROI
()
self
.
roi
.
setGeometry
(
origin
=
self
.
getRoi
().
getOrigin
(),
size
=
self
.
getRoi
().
getSize
())
# self._update_dataset = self.dataset.apply_roi(size=numpy.flip(self.roi.getSize()),
# center=numpy.flip(self.roi.getCenter()))
# self.setStack(self._update_dataset)
# self.resetROI()
def
apply
(
self
):
"""
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.
"""
if
self
.
roi
:
if
self
.
bg_dataset
is
not
None
:
self
.
bg_dataset
=
self
.
bg_dataset
.
apply_roi
(
self
.
roi
.
getSize
()
/
2
,
numpy
.
flip
(
self
.
roi
.
getCenter
()))
self
.
sigComputed
.
emit
(
self
.
roi
.
getOrigin
().
tolist
(),
self
.
roi
.
getSize
().
tolist
())
else
:
self
.
sigComputed
.
emit
([],
[])
def
getDataset
(
self
):
return
self
.
_update_dataset
,
self
.
indices
,
self
.
bg_indices
,
self
.
bg_dataset
def
resetROI
(
self
):
"""
Sets the region of interest in the middle of the stack, with size 1/5 of the image.
"""
# frame_shape = numpy.array(self._update_dataset.get_data(0).shape)
frame_shape
=
numpy
.
array
(
self
.
_update_dataset
[
0
].
shape
)
center
=
numpy
.
flip
(
frame_shape
)
/
2
size
=
numpy
.
flip
(
frame_shape
)
/
5
self
.
setRoi
(
center
=
center
,
size
=
size
)
def
resetStack
(
self
):
"""
Restores stack with the dataset data.
"""
self
.
roi
=
None
self
.
_update_dataset
=
self
.
dataset
self
.
setStack
(
self
.
dataset
)
def
clearStack
(
self
):
"""
Clears stack.
"""
self
.
_sv
.
setStack
(
None
)
self
.
_roi
.
setGeometry
(
origin
=
(
0
,
0
),
size
=
(
10
,
10
))
class
RoiSelectionWidgetOW
(
OWWidget
):
name
=
"
roi selection
"
icon
=
"
icons/one_round.png
"
want_main_area
=
False
# Inputs/Outputs
class
Inputs
:
data
=
Input
(
"
data
"
,
ndarray
)
colormap
=
Input
(
"
colormap
"
,
Colormap
)
class
Outputs
:
roi_origin
=
Output
(
"
roi_origin
"
,
list
)
roi_size
=
Output
(
"
roi_size
"
,
list
)
# data = Output("data", ndarray)
# colormap = Output("colormap", Colormap)
# Settings
roi_origin
=
Setting
(
list
(),
schema_only
=
True
)
roi_size
=
Setting
(
list
(),
schema_only
=
True
)
def
__init__
(
self
):
super
().
__init__
()
self
.
_widget
=
ROISelectionWidget
(
parent
=
self
)
self
.
controlArea
.
layout
().
addWidget
(
self
.
_widget
)
self
.
_widget
.
sigComputed
.
connect
(
self
.
_sendSignal
)
@Inputs.data
def
setDataset
(
self
,
data
):
if
data
is
not
None
:
self
.
_widget
.
setDataset
(
data
)
else
:
self
.
_widget
.
clearStack
()
# Emit None
self
.
Outputs
.
roi
.
send
(
None
)
# Set saved roi
if
len
(
self
.
roi_origin
)
and
len
(
self
.
roi_size
):
self
.
_widget
.
setRoi
(
origin
=
self
.
roi_origin
,
size
=
self
.
roi_size
)
# self.open()
@Inputs.colormap
def
setColormap
(
self
,
colormap
):
self
.
_widget
.
setStackViewColormap
(
colormap
)
def
_sendSignal
(
self
,
roi_origin
=
[],
roi_size
=
[]):
"""
Emits the signal with the new data.
"""
self
.
close
()
self
.
roi_origin
=
roi_origin
self
.
roi_size
=
roi_size
self
.
Outputs
.
roi_origin
.
send
(
self
.
roi_origin
)
self
.
Outputs
.
roi_size
.
send
(
self
.
roi_size
)
# roi = (tuple(self.origin), (self.origin[0] + self.roi_size[0], self.origin[1] + self.roi_size[1]))
# self.Outputs.dataset.send(self._widget.getDataset())
# self.Outputs.colormap.send(self._widget.getStackViewColormap())
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment