LSSTApplications  19.0.0-10-g920eed2,19.0.0-11-g48a0200+2,19.0.0-18-gfc4e62b+10,19.0.0-2-g3b2f90d+2,19.0.0-2-gd671419+5,19.0.0-20-g5a5a17ab+8,19.0.0-21-g2644856+10,19.0.0-22-gc5dc5b1+5,19.0.0-23-gdc29a50+2,19.0.0-24-g923e380+10,19.0.0-25-g6c8df7140,19.0.0-27-g567f04d+6,19.0.0-3-g2b32d65+5,19.0.0-3-g8227491+9,19.0.0-3-g9c54d0d+9,19.0.0-3-gca68e65+5,19.0.0-3-gcfc5f51+5,19.0.0-3-ge110943+8,19.0.0-3-ge74d124,19.0.0-3-gfe04aa6+10,19.0.0-4-g06f5963+5,19.0.0-4-g3d16501+10,19.0.0-4-g4a9c019+5,19.0.0-4-g5a8b323,19.0.0-4-g66397f0+1,19.0.0-4-g8278b9b+1,19.0.0-4-g8557e14,19.0.0-4-g8964aba+10,19.0.0-4-ge404a01+9,19.0.0-5-g40f3a5a,19.0.0-5-g4db63b3,19.0.0-5-gfb03ce7+10,19.0.0-6-gbaebbfb+9,19.0.0-60-gafafd468+10,19.0.0-64-gf672fef+7,19.0.0-7-g039c0b5+8,19.0.0-7-gbea9075+4,19.0.0-7-gc567de5+10,19.0.0-8-g3a3ce09+5,19.0.0-9-g463f923+9,w.2020.21
LSSTDataManagementBasePackage
Namespaces | Functions
lsst.afw.display Namespace Reference

Namespaces

 displayLib
 
 ds9
 
 ds9Regions
 
 interface
 
 rgb
 
 utils
 
 virtualDevice
 

Functions

 PYBIND11_MODULE (_simpleFits, mod)
 
 PYBIND11_MODULE (rgb, mod)
 
template<typename ImageT >
void replaceSaturatedPixels (ImageT &rim, ImageT &gim, ImageT &bim, int borderWidth=2, float saturatedPixelValue=65535)
 
template<class T >
std::pair< double, double > getZScale (image::Image< T > const &image, int const nSamples=1000, double const contrast=0.25)
 Calculate an IRAF/ds9-style zscaling. More...
 
template void replaceSaturatedPixels (image::MaskedImage< float > &rim, image::MaskedImage< float > &gim, image::MaskedImage< float > &bim, int borderWidth, float saturatedPixelValue)
 
template std::pair< double, double > getZScale (image::Image< std::uint16_t > const &image, int const nSamples, double const contrast)
 
template std::pair< double, double > getZScale (image::Image< float > const &image, int const nSamples, double const contrast)
 
template<typename ImageT >
void writeBasicFits (int fd, ImageT const &data, geom::SkyWcs const *Wcs, char const *title)
 
template<typename ImageT >
void writeBasicFits (std::string const &filename, ImageT const &data, geom::SkyWcs const *Wcs, char const *title)
 
template<typename ImageT >
void writeBasicFits (int fd, ImageT const &data, lsst::afw::geom::SkyWcs const *Wcs=NULL, char const *title=NULL)
 
template<typename ImageT >
void writeBasicFits (std::string const &filename, ImageT const &data, lsst::afw::geom::SkyWcs const *Wcs=NULL, const char *title=NULL)
 

Function Documentation

◆ getZScale() [1/3]

template std::pair<double, double> lsst::afw::display::getZScale ( image::Image< float > const &  image,
int const  nSamples,
double const  contrast 
)

◆ getZScale() [2/3]

template std::pair<double, double> lsst::afw::display::getZScale ( image::Image< std::uint16_t > const &  image,
int const  nSamples,
double const  contrast 
)

◆ getZScale() [3/3]

template<class T >
std::pair< double, double > lsst::afw::display::getZScale ( image::Image< T > const &  image,
int const  nSamples = 1000,
double const  contrast = 0.25 
)

Calculate an IRAF/ds9-style zscaling.

