summaryrefslogtreecommitdiff
path: root/silx/gui/plot/actions/medfilt.py
blob: 4284a8bdf44cbece0c2b0bb9cfc6eea2fc0a4ef6 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-2018 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.
#
# ###########################################################################*/
"""
:mod:`silx.gui.plot.actions.medfilt` provides a set of QAction to apply filter
on data contained in a :class:`.PlotWidget`.

The following QAction are available:

- :class:`MedianFilterAction`
- :class:`MedianFilter1DAction`
- :class:`MedianFilter2DAction`

"""

from __future__ import division

__authors__ = ["V.A. Sole", "T. Vincent", "P. Knobel"]
__license__ = "MIT"

__date__ = "03/01/2018"

from . import PlotAction
from silx.gui.widgets.MedianFilterDialog import MedianFilterDialog
from silx.math.medianfilter import medfilt2d
import logging

_logger = logging.getLogger(__name__)


class MedianFilterAction(PlotAction):
    """QAction to plot the pixels intensities diagram

    :param plot: :class:`.PlotWidget` instance on which to operate
    :param parent: See :class:`QAction`
    """

    def __init__(self, plot, parent=None):
        PlotAction.__init__(self,
                            plot,
                            icon='median-filter',
                            text='median filter',
                            tooltip='Apply a median filter on the image',
                            triggered=self._triggered,
                            parent=parent)
        self._originalImage = None
        self._legend = None
        self._filteredImage = None
        self._popup = MedianFilterDialog(parent=plot)
        self._popup.sigFilterOptChanged.connect(self._updateFilter)
        self.plot.sigActiveImageChanged.connect(self._updateActiveImage)
        self._updateActiveImage()

    def _triggered(self, checked):
        """Update the plot of the histogram visibility status

        :param bool checked: status  of the action button
        """
        self._popup.show()

    def _updateActiveImage(self):
        """Set _activeImageLegend and _originalImage from the active image"""
        self._activeImageLegend = self.plot.getActiveImage(just_legend=True)
        if self._activeImageLegend is None:
            self._originalImage = None
            self._legend = None
        else:
            self._originalImage = self.plot.getImage(self._activeImageLegend).getData(copy=False)
            self._legend = self.plot.getImage(self._activeImageLegend).getLegend()

    def _updateFilter(self, kernelWidth, conditional=False):
        if self._originalImage is None:
            return

        self.plot.sigActiveImageChanged.disconnect(self._updateActiveImage)
        filteredImage = self._computeFilteredImage(kernelWidth, conditional)
        self.plot.addImage(data=filteredImage,
                           legend=self._legend,
                           replace=True)
        self.plot.sigActiveImageChanged.connect(self._updateActiveImage)

    def _computeFilteredImage(self, kernelWidth, conditional):
        raise NotImplementedError('MedianFilterAction is a an abstract class')

    def getFilteredImage(self):
        """
        :return: the image with the median filter apply on"""
        return self._filteredImage


class MedianFilter1DAction(MedianFilterAction):
    """Define the MedianFilterAction for 1D

    :param plot: :class:`.PlotWidget` instance on which to operate
    :param parent: See :class:`QAction`
    """
    def __init__(self, plot, parent=None):
        MedianFilterAction.__init__(self,
                                    plot,
                                    parent=parent)

    def _computeFilteredImage(self, kernelWidth, conditional):
        assert(self.plot is not None)
        return medfilt2d(self._originalImage,
                         (kernelWidth, 1),
                         conditional)


class MedianFilter2DAction(MedianFilterAction):
    """Define the MedianFilterAction for 2D

    :param plot: :class:`.PlotWidget` instance on which to operate
    :param parent: See :class:`QAction`
    """
    def __init__(self, plot, parent=None):
        MedianFilterAction.__init__(self,
                                    plot,
                                    parent=parent)

    def _computeFilteredImage(self, kernelWidth, conditional):
        assert(self.plot is not None)
        return medfilt2d(self._originalImage,
                         (kernelWidth, kernelWidth),
                         conditional)