lsst.afw  14.0-25-g0e8fc950e+7
FixedKernel.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 #include <stdexcept>
25 #include <numeric>
26 
27 #include "lsst/pex/exceptions.h"
28 #include "lsst/afw/math/Kernel.h"
30 
32 
33 namespace lsst {
34 namespace afw {
35 namespace math {
36 
37 FixedKernel::FixedKernel() : Kernel(), _image(), _sum(0) {}
38 
40  : Kernel(image.getWidth(), image.getHeight(), 0), _image(image, true), _sum(0) {
41  typedef image::Image<Pixel>::x_iterator XIter;
42  double imSum = 0.0;
43  for (int y = 0; y != image.getHeight(); ++y) {
44  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y); imPtr != imEnd; ++imPtr) {
45  imSum += *imPtr;
46  }
47  }
48  this->_sum = imSum;
49 }
50 
51 FixedKernel::FixedKernel(Kernel const& kernel, geom::Point2D const& pos)
52  : Kernel(kernel.getWidth(), kernel.getHeight(), 0), _image(kernel.getDimensions()), _sum(0) {
53  _sum = kernel.computeImage(_image, false, pos[0], pos[1]);
54 }
55 
57  std::shared_ptr<Kernel> retPtr(new FixedKernel(_image));
58  retPtr->setCtr(this->getCtr());
59  return retPtr;
60 }
61 
62 std::shared_ptr<Kernel> FixedKernel::resized(int width, int height) const {
63  if ((width <= 0) || (height <= 0)) {
65  os << "Cannot create FixedKernel with dimensions (" << width << ", " << height << "). ";
67  }
68  if ((width - getWidth()) % 2 || (height - getHeight()) % 2) {
70  os << "Cannot resize FixedKernel from (" << getWidth() << ", " << getHeight() << ") to ("
71  << width << ", " << height << "), because at least one dimension would change by an odd value.";
73  }
74 
75  lsst::afw::geom::Box2I bboxNew(lsst::afw::geom::Point2I((1 - width) / 2, (1 - height) / 2),
76  lsst::afw::geom::Extent2I(width, height));
77  std::shared_ptr<image::Image<Pixel>> imNew = std::make_shared<image::Image<Pixel>>(bboxNew);
78 
79  // getBBox() instantiates a new BBox from member data _width, _height, _ctrX, _ctrY
80  // so modifying it is OK
81  lsst::afw::geom::Box2I bboxIntersect = getBBox();
82  bboxIntersect.clip(bboxNew);
83 
84  // Kernel (this) and member image (this->_image) do not always have same XY0.
85  // Member image of resized kernel will not have same BBox as orig,
86  // but BBox of member image is ignored by the kernel.
87  int offsetX = _image.getX0() - getBBox().getMinX();
88  int offsetY = _image.getY0() - getBBox().getMinY();
89  lsst::afw::geom::Box2I bboxIntersectShifted = lsst::afw::geom::Box2I(
90  lsst::afw::geom::Point2I(bboxIntersect.getMinX() + offsetX,
91  bboxIntersect.getMinY() + offsetY),
92  bboxIntersect.getDimensions());
93  image::Image<Pixel> imIntersect = image::Image<Pixel>(_image, bboxIntersectShifted);
94 
95  imNew->assign(imIntersect, bboxIntersect);
96  std::shared_ptr<Kernel> retPtr = std::make_shared<FixedKernel>(*imNew);
97  return retPtr;
98 }
99 
100 double FixedKernel::doComputeImage(image::Image<Pixel>& image, bool doNormalize) const {
101  double multFactor = 1.0;
102  double imSum = this->_sum;
103  if (doNormalize) {
104  if (imSum == 0) {
105  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
106  }
107  multFactor = 1.0 / static_cast<double>(this->_sum);
108  imSum = 1.0;
109  }
110 
111  typedef image::Image<Pixel>::x_iterator XIter;
112 
113  for (int y = 0; y != this->getHeight(); ++y) {
114  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y), kPtr = this->_image.row_begin(y);
115  imPtr != imEnd; ++imPtr, ++kPtr) {
116  imPtr[0] = multFactor * kPtr[0];
117  }
118  }
119 
120  return imSum;
121 }
122 
125  os << prefix << "FixedKernel:" << std::endl;
126  os << prefix << "..sum: " << _sum << std::endl;
127  os << Kernel::toString(prefix + "\t");
128  return os.str();
129 }
130 
131 // ------ Persistence ---------------------------------------------------------------------------------------
132 
133 namespace {
134 
135 struct FixedKernelPersistenceHelper : public Kernel::PersistenceHelper {
136  table::Key<table::Array<Kernel::Pixel> > image;
137 
138  explicit FixedKernelPersistenceHelper(geom::Extent2I const& dimensions)
139  : Kernel::PersistenceHelper(0),
140  image(schema.addField<table::Array<Kernel::Pixel> >("image", "pixel values (row-major)",
141  dimensions.getX() * dimensions.getY())) {}
142 
143  explicit FixedKernelPersistenceHelper(table::Schema const& schema_)
144  : Kernel::PersistenceHelper(schema_), image(schema["image"]) {}
145 };
146 
147 } // anonymous
148 
150 public:
152  CatalogVector const& catalogs) const {
153  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
154  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
155  FixedKernelPersistenceHelper const keys(catalogs.front().getSchema());
156  afw::table::BaseRecord const& record = catalogs.front().front();
157  image::Image<Pixel> image(geom::Extent2I(record.get(keys.dimensions)));
158  ndarray::flatten<1>(ndarray::static_dimension_cast<2>(image.getArray())) = record[keys.image];
159  std::shared_ptr<FixedKernel> result = std::make_shared<FixedKernel>(image);
160  result->setCtr(record.get(keys.center));
161  return result;
162  }
163 
164  explicit Factory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
165 };
166 
167 namespace {
168 
169 std::string getFixedKernelPersistenceName() { return "FixedKernel"; }
170 
171 FixedKernel::Factory registration(getFixedKernelPersistenceName());
172 
173 } // anonymous
174 
175 std::string FixedKernel::getPersistenceName() const { return getFixedKernelPersistenceName(); }
176 
178  FixedKernelPersistenceHelper const keys(getDimensions());
179  std::shared_ptr<afw::table::BaseRecord> record = keys.write(handle, *this);
180  (*record)[keys.image] = ndarray::flatten<1>(ndarray::copy(_image.getArray()));
181 }
182 }
183 }
184 } // namespace lsst::afw::math
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:239
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: FixedKernel.cc:123
int getMinX() const
Definition: Box.h:124
table::Key< std::string > name
Definition: ApCorrMap.cc:71
An object passed to Persistable::write to allow it to persist itself.
T front(T... args)
lsst::afw::geom::Box2I getBBox() const
return parent bounding box, with XY0 = -center
Definition: Kernel.h:263
afw::table::Schema schema
Definition: GaussianPsf.cc:43
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:214
T endl(T... args)
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:137
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:244
Extent< int, 2 > Extent2I
Definition: Extent.h:341
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:307
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:402
An integer coordinate rectangle.
Definition: Box.h:55
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: FixedKernel.cc:177
STL class.
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
Definition: FixedKernel.cc:62
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
A base class for image defects.
FixedKernel()
Construct an empty FixedKernel of size 0x0.
Definition: FixedKernel.cc:37
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
Definition: FixedKernel.cc:56
T str(T... args)
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
Definition: Kernel.cc:88
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:234
virtual std::shared_ptr< afw::table::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
Construct a new object from the given InputArchive and vector of catalogs.
Definition: FixedKernel.cc:151
T size(T... args)
#define LSST_EXCEPT(type,...)
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
Base class for all records.
Definition: BaseRecord.h:29
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:315
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: FixedKernel.cc:175
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
Definition: FixedKernel.cc:100
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:136
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: ImageBase.h:405
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
int getMinY() const
Definition: Box.h:125
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:222
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
Factory(std::string const &name)
Definition: FixedKernel.cc:164