To quote Frank Valdes (http://iraf.net/forum/viewtopic.php?showtopic=134139)

ZSCALE ALGORITHM

The zscale algorithm is designed to display the  image  values  near
the  median  image  value  without  the  time  consuming  process of
computing a full image histogram.  This is particularly  useful  for
astronomical  images  which  generally  have a very peaked histogram
corresponding to  the  background  sky  in  direct  imaging  or  the
continuum in a two dimensional spectrum.

The  sample  of pixels, specified by values greater than zero in the
sample mask zmask or by an  image  section,  is  selected  up  to  a
maximum  of nsample pixels.  If a bad pixel mask is specified by the
bpmask parameter then any pixels with mask values which are  greater
than  zero  are not counted in the sample.  Only the first pixels up
to the limit are selected where the order is by line beginning  from
the  first line.  If no mask is specified then a grid of pixels with
even spacing along lines and columns that  make  up  a  number  less
than or equal to the maximum sample size is used.

If  a  contrast of zero is specified (or the zrange flag is used and
the image does not have a  valid  minimum/maximum  value)  then  the
minimum  and maximum of the sample is used for the intensity mapping
range.

If the contrast  is  not  zero  the  sample  pixels  are  ranked  in
brightness  to  form  the  function  I(i) where i is the rank of the
pixel and I is its value.  Generally the midpoint of  this  function
(the  median) is very near the peak of the image histogram and there
is a well defined slope about the midpoint which is related  to  the
width  of the histogram.  At the ends of the I(i) function there are
a few very bright and dark pixels due to objects and defects in  the
field.   To  determine  the  slope  a  linear  function  is fit with
iterative rejection;

<code>
        I(i) = intercept + slope * (i - midpoint)
</code>

If more than half of the points are rejected then there is  no  well
defined  slope  and  the full range of the sample defines z1 and z2.
Otherwise the endpoints of the linear function  are  used  (provided
they are within the original range of the sample):

<code>
        z1 = I(midpoint) + (slope / contrast) * (1 - midpoint)
        z2 = I(midpoint) + (slope / contrast) * (npoints - midpoint)
</code>

As  can  be  seen,  the parameter contrast may be used to adjust the
contrast produced by this algorithm.
Parameters
imageThe image we wish to stretch
nSamplesNumber of samples to use
contrastStretch parameter; see description

Definition at line 167 of file scaling.cc.

167  {
168  // extract samples
169  std::vector<T> vSample;
170  getSample(image, nSamples, vSample);
171  int nPix = vSample.size();
172 
173  if (vSample.empty()) {
174  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "ZScale: No pixel in image is finite");
175  }
176 
177  std::sort(vSample.begin(), vSample.end());
178 
179  // max, min, median
180  // N.b. you can get a median in linear time, but we need the sorted array for fitLine()
181  // If we wanted to speed this up, the best option would be to quantize
182  // the pixel values and build a histogram
183  double const zmin = vSample.front();
184  double const zmax = vSample.back();
185  int const iCenter = nPix / 2;
186  T median = (nPix & 1) ? vSample[iCenter] : (vSample[iCenter] + vSample[iCenter + 1]) / 2;
187 
188  // fit a line to the sorted sample
189  const int maxRejectionRatio = 2;
190  const int npixelsMin = 5;
191 
192  int minpix = std::max(npixelsMin, nPix / maxRejectionRatio);
193  int nGrow = std::max(1, nPix / 100);
194 
195  const double nSigmaClip = 2.5;
196  const int nIterations = 5;
197 
198  int nGoodPix = 0;
199  std::pair<double, double> ret = fitLine(&nGoodPix, vSample, nSigmaClip, nGrow, minpix, nIterations);
200 #if 0 // unused, but calculated and potentially useful
201  double const zstart = ret.first;
202 #endif
203  double const zslope = ret.second;
204 
205  double z1, z2;
206  if (nGoodPix < minpix) {
207  z1 = zmin;
208  z2 = zmax;
209  } else {
210  double const slope = zslope / contrast;
211 
212  z1 = std::max(zmin, median - iCenter * slope);
213  z2 = std::min(zmax, median + (nPix - iCenter - 1) * slope);
214  }
215 
216  return std::make_pair(z1, z2);
217 }

◆ PYBIND11_MODULE() [1/2]

lsst::afw::display::PYBIND11_MODULE ( _simpleFits  ,
mod   
)

Definition at line 53 of file _simpleFits.cc.

53  {
54  declareAll<image::Image<std::uint16_t>>(mod);
55  declareAll<image::Image<std::uint64_t>>(mod);
56  declareAll<image::Image<int>>(mod);
57  declareAll<image::Image<float>>(mod);
58  declareAll<image::Image<double>>(mod);
59  declareAll<image::Mask<std::uint16_t>>(mod);
60  declareAll<image::Mask<image::MaskPixel>>(mod);
61 }

◆ PYBIND11_MODULE() [2/2]

lsst::afw::display::PYBIND11_MODULE ( rgb  ,
mod   
)

