28 #include "boost/format.hpp"
29 #include "lsst/pex/exceptions.h"
46 struct FlattenWithSetter {
47 FlattenWithSetter(T val) :
_val(val) {}
61 FlattenWithSetter(T val) : _mask(~val) {}
73 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
78 _image(ndarray::allocate(ndarray::makeVector(foot.getArea()))),
79 _mask(ndarray::allocate(ndarray::makeVector(foot.getArea()))),
80 _variance(ndarray::allocate(ndarray::makeVector(foot.getArea()))) {
109 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
113 _image(ndarray::allocate(ndarray::makeVector(foot.getArea()))),
114 _mask(ndarray::allocate(ndarray::makeVector(foot.getArea()))),
115 _variance(ndarray::allocate(ndarray::makeVector(foot.getArea()))) {}
117 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
120 getSpans()->unflatten(mimage.
getImage()->getArray(), _image, mimage.
getXY0());
121 getSpans()->unflatten(mimage.
getMask()->getArray(), _mask, mimage.
getXY0());
122 getSpans()->unflatten(mimage.
getVariance()->getArray(), _variance, mimage.
getXY0());
125 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
130 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
150 return std::make_shared<HeavyFootprint<ImagePixelT, MaskPixelT, VariancePixelT>>(*foot, im1);
153 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
157 typedef typename ndarray::Array<ImagePixelT const, 1, 1>::Iterator ArrayIter;
158 ArrayIter lhsArray = getImageArray().begin(), rhsArray = rhs.
getImageArray().begin();
159 auto lhsIter = getSpans()->begin(), rhsIter = rhs.
getSpans()->begin();
160 auto const lhsEnd = getSpans()->end(), rhsEnd = rhs.
getSpans()->end();
162 while (lhsIter != lhsEnd && rhsIter != rhsEnd) {
163 geom::Span const &lhsSpan = *lhsIter, rhsSpan = *rhsIter;
164 int const yLhs = lhsSpan.
getY(), yRhs = rhsSpan.getY();
166 int const x0Lhs = lhsSpan.
getX0(), x1Lhs = lhsSpan.
getX1();
167 int const x0Rhs = rhsSpan.getX0(), x1Rhs = rhsSpan.getX1();
168 int const xMin = std::max(x0Lhs, x0Rhs), xMax =
std::min(x1Lhs, x1Rhs);
170 lhsArray += xMin - x0Lhs;
171 rhsArray += xMin - x0Rhs;
172 for (
int x = xMin;
x <= xMax; ++
x, ++lhsArray, ++rhsArray) {
173 sum += (*lhsArray) * (*rhsArray);
176 lhsArray -= xMax + 1 - x0Lhs;
177 rhsArray -= xMax + 1 - x0Rhs;
179 if (x1Lhs <= x1Rhs) {
183 rhsArray += rhsSpan.getWidth();
187 }
else if (yLhs < yRhs) {
188 while (lhsIter != lhsEnd && lhsIter->getY() < yRhs) {
189 lhsArray += lhsIter->getWidth();
194 while (rhsIter != rhsEnd && rhsIter->getY() < yLhs) {
195 rhsArray += rhsIter->getWidth();
213 struct HeavyFootprintPersistenceHelper {
215 afw::table::Key<afw::table::Array<ImagePixelT>>
image;
216 afw::table::Key<afw::table::Array<MaskPixelT>>
mask;
217 afw::table::Key<afw::table::Array<VariancePixelT>>
variance;
219 static HeavyFootprintPersistenceHelper
const&
get() {
220 static HeavyFootprintPersistenceHelper
const instance;
225 HeavyFootprintPersistenceHelper()
227 image(
schema.addField<afw::table::Array<ImagePixelT>>(
228 "image",
"image pixels for HeavyFootprint",
"count")),
229 mask(
schema.addField<afw::table::Array<MaskPixelT>>(
"mask",
"mask pixels for HeavyFootprint")),
231 "variance",
"variance pixels for HeavyFootprint",
"count^2")) {
232 schema.getCitizen().markPersistent();
243 struct ComputeSuffix;
245 struct ComputeSuffix<std::uint16_t> {
246 static std::string apply() {
return "U"; }
249 struct ComputeSuffix<float> {
250 static std::string apply() {
return "F"; }
253 struct ComputeSuffix<double> {
254 static std::string apply() {
return "D"; }
257 struct ComputeSuffix<int> {
258 static std::string apply() {
return "I"; }
263 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
265 return "HeavyFootprint" + ComputeSuffix<ImagePixelT, MaskPixelT, VariancePixelT>::apply();
268 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
270 HeavyFootprintPersistenceHelper<ImagePixelT, MaskPixelT, VariancePixelT>
const& keys =
271 HeavyFootprintPersistenceHelper<ImagePixelT, MaskPixelT, VariancePixelT>::get();
276 std::shared_ptr<afw::table::BaseRecord> record = cat.
addNew();
280 record->set(keys.image, ndarray::const_array_cast<ImagePixelT>(getImageArray()));
281 record->set(keys.mask, ndarray::const_array_cast<MaskPixelT>(getMaskArray()));
282 record->set(keys.variance, ndarray::const_array_cast<VariancePixelT>(getVarianceArray()));
286 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
290 explicit Factory(std::string
const&
name) : afw::table::io::PersistableFactory(name) {}
294 HeavyFootprintPersistenceHelper<ImagePixelT, MaskPixelT, VariancePixelT>
const& keys =
295 HeavyFootprintPersistenceHelper<ImagePixelT, MaskPixelT, VariancePixelT>::get();
299 std::shared_ptr<Footprint> loadedFootprint =
readSpanSet(catalogs[0], archive);
301 readPeaks(catalogs[1], *loadedFootprint);
306 std::make_shared<HeavyFootprint<ImagePixelT, MaskPixelT, VariancePixelT>>(*loadedFootprint);
307 result->_image = ndarray::const_array_cast<ImagePixelT>(record.
get(keys.image));
308 result->_mask = ndarray::const_array_cast<MaskPixelT>(record.
get(keys.mask));
309 result->_variance = ndarray::const_array_cast<VariancePixelT>(record.
get(keys.variance));
317 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
320 "HeavyFootprint" + ComputeSuffix<ImagePixelT, MaskPixelT, VariancePixelT>::apply());
326 #define INSTANTIATE(TYPE) \
327 template class HeavyFootprint<TYPE>; \
328 template std::shared_ptr<HeavyFootprint<TYPE>> mergeHeavyFootprints<TYPE>(HeavyFootprint<TYPE> const&, \
329 HeavyFootprint<TYPE> const&);
std::uint16_t MaskPixel
default type for Masks and MaskedImage Masks
int getWidth() const
Return the number of pixels.
int getX0() const
Return the starting x-value.
An object passed to Persistable::write to allow it to persist itself.
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
A custom container class for records, based on std::vector.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
A range of pixels within one row of an Image.
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
A base class for factory classes used to reconstruct objects from records.
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
An integer coordinate rectangle.
geom::Point2I getXY0() const
Return the image's origin.
int getY() const
Return the y-value.
A class to manipulate images, masks, and variance as a single object.
details::FlatNdGetter< T, inA, inB > ndFlat(ndarray::Array< T, inA, inB > const &array)
Marks a ndarray to be interpreted as a 1D vector when applying a functor from a SpanSet.
table::Key< std::string > name
std::shared_ptr< Footprint > mergeFootprints(Footprint const &footprint1, Footprint const &footprint2)
Merges two Footprints – appends their peaks, and unions their spans, returning a new Footprint...
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
details::ImageNdGetter< T, inA, inB > ndImage(ndarray::Array< T, inA, inB > const &array, afwGeom::Point2I xy0=afwGeom::Point2I())
Marks a ndarray to be interpreted as an image when applying a functor from a SpanSet.
std::shared_ptr< HeavyFootprint< ImagePixelT, MaskPixelT, VariancePixelT > > mergeHeavyFootprints(HeavyFootprint< ImagePixelT, MaskPixelT, VariancePixelT > const &h1, HeavyFootprint< ImagePixelT, MaskPixelT, VariancePixelT > const &h2)
Sum the two given HeavyFootprints h1 and h2, returning a HeavyFootprint with the union footprint...
A vector of catalogs used by Persistable.
Base class for all records.
geom::Point2I getXY0() const
Return the image's origin.
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
int getX1() const
Return the ending x-value.
float VariancePixel
default type for MaskedImage variance images
A class to represent a 2-dimensional array of pixels.
afw::table::Schema schema
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.