summaryrefslogtreecommitdiff
path: root/silx/gui/plot/actions/histogram.py
blob: a4a91e9c06d46cf78a6818ebbbca419eab535c84 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-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.
#
# ###########################################################################*/
"""
:mod:`silx.gui.plot.actions.histogram` provides actions relative to histograms
for :class:`.PlotWidget`.

The following QAction are available:

- :class:`PixelIntensitiesHistoAction`
"""

from __future__ import division

__authors__ = ["V.A. Sole", "T. Vincent", "P. Knobel"]
__date__ = "27/06/2017"
__license__ = "MIT"

from . import PlotAction
from silx.math.histogram import Histogramnd
import numpy
import logging
from silx.gui import qt

_logger = logging.getLogger(__name__)


class PixelIntensitiesHistoAction(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='pixel-intensities',
                            text='pixels intensity',
                            tooltip='Compute image intensity distribution',
                            triggered=self._triggered,
                            parent=parent,
                            checkable=True)
        self._plotHistogram = None
        self._connectedToActiveImage = False
        self._histo = None

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

        :param bool checked: status  of the action button
        """
        if checked:
            if not self._connectedToActiveImage:
                self.plot.sigActiveImageChanged.connect(
                    self._activeImageChanged)
                self._connectedToActiveImage = True
                self.computeIntensityDistribution()

            self.getHistogramPlotWidget().show()

        else:
            if self._connectedToActiveImage:
                self.plot.sigActiveImageChanged.disconnect(
                    self._activeImageChanged)
                self._connectedToActiveImage = False

            self.getHistogramPlotWidget().hide()

    def _activeImageChanged(self, previous, legend):
        """Handle active image change: toggle enabled toolbar, update curve"""
        if self.isChecked():
            self.computeIntensityDistribution()

    def computeIntensityDistribution(self):
        """Get the active image and compute the image intensity distribution
        """
        activeImage = self.plot.getActiveImage()

        if activeImage is not None:
            image = activeImage.getData(copy=False)
            if image.ndim == 3:  # RGB(A) images
                _logger.info('Converting current image from RGB(A) to grayscale\
                    in order to compute the intensity distribution')
                image = (image[:, :, 0] * 0.299 +
                         image[:, :, 1] * 0.587 +
                         image[:, :, 2] * 0.114)

            xmin = numpy.nanmin(image)
            xmax = numpy.nanmax(image)
            nbins = min(1024, int(numpy.sqrt(image.size)))
            data_range = xmin, xmax

            # bad hack: get 256 bins in the case we have a B&W
            if numpy.issubdtype(image.dtype, numpy.integer):
                if nbins > xmax - xmin:
                    nbins = xmax - xmin

            nbins = max(2, nbins)

            data = image.ravel().astype(numpy.float32)
            histogram = Histogramnd(data, n_bins=nbins, histo_range=data_range)
            assert len(histogram.edges) == 1
            self._histo = histogram.histo
            edges = histogram.edges[0]
            plot = self.getHistogramPlotWidget()
            plot.addHistogram(histogram=self._histo,
                              edges=edges,
                              legend='pixel intensity',
                              fill=True,
                              color='red')
            plot.resetZoom()

    def eventFilter(self, qobject, event):
        """Observe when the close event is emitted then
        simply uncheck the action button

        :param qobject: the object observe
        :param event: the event received by qobject
        """
        if event.type() == qt.QEvent.Close:
            if self._plotHistogram is not None:
                self._plotHistogram.hide()
            self.setChecked(False)

        return PlotAction.eventFilter(self, qobject, event)

    def getHistogramPlotWidget(self):
        """Create the plot histogram if needed, otherwise create it

        :return: the PlotWidget showing the histogram of the pixel intensities
        """
        from silx.gui.plot.PlotWindow import Plot1D
        if self._plotHistogram is None:
            self._plotHistogram = Plot1D(parent=self.plot)
            self._plotHistogram.setWindowFlags(qt.Qt.Window)
            self._plotHistogram.setWindowTitle('Image Intensity Histogram')
            self._plotHistogram.installEventFilter(self)
            self._plotHistogram.getXAxis().setLabel("Value")
            self._plotHistogram.getYAxis().setLabel("Count")

        return self._plotHistogram

    def getHistogram(self):
        """Return the last computed histogram

        :return: the histogram displayed in the HistogramPlotWiget
        """
        return self._histo