Definition at line 37 of file rgb.cc.

37  {
38  /* Module level */
39  mod.def("replaceSaturatedPixels", replaceSaturatedPixels<lsst::afw::image::MaskedImage<float>>, "rim"_a,
40  "gim"_a, "bim"_a, "borderWidth"_a = 2, "saturatedPixelValue"_a = 65535);
41  mod.def("getZScale", getZScale<std::uint16_t>, "image"_a, "nsamples"_a = 1000, "contrast"_a = 0.25);
42  mod.def("getZScale", getZScale<float>, "image"_a, "nsamples"_a = 1000, "contrast"_a = 0.25);
43 }

◆ replaceSaturatedPixels() [1/2]

template void lsst::afw::display::replaceSaturatedPixels ( image::MaskedImage< float > &  rim,
image::MaskedImage< float > &  gim,
image::MaskedImage< float > &  bim,
int  borderWidth,
float  saturatedPixelValue 
)

◆ replaceSaturatedPixels() [2/2]

template<typename ImageT >
void lsst::afw::display::replaceSaturatedPixels ( ImageT &  rim,
ImageT &  gim,
ImageT &  bim,
int  borderWidth = 2,
float  saturatedPixelValue = 65535 
)

Definition at line 30 of file saturated.cc.

35  {
36  int const width = rim.getWidth(), height = rim.getHeight();
37  int const x0 = rim.getX0(), y0 = rim.getY0();
38 
39  if (width != gim.getWidth() || height != gim.getHeight() || x0 != gim.getX0() || y0 != gim.getY0()) {
40  throw LSST_EXCEPT(
41  pex::exceptions::InvalidParameterError,
42  str(boost::format("R image has different size/origin from G image "
43  "(%dx%d+%d+%d v. %dx%d+%d+%d") %
44  width % height % x0 % y0 % gim.getWidth() % gim.getHeight() % gim.getX0() % gim.getY0()));
45  }
46  if (width != bim.getWidth() || height != bim.getHeight() || x0 != bim.getX0() || y0 != bim.getY0()) {
47  throw LSST_EXCEPT(
48  pex::exceptions::InvalidParameterError,
49  str(boost::format("R image has different size/origin from B image "
50  "(%dx%d+%d+%d v. %dx%d+%d+%d") %
51  width % height % x0 % y0 % bim.getWidth() % bim.getHeight() % bim.getX0() % bim.getY0()));
52  }
53 
54  bool const useMaxPixel = !std::isfinite(saturatedPixelValue);
55 
56  SetPixels<typename ImageT::Image> setR, setG, setB; // functors used to set pixel values
57 
58  // Find all the saturated pixels in any of the three image
59  int const npixMin = 1; // minimum number of pixels in an object
60  afw::image::MaskPixel const SAT = rim.getMask()->getPlaneBitMask("SAT");
61  detection::Threshold const satThresh(SAT, detection::Threshold::BITMASK);
62 
63  detection::FootprintSet sat(*rim.getMask(), satThresh, npixMin);
64  sat.merge(detection::FootprintSet(*gim.getMask(), satThresh, npixMin));
65  sat.merge(detection::FootprintSet(*bim.getMask(), satThresh, npixMin));
66  // go through the list of saturated regions, determining the mean colour of the surrounding pixels
67  typedef detection::FootprintSet::FootprintList FootprintList;
68  std::shared_ptr<FootprintList> feet = sat.getFootprints();
69  for (FootprintList::const_iterator ptr = feet->begin(), end = feet->end(); ptr != end; ++ptr) {
71  auto const bigFoot = std::make_shared<detection::Footprint>(foot->getSpans()->dilated(borderWidth),
72  foot->getRegion());
73 
74  double sumR = 0, sumG = 0, sumB = 0; // sum of all non-saturated adjoining pixels
75  double maxR = 0, maxG = 0, maxB = 0; // maximum of non-saturated adjoining pixels
76 
77  for (auto span = bigFoot->getSpans()->begin(), send = bigFoot->getSpans()->end(); span != send;
78  ++span) {
79  int const y = span->getY() - y0;
80  if (y < 0 || y >= height) {
81  continue;
82  }
83  int sx0 = span->getX0() - x0;
84  if (sx0 < 0) {
85  sx0 = 0;
86  }
87  int sx1 = span->getX1() - x0;
88  if (sx1 >= width) {
89  sx1 = width - 1;
90  }
91 
92  for (typename ImageT::iterator rptr = rim.at(sx0, y), rend = rim.at(sx1 + 1, y),
93  gptr = gim.at(sx0, y), bptr = bim.at(sx0, y);
94  rptr != rend; ++rptr, ++gptr, ++bptr) {
95  if (!((rptr.mask() | gptr.mask() | bptr.mask()) & SAT)) {
96  float val = rptr.image();
97  sumR += val;
98  if (val > maxR) {
99  maxR = val;
100  }
101 
102  val = gptr.image();
103  sumG += val;
104  if (val > maxG) {
105  maxG = val;
106  }
107 
108  val = bptr.image();
109  sumB += val;
110  if (val > maxB) {
111  maxB = val;
112  }
113  }
114  }
115  }
116  // OK, we have the mean fluxes for the pixels surrounding this set of saturated pixels
117  // so we can figure out the proper values to use for the saturated ones
118  float R = 0, G = 0, B = 0; // mean intensities
119  if (sumR + sumB + sumG > 0) {
120  if (sumR > sumG) {
121  if (sumR > sumB) {
122  R = useMaxPixel ? maxR : saturatedPixelValue;
123 
124  G = (R * sumG) / sumR;
125  B = (R * sumB) / sumR;
126  } else {
127  B = useMaxPixel ? maxB : saturatedPixelValue;
128  R = (B * sumR) / sumB;
129  G = (B * sumG) / sumB;
130  }
131  } else {
132  if (sumG > sumB) {
133  G = useMaxPixel ? maxG : saturatedPixelValue;
134  R = (G * sumR) / sumG;
135  B = (G * sumB) / sumG;
136  } else {
137  B = useMaxPixel ? maxB : saturatedPixelValue;
138  R = (B * sumR) / sumB;
139  G = (B * sumG) / sumB;
140  }
141  }
142  }
143  // Now that we know R, G, and B we can fix the values
144  setR.setValue(R);
145  foot->getSpans()->applyFunctor(setR, *rim.getImage());
146  setG.setValue(G);
147  foot->getSpans()->applyFunctor(setG, *gim.getImage());
148  setB.setValue(B);
149  foot->getSpans()->applyFunctor(setB, *bim.getImage());
150  }
151 }

