Coverage for python/lsst/afw/display/rgb.py : 77%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
# # LSST Data Management System # Copyright 2015-2016 LSST/AURA # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <https://www.lsstcorp.org/LegalNotices/>. #
"""!Return a naive total intensity from the red, blue, and green intensities @param imageR intensity of image that'll be mapped to red; or intensity if imageG and imageB are None @param imageG intensity of image that'll be mapped to green; or None @param imageB intensity of image that'll be mapped to blue; or None
Inputs may be MaskedImages, Images, or numpy arrays and the return is of the same type """ "Please specify either a single image or red, green, and blue images"
c = imageRGB[i] = c.getImage()
# # Repack into whatever type was passed to us #
intensity = afwImage.makeMaskedImage(Image(intensity))
"""!Baseclass to map red, blue, green intensities into uint8 values"""
"""!Create a mapping @param minimum Intensity that should be mapped to black (a scalar or array for R, G, B) @param image The image to be used to calculate the mapping.
If provided, also the default for makeRgbImage() """
xSize=None, ySize=None, rescaleFactor=None): """!Convert 3 arrays, imageR, imageG, and imageB into a numpy RGB image @param imageR Image to map to red (if None, use the image passed to the ctor) @param imageG Image to map to green (if None, use imageR) @param imageB Image to map to blue (if None, use imageR) @param xSize Desired width of RGB image (or None). If ySize is None, preserve aspect ratio @param ySize Desired height of RGB image (or None) @param rescaleFactor Make size of output image rescaleFactor*size of the input image (or None)
N.b. images may be afwImage.Images or numpy arrays """ raise RuntimeError( "You must provide an image (or pass one to the constructor)")
assert rescaleFactor is None, "You may not specify a size and rescaleFactor" h, w = imageRGB[0].shape if ySize is None: ySize = int(xSize*h/float(w) + 0.5) elif xSize is None: xSize = int(ySize*w/float(h) + 0.5)
size = (ySize, xSize) # n.b. y, x order for scipy size = float(rescaleFactor) # an int is intepreted as a percentage else:
try: import scipy.misc except ImportError as e: raise RuntimeError( "Unable to rescale as scipy.misc is unavailable: %s" % e)
for i, im in enumerate(imageRGB): imageRGB[i] = scipy.misc.imresize( im, size, interp='bilinear', mode='F')
"""!Return the total intensity from the red, blue, and green intensities
This is a naive computation, and may be overridden by subclasses """
"""Map an intensity into the range of a uint8, [0, 255] (but not converted to uint8)""" with np.errstate(invalid='ignore', divide='ignore'): # n.b. np.where can't and doesn't short-circuit return np.where(intensity <= 0, 0, np.where(intensity < self._uint8Max, intensity, self._uint8Max))
"""Use the mapping to convert images imageR, imageG, and imageB to a triplet of uint8 images"""
# individual bands can still be < 0, even if fac isn't
# copies -- could work row by row to minimise memory usage
# n.b. np.where can't and doesn't short-circuit np.where(r0 > b0, np.where(r0 >= pixmax, c*pixmax/r0, c), np.where(b0 >= pixmax, c*pixmax/b0, c)), np.where(g0 > b0, np.where(g0 >= pixmax, c*pixmax/g0, c), np.where(b0 >= pixmax, c*pixmax/b0, c))).astype(np.uint8)
"""!A linear map map of red, blue, green intensities into uint8 values"""
"""!A linear stretch from [minimum, maximum]; if one or both are omitted use image minimum/maximum to set them
@param minimum Intensity that should be mapped to black (a scalar or array for R, G, B) @param maximum Intensity that should be mapped to white (a scalar) @param image Image to estimate minimum/maximum if not explicitly set """
self._range = None else:
"""Return an array which, when multiplied by an image, returns that image mapped to the range of a uint8, [0, 255] (but not converted to uint8)
The intensity is assumed to have had minimum subtracted (as that can be done per-band) """ np.where(intensity >= self._range, self._uint8Max/intensity, self._uint8Max/self._range))
"""!A mapping for a linear stretch chosen by the zscale algorithm (preserving colours independent of brightness)
x = (I - minimum)/range """
"""!A linear stretch from [z1, z2] chosen by the zscale algorithm @param image Image whose parameters are desired @param nSamples The number of samples to use to estimate the zscale parameters @param contrast The number of samples to use to estimate the zscale parameters """
"""!A mapping for an asinh stretch (preserving colours independent of brightness)
x = asinh(Q (I - minimum)/range)/Q
This reduces to a linear stretch if Q == 0
See http://adsabs.harvard.edu/abs/2004PASP..116..133L """
# 32bit floating point machine epsilon; sys.float_info.epsilon is 64bit Q = 0.1 else: Q = Qmax
if False: self._slope = self._uint8Max/Q # gradient at origin is self._slope else:
"""Return an array which, when multiplied by an image, returns that image mapped to the range of a uint8, [0, 255] (but not converted to uint8)
The intensity is assumed to have had minimum subtracted (as that can be done per-band) """
"""!A mapping for an asinh stretch, estimating the linear stretch by zscale
x = asinh(Q (I - z1)/(z2 - z1))/Q
See AsinhMapping """
"""! Create an asinh mapping from an image, setting the linear part of the stretch using zscale
@param image The image to analyse, or a list of 3 images to be converted to an intensity image @param Q The asinh softening parameter @param pedestal The value, or array of 3 values, to subtract from the images; or None
N.b. pedestal, if not None, is removed from the images when calculating the zscale stretch, and added back into Mapping.minimum[] """
1, 3,), "Please provide 1 or 3 pedestals" except TypeError: pedestal = 3*[pedestal]
im = im.getImage()
else:
# zscale.minimum is always a triple
saturatedBorderWidth=0, saturatedPixelValue=None, xSize=None, ySize=None, rescaleFactor=None): """Make a set of three images into an RGB image using an asinh stretch and optionally write it to disk
If saturatedBorderWidth is non-zero, replace saturated pixels with saturatedPixelValue. Note that replacing saturated pixels requires that the input images be MaskedImages. """ imageG = imageR imageB = imageR
raise ValueError( "saturatedPixelValue must be set if saturatedBorderWidth is set") saturatedBorderWidth, saturatedPixelValue)
xSize=xSize, ySize=ySize, rescaleFactor=rescaleFactor)
"""!Display an rgb image using matplotlib @param rgb The RGB image in question @param show If true, call plt.show() """ import matplotlib.pyplot as plt plt.imshow(rgb, interpolation='nearest', origin="lower") if show: plt.show() return plt
"""!Write an RGB image to disk @param fileName The output file. The suffix defines the format, and must be supported by matplotlib @param rgbImage The image, as made by e.g. makeRGB
Most versions of matplotlib support png and pdf (although the eps/pdf/svg writers may be buggy, possibly due an interaction with useTeX=True in the matplotlib settings).
If your matplotlib bundles pil/pillow you should also be able to write jpeg and tiff files. """
# # Support the legacy API #
"""!@deprecated Object used to support legacy API"""
"""!@deprecated Object used to support legacy API"""
"""!@deprecated Legacy API"""
"""!@deprecated Legacy API"""
"""!@deprecated Legacy API""" |