File: MaskImageTools.py

package info (click to toggle)
pymca 5.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 40,004 kB
  • ctags: 17,800
  • sloc: python: 132,302; ansic: 20,016; cpp: 827; makefile: 48; sh: 30; xml: 24
file content (265 lines) | stat: -rw-r--r-- 9,611 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#/*##########################################################################
# Copyright (C) 2004-2014 V.A. Sole, European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# 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.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__doc__ = """
    Common tools to deal with common graphics operations on images.
    """
import sys
import os
import numpy

from PyMca5 import spslut
COLORMAP_LIST = [spslut.GREYSCALE, spslut.REVERSEGREY, spslut.TEMP,
                 spslut.RED, spslut.GREEN, spslut.BLUE, spslut.MANY]

DEFAULT_COLORMAP_INDEX = 2
DEFAULT_COLORMAP_LOG_FLAG = False

def convertToRowAndColumn(x, y, shape, xScale=None, yScale=None, safe=True):
    """
    Convert from plot coordinates to image row and column.
    """
    if xScale is None:
        c = x
    else:
        if x < xScale[0]:
            x = xScale[0]
        c = (x - xScale[0]) / xScale[1]
    if yScale is None:
        r = y
    else:
        if y < yScale[0]:
            y = yScale[0]
        r = ( y - yScale[0]) / yScale[1]

    if safe:
        c = min(int(c), shape[1] - 1)
        r = min(int(r), shape[0] - 1)
    else:
        c = int(c)
        r = int(r)
    return r, c

def getPixmapFromData(ndarray, colormap=None, mask=None, colors=None):
    """
    Calculate a colormap and apply a mask (given as a set of unsigned ints) to
    it.

    :param ndarray:  Data values
    :type ndarray: Numpy array
    :param colormap: None or a list of seven parameters:

                0. Colormap index. Positive integer
                1. Autoscale flag
                2. Minimum value to be mapped
                3. Maximum value to be mapped
                4. Minimum data value
                5. Maximum data value
                6. Flag to indicate mode (0 - linear, 1 - logarithmic)

    :type colormap: list or None (default)
    :param mask: Numpy array of indices to indicating mask levels
    :type mask: Numpy nd array of indices or None (default)
    :param colors: List containing the colors associated to the mask levels
    :type colors: Numpy array of dimensions (N mask levels, 4) or None (default)
    :returns: Numpy uint8 array of shape equal data.shape + [4]
    """
    oldShape = list(ndarray.shape)

    # deal with numpy masked arrays
    if hasattr(ndarray, 'mask'):
        data = ndarray.data[:]
    else:
        data = ndarray[:]

    if len(oldShape) == 1:
        data.shape = -1, 1
    elif len(oldShape) != 2:
        raise TypeError("Input array must be of dimension 2 got %d" % \
                                len(oldShape))

    # deal with non finite data
    finiteData = numpy.isfinite(data)
    goodData = finiteData.min()

    if goodData:
        minData = data.min()
        maxData = data.max()
    else:
        tmpData = data[finiteData]
        if tmpData.size > 0:
            minData = tmpData.min()
            maxData = tmpData.max()
        else:
            minData = None
            maxData = None
        tmpData = None

    # apply colormap
    if colormap is None:
        colormapName = COLORMAP_LIST[min(DEFAULT_COLORMAP_INDEX,
                                    len(COLORMAP_LIST) - 1)]
        if DEFAULT_COLORMAP_LOG_FLAG:
            colormapScaling = spslut.log
        else:
            colormapScaling = spslut.LINEAR
        if minData is None:
            (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormapScaling, 3.0),
                            "RGBX",
                            colormapName,
                            1,
                            (0, 1),
                            (0, 255), 1)
        else:
            (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormapScaling,3.0),
                            "RGBX",
                            colormapName,
                            0,
                            (minData,maxData),
                            (0, 255), 1)
    else:
        if len(colormap) < 7:
            print("Missing colormap log flag assuming linear")
            colormap.append(spslut.LINEAR)
        if goodData:
            (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormap[6],3.0),
                            "RGBX",
                            COLORMAP_LIST[int(str(colormap[0]))],
                            colormap[1],
                            (colormap[2],colormap[3]),
                            (0, 255), 1)
        elif colormap[1]:
            #autoscale
            if minData is None:
                colormapName = COLORMAP_LIST[min(DEFAULT_COLORMAP_INDEX,
                                            len(COLORMAP_LIST) - 1)]
                colormapScaling = DEFAULT_COLORMAP_LOG_FLAG
                (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormapScaling, 3.0),
                            "RGBX",
                            colormapName,
                            1,
                            (0, 1),
                            (0, 255), 1)
            else:
                (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormap[6],3.0),
                            "RGBX",
                            COLORMAP_LIST[int(str(colormap[0]))],
                            0,
                            (minData, maxData),
                            (0,255), 1)
        else:
            (pixmap, size, minmax)= spslut.transform(\
                            data,
                            (1,0),
                            (colormap[6],3.0),
                            "RGBX",
                            COLORMAP_LIST[int(str(colormap[0]))],
                            colormap[1],
                            (colormap[2],colormap[3]),
                            (0,255), 1)

    # make sure alpha is set
    pixmap.shape = -1, 4
    pixmap[:, 3] = 255
    pixmap.shape = list(data.shape) + [4]
    if not goodData:
        pixmap[finiteData < 1] = 255
    if mask is not None:
        return applyMaskToImage(pixmap, mask, colors=colors, copy=False)
    return pixmap


def applyMaskToImage(pixmap, mask=None, colors=None, copy=True):
    """
    Calculate the resulting pixmap after applying a mask. Each value of the
    mask indicates the color index to be used.

    :param pixmap: Numpy array of RGBA values.
    :type pixmap: Numpy ndarray.
    :param mask: Numpy array of positive indices. Usually of type uint8.
    :type mask: Numpy ndarray.
    :param colors: Array of dimension (n_colors, 4) containing the RGBA colors.
    :type colors: Numpy ndarray of uint8 values.
    :param copy: Flag to indicate if a copy of th einput pixmap is to be made.
    :type copy: Boolean, default True.
    :returns: The resulting pixmap.
    """
    if copy:
        pixmap = pixmap.copy()
    if mask is None:
        return pixmap

    maxValue = mask.max()
    startIndex = mask.min()
    if colors is None:
        if maxValue == 1:
            startIndex = 1
            colors = numpy.zeros((2, 4), dtype=numpy.uint8)
            colors[1, 3] = 255
        else:
            raise ValueError("Different mask levels require color list input")
    oldShape = pixmap.shape
    pixmap.shape = -1, 4
    maskView = mask[:]
    maskView.shape = -1,
    blendFactor = 0.8
    for i in range(startIndex, maxValue + 1):
        idx = (maskView==i)
        pixmap[idx, :] = pixmap[idx, :] * blendFactor + \
                         colors[i] * (1.0 - blendFactor)
    pixmap.shape = oldShape
    return pixmap

if __name__ == "__main__":
    from PyMca5.PyMcaGui import PyMcaQt as qt
    from PyMca5.PyMcaGui import PlotWidget
    app = qt.QApplication([])
    w = PlotWidget.PlotWidget()
    data = numpy.arange(10000.).reshape(100, 100)
    mask = numpy.zeros(data.shape, dtype=numpy.uint8)
    mask[25:75, 25:75] = 1
    image = getPixmapFromData(data, mask=mask)
    w.addImage(image, mask=mask)
    w.show()
    app.exec_()