◆ writeBasicFits() [1/4]

template<typename ImageT >
void lsst::afw::display::writeBasicFits ( int  fd,
ImageT const &  data,
geom::SkyWcs const *  Wcs,
char const *  title 
)

Definition at line 363 of file simpleFits.cc.

367  {
368  /*
369  * Allocate cards for FITS headers
370  */
371  std::list<Card> cards;
372  /*
373  * What sort if image is it?
374  */
375  int bitpix = lsst::afw::fits::getBitPix<typename ImageT::Pixel>();
376  if (bitpix == 20) { // cfitsio for "Unsigned short"
377  cards.push_back(Card("BZERO", 32768.0, ""));
378  cards.push_back(Card("BSCALE", 1.0, ""));
379  bitpix = 16;
380  } else if (bitpix == 0) {
381  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unsupported image type");
382  }
383  /*
384  * Generate WcsA, pixel coordinates, allowing for X0 and Y0
385  */
386  addWcs("A", cards, data.getX0(), data.getY0());
387  /*
388  * Now WcsB, so that pixel (0,0) is correctly labelled (but ignoring XY0)
389  */
390  addWcs("B", cards);
391 
392  if (title) {
393  cards.push_back(Card("OBJECT", title, "Image being displayed"));
394  }
395  /*
396  * Was there something else?
397  */
398  if (Wcs == NULL) {
399  addWcs("", cards); // works around a ds9 bug that WCSA/B is ignored if no Wcs is present
400  } else {
401  typedef std::vector<std::string> NameList;
402 
403  auto shift = lsst::geom::Extent2D(-data.getX0(), -data.getY0());
404  auto newWcs = Wcs->copyAtShiftedPixelOrigin(shift);
405 
406  std::shared_ptr<lsst::daf::base::PropertySet> metadata = newWcs->getFitsMetadata();
407 
408  NameList paramNames = metadata->paramNames();
409 
410  for (NameList::const_iterator i = paramNames.begin(), end = paramNames.end(); i != end; ++i) {
411  if (*i == "SIMPLE" || *i == "BITPIX" || *i == "NAXIS" || *i == "NAXIS1" || *i == "NAXIS2" ||
412  *i == "XTENSION" || *i == "PCOUNT" || *i == "GCOUNT") {
413  continue;
414  }
415  std::type_info const &type = metadata->typeOf(*i);
416  if (type == typeid(bool)) {
417  cards.push_back(Card(*i, metadata->get<bool>(*i)));
418  } else if (type == typeid(int)) {
419  cards.push_back(Card(*i, metadata->get<int>(*i)));
420  } else if (type == typeid(float)) {
421  cards.push_back(Card(*i, metadata->get<float>(*i)));
422  } else if (type == typeid(double)) {
423  cards.push_back(Card(*i, metadata->get<double>(*i)));
424  } else {
425  cards.push_back(Card(*i, metadata->get<std::string>(*i)));
426  }
427  }
428  }
429  /*
430  * Basic FITS stuff
431  */
432  const int naxis = 2; // == NAXIS
433  int naxes[naxis]; /* values of NAXIS1 etc */
434  naxes[0] = data.getWidth();
435  naxes[1] = data.getHeight();
436 
437  write_fits_hdr(fd, bitpix, naxis, naxes, cards, 1);
438  for (int y = 0; y != data.getHeight(); ++y) {
439  if (write_fits_data(fd, bitpix, (char *)(data.row_begin(y)), (char *)(data.row_end(y))) < 0) {
441  (boost::format("Error writing data for row %d") % y).str());
442  }
443  }
444 
445  pad_to_fits_record(fd, data.getWidth() * data.getHeight(), bitpix);
446 }

