49 int bitcount(
unsigned int x) {
51 for (b = 0; x != 0; x >>= 1) {
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.");
72 template <
typename ObjectVectorT,
typename WeightVectorT>
73 void checkObjectsAndWeights(ObjectVectorT
const &objects, WeightVectorT
const &wvector) {
74 if (objects.size() == 0) {
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()));
86 template <
typename ImageT>
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())
115 template <
typename PixelT,
bool isWeighted,
bool useVariance>
118 Property flags, StatisticsControl
const &sctrl,
121 WeightVector
const &wvector = WeightVector()) {
129 MaskedVector<PixelT> pixelSet(images.size());
130 WeightVector weights;
132 StatisticsControl sctrlTmp(sctrl);
136 assert(wvector.empty());
138 weights.resize(images.size());
140 sctrlTmp.setWeighted(
true);
141 }
else if (isWeighted) {
142 weights.assign(wvector.begin(), wvector.end());
144 sctrlTmp.setWeighted(
true);
146 assert(weights.empty() || weights.size() == images.size());
151 for (
unsigned int i = 0; i < images.size(); ++i) {
152 x_iterator ptr = images[i]->row_begin(
y);
162 WeightVector::iterator wtPtr = weights.begin();
163 for (
unsigned int i = 0; i < images.size(); ++rows[i], ++i, ++psPtr, ++wtPtr) {
166 *wtPtr = 1.0 / rows[i].variance();
171 Statistics stat = isWeighted ?
makeStatistics(pixelSet, weights, eflags, sctrlTmp)
172 : makeStatistics(pixelSet, eflags, sctrlTmp);
174 PixelT
variance = ::pow(stat.getError(flags), 2);
176 int const npoint = stat.getValue(
NPOINT);
178 msk = sctrlTmp.getNoGoodPixelsMask();
179 }
else if (npoint == 1) {
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) {
206 template <
typename PixelT,
bool isWeighted,
bool useVariance>
209 Property flags, StatisticsControl
const &sctrl,
212 WeightVector
const &wvector = WeightVector()) {
215 computeMaskedImageStack<PixelT, isWeighted, useVariance>(
216 imgStack, images, flags, sctrl, clipped, maskMap, wvector
223 template <
typename PixelT>
228 if (images.size() == 0) {
237 template <
typename PixelT>
242 if (images.size() == 0) {
247 statisticsStack(*out, images, flags, sctrl, wvector, clipped, maskMap);
251 template <
typename PixelT>
256 checkObjectsAndWeights(images, wvector);
257 checkOnlyOneFlag(flags);
258 checkImageSizes(out, images);
261 if (wvector.empty()) {
262 return computeMaskedImageStack<PixelT, true, true>(out, images, flags, sctrl,
265 return computeMaskedImageStack<PixelT, true, false>(out, images, flags, sctrl, clipped, excuse,
269 return computeMaskedImageStack<PixelT, false, false>(out, images, flags, sctrl, clipped, excuse);
273 template <
typename PixelT>
278 checkObjectsAndWeights(images, wvector);
279 checkOnlyOneFlag(flags);
280 checkImageSizes(out, images);
283 if (wvector.empty()) {
284 return computeMaskedImageStack<PixelT, true, true>(out, images, flags, sctrl,
287 return computeMaskedImageStack<PixelT, true, false>(out, images, flags, sctrl, clipped, maskMap,
291 return computeMaskedImageStack<PixelT, false, false>(out, images, flags, sctrl, clipped, maskMap);
311 template <
typename PixelT,
bool isWeighted>
321 if (!weights.empty()) {
322 sctrlTmp.setWeighted(
true);
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);
343 template <
typename PixelT>
347 if (images.size() == 0) {
355 template <
typename PixelT>
358 checkObjectsAndWeights(images, wvector);
359 checkOnlyOneFlag(flags);
360 checkImageSizes(out, images);
362 if (wvector.empty()) {
363 return computeImageStack<PixelT, false>(out, images, flags, sctrl);
365 return computeImageStack<PixelT, true>(out, images, flags, sctrl, wvector);
383 template <
typename PixelT,
bool isWeighted>
397 if (!wvector.empty()) {
398 sctrlTmp.setWeighted(
true);
402 for (
unsigned int x = 0; x < vectors[0]->size(); ++
x) {
404 for (
unsigned int i = 0; i < vectors.size(); ++i, ++psPtr) {
405 psPtr.
value() = (*vectors[i])[x];
420 template <
typename PixelT>
424 checkObjectsAndWeights(vectors, wvector);
425 checkOnlyOneFlag(flags);
427 if (wvector.empty()) {
428 return computeVectorStack<PixelT, false>(vectors, flags, sctrl);
430 return computeVectorStack<PixelT, true>(vectors, flags, sctrl, wvector);
440 template <
typename PixelT>
443 int x0 = image.
getX0();
444 int y0 = image.
getY0();
450 if (dimension ==
'x') {
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) {
462 }
else if (dimension ==
'y') {
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) {
475 "Can only run statisticsStack in x or y for single image.");
481 template <
typename PixelT>
485 int const x0 = image.
getX0();
486 int const y0 = image.
getY0();
492 if (dimension ==
'x') {
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) {
504 }
else if (dimension ==
'y') {
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) {
517 "Can only run statisticsStack in x or y for single image.");
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, \ 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, \ 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); 561 INSTANTIATE_STACKS(
double)
562 INSTANTIATE_STACKS(
float)
lsst::afw::image::pixel::Pixel< ImagePixelT, MaskPixelT, VariancePixelT > Pixel
A Pixel in the MaskedImage.
lsst::afw::image::MaskedImage< EntryT >::Image::Pixel & value()
double getError(Property const prop=NOTHING) const
Return the error in the desired property (if specified in the constructor)
int getHeight() const
Return the number of rows in the image.
Extent< int, 2 > Extent2I
int getX0() const
Return the image's column-origin.
Include errors of requested quantities.
A class to evaluate image statistics.
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
An integer coordinate rectangle.
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.
int getHeight() const
Return the number of rows in the image.
Pass parameters to a Statistics object.
A base class for image defects.
int getX0() const
Return the image's column-origin.
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)
A Mask wrapper to provide an infinite_iterator for Mask::row_begin().
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
A class to manipulate images, masks, and variance as a single object.
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
int getY0() const
Return the image's row-origin.
#define LSST_EXCEPT(type,...)
int getY0() const
Return the image's row-origin.
int getWidth() const
Return the number of columns in the image.
int getWidth() const
Return the number of columns in the image.
Property
control what is calculated
A class to represent a 2-dimensional array of pixels.