lsst.afw  16.0-10-gd1b3a1c94
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 
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 (" << width
71  << ", " << height << "), because at least one dimension would change by an odd value.";
73  }
74 
75  lsst::geom::Box2I bboxNew(lsst::geom::Point2I((1 - width) / 2, (1 - height) / 2),
76  lsst::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::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::geom::Box2I bboxIntersectShifted = lsst::geom::Box2I(
90  lsst::geom::Point2I(bboxIntersect.getMinX() + offsetX, bboxIntersect.getMinY() + offsetY),
91  bboxIntersect.getDimensions());
92  image::Image<Pixel> imIntersect = image::Image<Pixel>(_image, bboxIntersectShifted);
93 
94  imNew->assign(imIntersect, bboxIntersect);
95  std::shared_ptr<Kernel> retPtr = std::make_shared<FixedKernel>(*imNew);
96  return retPtr;
97 }
98 
99 double FixedKernel::doComputeImage(image::Image<Pixel>& image, bool doNormalize) const {
100  double multFactor = 1.0;
101  double imSum = this->_sum;
102  if (doNormalize) {
103  if (imSum == 0) {
104  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
105  }
106  multFactor = 1.0 / static_cast<double>(this->_sum);
107  imSum = 1.0;
108  }
109 
110  typedef image::Image<Pixel>::x_iterator XIter;
111 
112  for (int y = 0; y != this->getHeight(); ++y) {
113  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y), kPtr = this->_image.row_begin(y);
114  imPtr != imEnd; ++imPtr, ++kPtr) {
115  imPtr[0] = multFactor * kPtr[0];
116  }
117  }
118 
119  return imSum;
120 }
121 
124  os << prefix << "FixedKernel:" << std::endl;
125  os << prefix << "..sum: " << _sum << std::endl;
126  os << Kernel::toString(prefix + "\t");
127  return os.str();
128 }
129 
130 // ------ Persistence ---------------------------------------------------------------------------------------
131 
132 namespace {
133 
134 struct FixedKernelPersistenceHelper : public Kernel::PersistenceHelper {
135  table::Key<table::Array<Kernel::Pixel>> image;
136 
137  explicit FixedKernelPersistenceHelper(lsst::geom::Extent2I const& dimensions)
138  : Kernel::PersistenceHelper(0),
139  image(schema.addField<table::Array<Kernel::Pixel>>("image", "pixel values (row-major)",
140  dimensions.getX() * dimensions.getY())) {}
141 
142  explicit FixedKernelPersistenceHelper(table::Schema const& schema_)
143  : Kernel::PersistenceHelper(schema_), image(schema["image"]) {}
144 };
145 
146 } // namespace
147 
149 public:
151  CatalogVector const& catalogs) const {
152  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
153  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
154  FixedKernelPersistenceHelper const keys(catalogs.front().getSchema());
155  afw::table::BaseRecord const& record = catalogs.front().front();
156  image::Image<Pixel> image(lsst::geom::Extent2I(record.get(keys.dimensions)));
157  ndarray::flatten<1>(ndarray::static_dimension_cast<2>(image.getArray())) = record[keys.image];
158  std::shared_ptr<FixedKernel> result = std::make_shared<FixedKernel>(image);
159  result->setCtr(record.get(keys.center));
160  return result;
161  }
162 
163  explicit Factory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
164 };
165 
166 namespace {
167 
168 std::string getFixedKernelPersistenceName() { return "FixedKernel"; }
169 
170 FixedKernel::Factory registration(getFixedKernelPersistenceName());
171 
172 } // namespace
173 
174 std::string FixedKernel::getPersistenceName() const { return getFixedKernelPersistenceName(); }
175 
177  FixedKernelPersistenceHelper const keys(getDimensions());
178  std::shared_ptr<afw::table::BaseRecord> record = keys.write(handle, *this);
179  (*record)[keys.image] = ndarray::flatten<1>(ndarray::copy(_image.getArray()));
180 }
181 } // namespace math
182 } // namespace afw
183 } // namespace lsst
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:245
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: FixedKernel.cc:122
def keys(self)
An object passed to Persistable::write to allow it to persist itself.
T front(T... args)
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:48
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:301
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:217
T endl(T... args)
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:135
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:309
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:404
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: FixedKernel.cc:176
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.
lsst::geom::Box2I getBBox() const
return parent bounding box, with XY0 = -center
Definition: Kernel.h:269
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
int getMinX() const
T str(T... args)
int getMinY() const
lsst::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:250
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
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:240
lsst::geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:228
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:150
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:31
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:317
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: FixedKernel.cc:174
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
Definition: FixedKernel.cc:99
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:135
std::string prefix
Definition: SchemaMapper.cc:79
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: ImageBase.h:407
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
std::ostream * os
Definition: Schema.cc:737
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
int y
Definition: SpanSet.cc:44
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:199
Factory(std::string const &name)
Definition: FixedKernel.cc:163