◆ writeBasicFits() [2/4]

template<typename ImageT >
void lsst::afw::display::writeBasicFits ( int  fd,
ImageT const &  data,
lsst::afw::geom::SkyWcs const *  Wcs = NULL,
char const *  title = NULL 
)

◆ writeBasicFits() [3/4]

template<typename ImageT >
void lsst::afw::display::writeBasicFits ( std::string const &  filename,
ImageT const &  data,
geom::SkyWcs const *  Wcs,
char const *  title 
)

Definition at line 449 of file simpleFits.cc.

453  {
454  int fd;
455  if ((filename.c_str())[0] == '|') { // a command
456  const char *cmd = filename.c_str() + 1;
457  while (isspace(*cmd)) {
458  cmd++;
459  }
460 
461  fd = fileno(popen(cmd, "w"));
462  } else {
463  fd = creat(filename.c_str(), 777);
464  }
465 
466  if (fd < 0) {
468  (boost::format("Cannot open \"%s\"") % filename).str());
469  }
470 
471  try {
472  writeBasicFits(fd, data, Wcs, title);
474  (void)close(fd);
475  throw;
476  }
477 
478  (void)close(fd);
479 }

◆ writeBasicFits() [4/4]

template<typename ImageT >
void lsst::afw::display::writeBasicFits ( std::string const &  filename,
ImageT const &  data,
lsst::afw::geom::SkyWcs const *  Wcs = NULL,
const char *  title = NULL 
)
y
int y
Definition: SpanSet.cc:49
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
std::string
STL class.
std::shared_ptr
STL class.
std::list
STL class.
std::pair< double, double >
std::vector
STL class.
std::vector::size
T size(T... args)
std::type_info
pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw.display::writeBasicFits
void writeBasicFits(std::string const &filename, ImageT const &data, geom::SkyWcs const *Wcs, char const *title)
Definition: simpleFits.cc:449
std::vector::back
T back(T... args)
val
ImageT val
Definition: CR.cc:146
std::vector::front
T front(T... args)
std::sort
T sort(T... args)
end
int end
Definition: BoundedField.cc:105
std::list::push_back
T push_back(T... args)
data
char * data
Definition: BaseRecord.cc:62
lsst::afw::image::MaskedImage
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
std::isfinite
T isfinite(T... args)
ptr
uint64_t * ptr
Definition: RangeSet.cc:88
lsst::afw::image::MaskPixel
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
Definition: LsstImageTypes.h:34
cmd
Definition: __init__.py:1
lsst::daf::base::PropertySet::paramNames
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::min
T min(T... args)
std::vector::begin
T begin(T... args)
type
table::Key< int > type
Definition: Detector.cc:163
lsst::afw.display::replaceSaturatedPixels
void replaceSaturatedPixels(ImageT &rim, ImageT &gim, ImageT &bim, int borderWidth=2, float saturatedPixelValue=65535)
Definition: saturated.cc:30
std::isspace
T isspace(T... args)
lsst::daf::base::PropertySet::typeOf
std::type_info const & typeOf(std::string const &name) const
Get the type of values for a property name (possibly hierarchical).
lsst::geom::Extent2D
Extent< double, 2 > Extent2D
Definition: Extent.h:400
std::vector::empty
T empty(T... args)
std::make_pair
T make_pair(T... args)
lsst::pex::exceptions::Exception
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
std::vector::end
T end(T... args)
lsst::daf::base::PropertySet::get
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
std::max
T max(T... args)
lsst::pex::exceptions::RuntimeError
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104