lsst.afw  15.0-29-ga12a2b06e
Stack.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Provide functions to stack images
27  *
28  */
29 #include <vector>
30 #include <cassert>
31 #include <memory>
32 
33 #include "lsst/base.h"
34 #include "lsst/pex/exceptions.h"
35 #include "lsst/afw/math/Stack.h"
37 
39 
40 namespace lsst {
41 namespace afw {
42 namespace math {
43 
44 namespace {
45 typedef std::vector<WeightPixel> WeightVector; // vector of weights (yes, really)
49 int bitcount(unsigned int x) {
50  int b;
51  for (b = 0; x != 0; x >>= 1) {
52  if (x & 01) {
53  b++;
54  }
55  }
56  return b;
57 }
58 
62 void checkOnlyOneFlag(unsigned int flags) {
63  if (bitcount(flags & ~ERRORS) != 1) {
65  "Requested more than one type of statistic to make the image stack.");
66  }
67 }
68 
72 template <typename ObjectVectorT, typename WeightVectorT>
73 void checkObjectsAndWeights(ObjectVectorT const &objects, WeightVectorT const &wvector) {
74  if (objects.size() == 0) {
75  throw LSST_EXCEPT(pexExcept::LengthError, "Please specify at least one object to stack");
76  }
77 
78  if (!wvector.empty() && wvector.size() != objects.size()) {
80  str(boost::format("Weight vector has different length "
81  "from number of objects to be stacked: %d v. %d") %
82  wvector.size() % objects.size()));
83  }
84 }
85 
86 template <typename ImageT>
87 void checkImageSizes(ImageT const &out, std::vector<std::shared_ptr<ImageT>> const &images) {
88  geom::Extent2I const &dim = out.getDimensions();
89  for (unsigned int i = 0; i < images.size(); ++i) {
90  if (images[i]->getDimensions() != dim) {
92  (boost::format("Bad dimensions for image %d: %dx%d vs %dx%d") % i %
93  images[i]->getDimensions().getX() % images[i]->getDimensions().getY() %
94  dim.getX() % dim.getY())
95  .str());
96  }
97  }
98 }
99 
100 /* ************************************************************************** *
101  *
102  * stack MaskedImages
103  *
104  * ************************************************************************** */
105 
107 
115 template <typename PixelT, bool isWeighted, bool useVariance>
116 void computeMaskedImageStack(image::MaskedImage<PixelT> &imgStack,
118  Property flags, StatisticsControl const &sctrl,
119  image::MaskPixel const clipped,
121  WeightVector const &wvector = WeightVector()) {
122 
123 
124  // get a list of row_begin iterators
125  typedef typename image::MaskedImage<PixelT>::x_iterator x_iterator;
127  rows.reserve(images.size());
128 
129  MaskedVector<PixelT> pixelSet(images.size()); // a pixel from x,y for each image
130  WeightVector weights; // weights; non-const version
131  //
132  StatisticsControl sctrlTmp(sctrl);
133 
134  if (useVariance) { // weight using the variance image
135  assert(isWeighted);
136  assert(wvector.empty());
137 
138  weights.resize(images.size());
139 
140  sctrlTmp.setWeighted(true);
141  } else if (isWeighted) {
142  weights.assign(wvector.begin(), wvector.end());
143 
144  sctrlTmp.setWeighted(true);
145  }
146  assert(weights.empty() || weights.size() == images.size());
147 
148  // loop over x,y ... the loop over the stack to fill pixelSet
149  // - get the stats on pixelSet and put the value in the output image at x,y
150  for (int y = 0; y != imgStack.getHeight(); ++y) {
151  for (unsigned int i = 0; i < images.size(); ++i) {
152  x_iterator ptr = images[i]->row_begin(y);
153  if (y == 0) {
154  rows.push_back(ptr);
155  } else {
156  rows[i] = ptr;
157  }
158  }
159 
160  for (x_iterator ptr = imgStack.row_begin(y), end = imgStack.row_end(y); ptr != end; ++ptr) {
161  typename MaskedVector<PixelT>::iterator psPtr = pixelSet.begin();
162  WeightVector::iterator wtPtr = weights.begin();
163  for (unsigned int i = 0; i < images.size(); ++rows[i], ++i, ++psPtr, ++wtPtr) {
164  *psPtr = *rows[i];
165  if (useVariance) { // we're weighting using the variance
166  *wtPtr = 1.0 / rows[i].variance();
167  }
168  }
169 
170  Property const eflags = static_cast<Property>(flags | NPOINT | ERRORS | NCLIPPED | NMASKED);
171  Statistics stat = isWeighted ? makeStatistics(pixelSet, weights, eflags, sctrlTmp)
172  : makeStatistics(pixelSet, eflags, sctrlTmp);
173 
174  PixelT variance = ::pow(stat.getError(flags), 2);
175  image::MaskPixel msk(stat.getOrMask());
176  int const npoint = stat.getValue(NPOINT);
177  if (npoint == 0) {
178  msk = sctrlTmp.getNoGoodPixelsMask();
179  } else if (npoint == 1) {
180  /*
181  * you should be using sctrl.setCalcErrorFromInputVariance(true) if you want to avoid
182  * getting a variance of NaN when you only have one input
183  */
184  }
185  // Check to see if any pixels were rejected due to clipping
186  if (stat.getValue(NCLIPPED) > 0) {
187  msk |= clipped;
188  }
189  // Check to see if any pixels were rejected by masking, and apply
190  // any associated masks to the result.
191  if (stat.getValue(NMASKED) > 0) {
192  for (auto const & pair : maskMap) {
193  for (auto pp = pixelSet.begin(); pp != pixelSet.end(); ++pp) {
194  if ((*pp).mask() & pair.first) {
195  msk |= pair.second;
196  break;
197  }
198  }
199  }
200  }
201 
202  *ptr = typename image::MaskedImage<PixelT>::Pixel(stat.getValue(flags), msk, variance);
203  }
204  }
205 }
206 template <typename PixelT, bool isWeighted, bool useVariance>
207 void computeMaskedImageStack(image::MaskedImage<PixelT> &imgStack,
209  Property flags, StatisticsControl const &sctrl,
210  image::MaskPixel const clipped,
211  image::MaskPixel const excuse,
212  WeightVector const &wvector = WeightVector()) {
214  maskMap.push_back(std::make_pair(sctrl.getAndMask() & ~excuse, clipped));
215  computeMaskedImageStack<PixelT, isWeighted, useVariance>(
216  imgStack, images, flags, sctrl, clipped, maskMap, wvector
217  );
218 }
220 
221 } // end anonymous namespace
222 
223 template <typename PixelT>
226  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel clipped,
227  image::MaskPixel excuse) {
228  if (images.size() == 0) {
229  throw LSST_EXCEPT(pexExcept::LengthError, "Please specify at least one image to stack");
230  }
232  new image::MaskedImage<PixelT>(images[0]->getDimensions()));
233  statisticsStack(*out, images, flags, sctrl, wvector, clipped, excuse);
234  return out;
235 }
236 
237 template <typename PixelT>
240  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel clipped,
242  if (images.size() == 0) {
243  throw LSST_EXCEPT(pexExcept::LengthError, "Please specify at least one image to stack");
244  }
246  new image::MaskedImage<PixelT>(images[0]->getDimensions()));
247  statisticsStack(*out, images, flags, sctrl, wvector, clipped, maskMap);
248  return out;
249 }
250 
251 template <typename PixelT>
254  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel clipped,
255  image::MaskPixel excuse) {
256  checkObjectsAndWeights(images, wvector);
257  checkOnlyOneFlag(flags);
258  checkImageSizes(out, images);
259 
260  if (sctrl.getWeighted()) {
261  if (wvector.empty()) {
262  return computeMaskedImageStack<PixelT, true, true>(out, images, flags, sctrl,
263  clipped, excuse); // use variance
264  } else {
265  return computeMaskedImageStack<PixelT, true, false>(out, images, flags, sctrl, clipped, excuse,
266  wvector); // use wvector
267  }
268  } else {
269  return computeMaskedImageStack<PixelT, false, false>(out, images, flags, sctrl, clipped, excuse);
270  }
271 }
272 
273 template <typename PixelT>
276  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel clipped,
278  checkObjectsAndWeights(images, wvector);
279  checkOnlyOneFlag(flags);
280  checkImageSizes(out, images);
281 
282  if (sctrl.getWeighted()) {
283  if (wvector.empty()) {
284  return computeMaskedImageStack<PixelT, true, true>(out, images, flags, sctrl,
285  clipped, maskMap); // use variance
286  } else {
287  return computeMaskedImageStack<PixelT, true, false>(out, images, flags, sctrl, clipped, maskMap,
288  wvector); // use wvector
289  }
290  } else {
291  return computeMaskedImageStack<PixelT, false, false>(out, images, flags, sctrl, clipped, maskMap);
292  }
293 }
294 
295 namespace {
296 /* ************************************************************************** *
297  *
298  * stack Images
299  *
300  * All the work is done in the function comuteImageStack.
301  * A boolean template variable has been used to allow the compiler to generate the different instantiations
302  * to handle cases when we are, or are not, weighting
303  *
304  * ************************************************************************** */
311 template <typename PixelT, bool isWeighted>
312 void computeImageStack(image::Image<PixelT> &imgStack,
314  StatisticsControl const &sctrl, WeightVector const &weights = WeightVector()) {
315  MaskedVector<PixelT> pixelSet(images.size()); // a pixel from x,y for each image
316  StatisticsControl sctrlTmp(sctrl);
317 
318  // set the mask to be an infinite iterator
320 
321  if (!weights.empty()) {
322  sctrlTmp.setWeighted(true);
323  }
324 
325  // get the desired statistic
326  for (int y = 0; y != imgStack.getHeight(); ++y) {
327  for (int x = 0; x != imgStack.getWidth(); ++x) {
328  for (unsigned int i = 0; i != images.size(); ++i) {
329  (*pixelSet.getImage())(i, 0) = (*images[i])(x, y);
330  }
331 
332  if (isWeighted) {
333  imgStack(x, y) = makeStatistics(pixelSet, weights, flags, sctrlTmp).getValue();
334  } else {
335  imgStack(x, y) = makeStatistics(pixelSet, weights, flags, sctrlTmp).getValue();
336  }
337  }
338  }
339 }
340 
341 } // end anonymous namespace
342 
343 template <typename PixelT>
346  StatisticsControl const &sctrl, WeightVector const &wvector) {
347  if (images.size() == 0) {
348  throw LSST_EXCEPT(pexExcept::LengthError, "Please specify at least one image to stack");
349  }
350  std::shared_ptr<image::Image<PixelT>> out(new image::Image<PixelT>(images[0]->getDimensions()));
351  statisticsStack(*out, images, flags, sctrl, wvector);
352  return out;
353 }
354 
355 template <typename PixelT>
357  Property flags, StatisticsControl const &sctrl, WeightVector const &wvector) {
358  checkObjectsAndWeights(images, wvector);
359  checkOnlyOneFlag(flags);
360  checkImageSizes(out, images);
361 
362  if (wvector.empty()) {
363  return computeImageStack<PixelT, false>(out, images, flags, sctrl);
364  } else {
365  return computeImageStack<PixelT, true>(out, images, flags, sctrl, wvector);
366  }
367 }
368 
369 /* ************************************************************************** *
370  *
371  * stack VECTORS
372  *
373  * ************************************************************************** */
374 
375 namespace {
376 
383 template <typename PixelT, bool isWeighted>
384 std::shared_ptr<std::vector<PixelT>> computeVectorStack(
386  StatisticsControl const &sctrl, WeightVector const &wvector = WeightVector()) {
387  // create the image to be returned
388  typedef std::vector<PixelT> Vect;
389  std::shared_ptr<Vect> vecStack(new Vect(vectors[0]->size(), 0.0));
390 
391  MaskedVector<PixelT> pixelSet(vectors.size()); // values from a given pixel of each image
392 
393  StatisticsControl sctrlTmp(sctrl);
394  // set the mask to be an infinite iterator
396 
397  if (!wvector.empty()) {
398  sctrlTmp.setWeighted(true);
399  }
400 
401  // collect elements from the stack into the MaskedVector to do stats
402  for (unsigned int x = 0; x < vectors[0]->size(); ++x) {
403  typename MaskedVector<PixelT>::iterator psPtr = pixelSet.begin();
404  for (unsigned int i = 0; i < vectors.size(); ++i, ++psPtr) {
405  psPtr.value() = (*vectors[i])[x];
406  }
407 
408  if (isWeighted) {
409  (*vecStack)[x] = makeStatistics(pixelSet, wvector, flags, sctrlTmp).getValue(flags);
410  } else {
411  (*vecStack)[x] = makeStatistics(pixelSet, flags, sctrlTmp).getValue(flags);
412  }
413  }
414 
415  return vecStack;
416 }
417 
418 } // end anonymous namespace
419 
420 template <typename PixelT>
423  StatisticsControl const &sctrl, WeightVector const &wvector) {
424  checkObjectsAndWeights(vectors, wvector);
425  checkOnlyOneFlag(flags);
426 
427  if (wvector.empty()) {
428  return computeVectorStack<PixelT, false>(vectors, flags, sctrl);
429  } else {
430  return computeVectorStack<PixelT, true>(vectors, flags, sctrl, wvector);
431  }
432 }
433 
434 /* ************************************************************************ *
435  *
436  * XY row column stacking
437  *
438  * ************************************************************************ */
439 
440 template <typename PixelT>
442  char dimension, StatisticsControl const &sctrl) {
443  int x0 = image.getX0();
444  int y0 = image.getY0();
445  typedef image::MaskedImage<PixelT> MImage;
447 
448  // do each row or column, one at a time
449  // - create a subimage with a bounding box, and get the stats and assign the value to the output image
450  if (dimension == 'x') {
451  imgOut = std::shared_ptr<MImage>(new MImage(geom::Extent2I(1, image.getHeight())));
452  int y = y0;
453  typename MImage::y_iterator oEnd = imgOut->col_end(0);
454  for (typename MImage::y_iterator oPtr = imgOut->col_begin(0); oPtr != oEnd; ++oPtr, ++y) {
456  image::Image<PixelT> subImage(image, bbox);
457  Statistics stat = makeStatistics(subImage, flags | ERRORS, sctrl);
458  *oPtr = typename image::MaskedImage<PixelT>::Pixel(stat.getValue(), 0x0,
459  stat.getError() * stat.getError());
460  }
461 
462  } else if (dimension == 'y') {
463  imgOut = std::shared_ptr<MImage>(new MImage(geom::Extent2I(image.getWidth(), 1)));
464  int x = x0;
465  typename MImage::x_iterator oEnd = imgOut->row_end(0);
466  for (typename MImage::x_iterator oPtr = imgOut->row_begin(0); oPtr != oEnd; ++oPtr, ++x) {
468  image::Image<PixelT> subImage(image, bbox);
469  Statistics stat = makeStatistics(subImage, flags | ERRORS, sctrl);
470  *oPtr = typename image::MaskedImage<PixelT>::Pixel(stat.getValue(), 0x0,
471  stat.getError() * stat.getError());
472  }
473  } else {
475  "Can only run statisticsStack in x or y for single image.");
476  }
477 
478  return imgOut;
479 }
480 
481 template <typename PixelT>
483  Property flags, char dimension,
484  StatisticsControl const &sctrl) {
485  int const x0 = image.getX0();
486  int const y0 = image.getY0();
487  typedef image::MaskedImage<PixelT> MImage;
489 
490  // do each row or column, one at a time
491  // - create a subimage with a bounding box, and get the stats and assign the value to the output image
492  if (dimension == 'x') {
493  imgOut = std::shared_ptr<MImage>(new MImage(geom::Extent2I(1, image.getHeight())));
494  int y = 0;
495  typename MImage::y_iterator oEnd = imgOut->col_end(0);
496  for (typename MImage::y_iterator oPtr = imgOut->col_begin(0); oPtr != oEnd; ++oPtr, ++y) {
498  image::MaskedImage<PixelT> subImage(image, bbox);
499  Statistics stat = makeStatistics(subImage, flags | ERRORS, sctrl);
500  *oPtr = typename image::MaskedImage<PixelT>::Pixel(stat.getValue(), 0x0,
501  stat.getError() * stat.getError());
502  }
503 
504  } else if (dimension == 'y') {
505  imgOut = std::shared_ptr<MImage>(new MImage(geom::Extent2I(image.getWidth(), 1)));
506  int x = 0;
507  typename MImage::x_iterator oEnd = imgOut->row_end(0);
508  for (typename MImage::x_iterator oPtr = imgOut->row_begin(0); oPtr != oEnd; ++oPtr, ++x) {
510  image::MaskedImage<PixelT> subImage(image, bbox);
511  Statistics stat = makeStatistics(subImage, flags | ERRORS, sctrl);
512  *oPtr = typename image::MaskedImage<PixelT>::Pixel(stat.getValue(), 0x0,
513  stat.getError() * stat.getError());
514  }
515  } else {
517  "Can only run statisticsStack in x or y for single image.");
518  }
519 
520  return imgOut;
521 }
522 
523 /*
524  * Explicit Instantiations
525  *
526  */
528 #define INSTANTIATE_STACKS(TYPE) \
529  template std::shared_ptr<image::Image<TYPE>> statisticsStack<TYPE>( \
530  std::vector<std::shared_ptr<image::Image<TYPE>>> & images, Property flags, \
531  StatisticsControl const &sctrl, WeightVector const &wvector); \
532  template void statisticsStack<TYPE>( \
533  image::Image<TYPE> & out, std::vector<std::shared_ptr<image::Image<TYPE>>> & images, \
534  Property flags, StatisticsControl const &sctrl, WeightVector const &wvector); \
535  template std::shared_ptr<image::MaskedImage<TYPE>> statisticsStack<TYPE>( \
536  std::vector<std::shared_ptr<image::MaskedImage<TYPE>>> & images, Property flags, \
537  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel, \
538  image::MaskPixel); \
539  template std::shared_ptr<image::MaskedImage<TYPE>> statisticsStack<TYPE>( \
540  std::vector<std::shared_ptr<image::MaskedImage<TYPE>>> & images, Property flags, \
541  StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel, \
542  std::vector<std::pair<image::MaskPixel, image::MaskPixel>> const &); \
543  template void statisticsStack<TYPE>( \
544  image::MaskedImage<TYPE> & out, std::vector<std::shared_ptr<image::MaskedImage<TYPE>>> & images, \
545  Property flags, StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel, \
546  image::MaskPixel); \
547  template void statisticsStack<TYPE>( \
548  image::MaskedImage<TYPE> & out, std::vector<std::shared_ptr<image::MaskedImage<TYPE>>> & images, \
549  Property flags, StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel, \
550  std::vector<std::pair<image::MaskPixel, image::MaskPixel>> const &); \
551  template std::shared_ptr<std::vector<TYPE>> statisticsStack<TYPE>( \
552  std::vector<std::shared_ptr<std::vector<TYPE>>> & vectors, Property flags, \
553  StatisticsControl const &sctrl, WeightVector const &wvector); \
554  template std::shared_ptr<image::MaskedImage<TYPE>> statisticsStack(image::Image<TYPE> const &image, \
555  Property flags, char dimension, \
556  StatisticsControl const &sctrl); \
557  template std::shared_ptr<image::MaskedImage<TYPE>> statisticsStack( \
558  image::MaskedImage<TYPE> const &image, Property flags, char dimension, \
559  StatisticsControl const &sctrl);
560 
561 INSTANTIATE_STACKS(double)
562 INSTANTIATE_STACKS(float)
564 }
565 }
566 } // end lsst::afw::math
afw::table::Key< afw::table::Array< VariancePixelT > > variance
lsst::afw::image::pixel::Pixel< ImagePixelT, MaskPixelT, VariancePixelT > Pixel
A Pixel in the MaskedImage.
Definition: MaskedImage.h:116
lsst::afw::image::MaskedImage< EntryT >::Image::Pixel & value()
Definition: MaskedVector.h:120
double getError(Property const prop=NOTHING) const
Return the error in the desired property (if specified in the constructor)
Definition: Statistics.cc:930
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:301
number of clipped points
Definition: Statistics.h:81
Extent< int, 2 > Extent2I
Definition: Extent.h:380
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:309
Include errors of requested quantities.
Definition: Statistics.h:65
A class to evaluate image statistics.
Definition: Statistics.h:215
number of masked points
Definition: Statistics.h:82
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
Definition: MaskedImage.h:547
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< lsst::afw::image::Image< PixelT > > statisticsStack(std::vector< std::shared_ptr< lsst::afw::image::Image< PixelT >>> &images, Property flags, StatisticsControl const &sctrl=StatisticsControl(), std::vector< lsst::afw::image::VariancePixel > const &wvector=std::vector< lsst::afw::image::VariancePixel >(0))
A function to compute some statistics of a stack of Images.
T push_back(T... args)
int getHeight() const
Return the number of rows in the image.
Definition: MaskedImage.h:1080
Pass parameters to a Statistics object.
Definition: Statistics.h:93
A base class for image defects.
int end
int getX0() const
Return the image&#39;s column-origin.
Definition: MaskedImage.h:1090
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
A Mask wrapper to provide an infinite_iterator for Mask::row_begin().
Definition: Statistics.h:339
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
Definition: MaskedImage.cc:694
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
number of sample points
Definition: Statistics.h:66
table::Key< int > b
T make_pair(T... args)
double x
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:928
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
Definition: MaskedImage.cc:704
int getY0() const
Return the image&#39;s row-origin.
Definition: MaskedImage.h:1098
#define LSST_EXCEPT(type,...)
STL class.
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:317
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:299
table::Box2IKey bbox
int getWidth() const
Return the number of columns in the image.
Definition: MaskedImage.h:1078
Property
control what is calculated
Definition: Statistics.h:63
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:69
int y
Definition: SpanSet.cc:44
T reserve(T... args)