lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
image.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 
25 #include "numpy/arrayobject.h"
26 #include "ndarray/pybind11.h"
27 
28 #include "lsst/afw/image/Image.h"
30 #include "lsst/afw/fits.h"
31 
32 namespace py = pybind11;
33 using namespace pybind11::literals;
34 
35 namespace lsst {
36 namespace afw {
37 namespace image {
38 
39 namespace {
40 
41 template <typename PixelT>
42 using PyImageBase = py::class_<ImageBase<PixelT>, std::shared_ptr<ImageBase<PixelT>>, daf::base::Persistable,
43  daf::base::Citizen>;
44 
45 template <typename PixelT>
46 using PyImage = py::class_<Image<PixelT>, std::shared_ptr<Image<PixelT>>, ImageBase<PixelT>>;
47 
48 template <typename PixelT>
49 using PyDecoratedImage =
50  py::class_<DecoratedImage<PixelT>, std::shared_ptr<DecoratedImage<PixelT>>, daf::base::Persistable>;
51 
60 template <typename FromPixelT, typename ToPixelT>
61 static void declareCastConstructor(PyImage<ToPixelT> &cls) {
62  cls.def(py::init<Image<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
63 }
64 
65 template <typename PixelT>
66 static void declareImageBase(py::module &mod, std::string const &suffix) {
67  PyImageBase<PixelT> cls(mod, ("ImageBase" + suffix).c_str());
68 
69  using Array = typename ImageBase<PixelT>::Array;
70 
71  cls.def(py::init<geom::Extent2I const &>(), "dimensions"_a = geom::Extent2I());
72  cls.def(py::init<ImageBase<PixelT> const &, bool>(), "src"_a, "deep"_a = false);
73  cls.def(py::init<ImageBase<PixelT> const &, geom::Box2I const &, ImageOrigin, bool>(), "src"_a, "bbox"_a,
74  "origin"_a = PARENT, "deep"_a = false);
75  cls.def(py::init<Array const &, bool, geom::Point2I const &>(), "array"_a, "deep"_a = false,
76  "xy0"_a = geom::Point2I());
77 
78  cls.def("assign", &ImageBase<PixelT>::assign, "rhs"_a, "bbox"_a = geom::Box2I(), "origin"_a = PARENT,
79  py::is_operator()); // py::is_operator is a workaround for code in slicing.py
80  // that expects NotImplemented to be returned on failure.
81  cls.def("getWidth", &ImageBase<PixelT>::getWidth);
82  cls.def("getHeight", &ImageBase<PixelT>::getHeight);
83  cls.def("getX0", &ImageBase<PixelT>::getX0);
84  cls.def("getY0", &ImageBase<PixelT>::getY0);
85  cls.def("getXY0", &ImageBase<PixelT>::getXY0);
86  cls.def("positionToIndex", &ImageBase<PixelT>::positionToIndex, "position"_a, "xOrY"_a);
87  cls.def("indexToPosition", &ImageBase<PixelT>::indexToPosition, "index"_a, "xOrY"_a);
88  cls.def("getDimensions", &ImageBase<PixelT>::getDimensions);
89  cls.def("getArray", (Array (ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray);
90  cls.def_property(
91  "array",
92  (Array (ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray,
93  [](ImageBase<PixelT> & self, ndarray::Array<PixelT const,2,0> const & array) {
94  // Avoid self-assignment, which is invoked when a Python in-place operator is used.
95  if (array.shallow() != self.getArray().shallow()) {
96  self.getArray().deep() = array;
97  }
98  }
99  );
100  cls.def("setXY0", (void (ImageBase<PixelT>::*)(geom::Point2I const)) & ImageBase<PixelT>::setXY0,
101  "xy0"_a);
102  cls.def("setXY0", (void (ImageBase<PixelT>::*)(int const, int const)) & ImageBase<PixelT>::setXY0, "x0"_a,
103  "y0"_a);
104  cls.def("getBBox", &ImageBase<PixelT>::getBBox, "origin"_a = PARENT);
105 }
106 
107 template <typename PixelT>
108 static PyImage<PixelT> declareImage(py::module &mod, const std::string &suffix) {
109  PyImage<PixelT> cls(mod, ("Image" + suffix).c_str());
110 
111  /* Constructors */
112  cls.def(py::init<unsigned int, unsigned int, PixelT>(), "width"_a, "height"_a, "intialValue"_a = 0);
113  cls.def(py::init<geom::Extent2I const &, PixelT>(), "dimensions"_a = geom::Extent2I(),
114  "initialValue"_a = 0);
115  cls.def(py::init<geom::Box2I const &, PixelT>(), "bbox"_a, "initialValue"_a = 0);
116  cls.def(py::init<Image<PixelT> const &, geom::Box2I const &, ImageOrigin const, const bool>(), "rhs"_a,
117  "bbox"_a, "origin"_a = PARENT, "deep"_a = false);
118  cls.def(py::init<ndarray::Array<PixelT, 2, 1> const &, bool, geom::Point2I const &>(), "array"_a,
119  "deep"_a = false, "xy0"_a = geom::Point2I());
120  cls.def(py::init<std::string const &, int, std::shared_ptr<daf::base::PropertySet>, geom::Box2I const &,
121  ImageOrigin>(),
122  "fileName"_a, "hdu"_a = INT_MIN, "metadata"_a = nullptr, "bbox"_a = geom::Box2I(),
123  "origin"_a = PARENT);
124  cls.def(py::init<fits::MemFileManager &, int, std::shared_ptr<daf::base::PropertySet>,
125  geom::Box2I const &, ImageOrigin>(),
126  "manager"_a, "hdu"_a = INT_MIN, "metadata"_a = nullptr, "bbox"_a = geom::Box2I(),
127  "origin"_a = PARENT);
128  cls.def(py::init<fits::Fits &, std::shared_ptr<daf::base::PropertySet>, geom::Box2I const &,
129  ImageOrigin>(),
130  "fitsFile"_a, "metadata"_a = nullptr, "bbox"_a = geom::Box2I(), "origin"_a = PARENT);
131 
132  /* Operators */
133  cls.def("__iadd__", [](Image<PixelT> &self, PixelT const &other) { return self += other; });
134  cls.def("__iadd__", [](Image<PixelT> &self, Image<PixelT> const &other) { return self += other; });
135  cls.def("__iadd__", [](Image<PixelT> &self, lsst::afw::math::Function2<double> const &other) {
136  return self += other;
137  });
138  cls.def("__isub__", [](Image<PixelT> &self, PixelT const &other) { return self -= other; });
139  cls.def("__isub__", [](Image<PixelT> &self, Image<PixelT> const &other) { return self -= other; });
140  cls.def("__isub__", [](Image<PixelT> &self, lsst::afw::math::Function2<double> const &other) {
141  return self -= other;
142  });
143  cls.def("__imul__", [](Image<PixelT> &self, PixelT const &other) { return self *= other; });
144  cls.def("__imul__", [](Image<PixelT> &self, Image<PixelT> const &other) { return self *= other; });
145  cls.def("__itruediv__", [](Image<PixelT> &self, PixelT const &other) { return self /= other; });
146  cls.def("__itruediv__", [](Image<PixelT> &self, Image<PixelT> const &other) { return self /= other; });
147 
148  /* Members */
149  cls.def("scaledPlus", &Image<PixelT>::scaledPlus);
150  cls.def("scaledMinus", &Image<PixelT>::scaledMinus);
151  cls.def("scaledMultiplies", &Image<PixelT>::scaledMultiplies);
152  cls.def("scaledDivides", &Image<PixelT>::scaledDivides);
153 
154  cls.def("writeFits",
155  (void (Image<PixelT>::*)(std::string const &, std::shared_ptr<daf::base::PropertySet const>,
156  std::string const &) const) &
157  Image<PixelT>::writeFits,
158  "fileName"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(), "mode"_a = "w");
159  cls.def("writeFits",
160  (void (Image<PixelT>::*)(fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet const>,
161  std::string const &) const) &
162  Image<PixelT>::writeFits,
163  "manager"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(), "mode"_a = "w");
164  cls.def("writeFits",
165  (void (Image<PixelT>::*)(fits::Fits &, std::shared_ptr<daf::base::PropertySet const>) const) &
166  Image<PixelT>::writeFits,
167  "fitsfile"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>());
168  cls.def("writeFits",
169  (void (Image<PixelT>::*)(std::string const&, fits::ImageWriteOptions const&,
170  std::string const&, std::shared_ptr<daf::base::PropertySet const>,
171  std::shared_ptr<image::Mask<image::MaskPixel> const>) const)
172  &Image<PixelT>::writeFits,
173  "filename"_a, "options"_a, "mode"_a="w", "header"_a=std::shared_ptr<daf::base::PropertyList>(),
174  "mask"_a=std::shared_ptr<image::Mask<image::MaskPixel>>());
175  cls.def("writeFits",
176  (void (Image<PixelT>::*)(fits::MemFileManager &, fits::ImageWriteOptions const&,
177  std::string const&, std::shared_ptr<daf::base::PropertySet const>,
178  std::shared_ptr<image::Mask<image::MaskPixel> const>) const)
179  &Image<PixelT>::writeFits,
180  "manager"_a, "options"_a, "mode"_a="w", "header"_a=std::shared_ptr<daf::base::PropertyList>(),
181  "mask"_a=std::shared_ptr<image::Mask<image::MaskPixel>>());
182  cls.def("writeFits",
183  (void (Image<PixelT>::*)(fits::Fits &, fits::ImageWriteOptions const&,
184  std::shared_ptr<daf::base::PropertySet const>,
185  std::shared_ptr<image::Mask<image::MaskPixel> const>) const)
186  &Image<PixelT>::writeFits,
187  "fits"_a, "options"_a, "header"_a=std::shared_ptr<daf::base::PropertyList>(),
188  "mask"_a=std::shared_ptr<image::Mask<image::MaskPixel>>());
189 
190  cls.def_static("readFits", (Image<PixelT>(*)(std::string const &, int))Image<PixelT>::readFits,
191  "filename"_a, "hdu"_a = INT_MIN);
192  cls.def_static("readFits", (Image<PixelT>(*)(fits::MemFileManager &, int))Image<PixelT>::readFits,
193  "manager"_a, "hdu"_a = INT_MIN);
194  cls.def("sqrt", &Image<PixelT>::sqrt);
195 
196  /* Add-ons for Python interface only */
197  cls.def("set", [](Image<PixelT> &img, double val) { img = val; });
198  cls.def("set", [](Image<PixelT> &img, int x, int y, double val) {
199  img(x, y, lsst::afw::image::CheckIndices(true)) = val;
200  });
201  cls.def("get",
202  [](Image<PixelT> &img, int x, int y) { return img(x, y, lsst::afw::image::CheckIndices(true)); });
203  cls.def("set0", [](Image<PixelT> &img, int x, int y, double val) {
204  img.set0(x, y, val, lsst::afw::image::CheckIndices(true));
205  });
206  cls.def("get0", [](Image<PixelT> &img, int x, int y) {
207  return img.get0(x, y, lsst::afw::image::CheckIndices(true));
208  });
209 
210  return cls;
211 }
212 
213 template <typename PixelT>
214 static void declareDecoratedImage(py::module &mod, std::string const &suffix) {
215  PyDecoratedImage<PixelT> cls(mod, ("DecoratedImage" + suffix).c_str());
216 
217  cls.def(py::init<const lsst::afw::geom::Extent2I &>(), "dimensions"_a = lsst::afw::geom::Extent2I());
218  cls.def(py::init<const lsst::afw::geom::Box2I &>(), "bbox"_a);
219  cls.def(py::init<std::shared_ptr<Image<PixelT>>>(), "rhs"_a);
220  cls.def(py::init<DecoratedImage<PixelT> const &, const bool>(), "rhs"_a, "deep"_a = false);
221  cls.def(py::init<std::string const &, const int, lsst::afw::geom::Box2I const &, ImageOrigin const>(),
222  "fileName"_a, "hdu"_a = INT_MIN, "bbox"_a = lsst::afw::geom::Box2I(), "origin"_a = PARENT);
223 
224  cls.def("getMetadata", &DecoratedImage<PixelT>::getMetadata);
225  cls.def("setMetadata", &DecoratedImage<PixelT>::setMetadata);
226  cls.def("getWidth", &DecoratedImage<PixelT>::getWidth);
227  cls.def("getHeight", &DecoratedImage<PixelT>::getHeight);
228  cls.def("getX0", &DecoratedImage<PixelT>::getX0);
229  cls.def("getY0", &DecoratedImage<PixelT>::getY0);
230  cls.def("getDimensions", &DecoratedImage<PixelT>::getDimensions);
231  cls.def("swap", &DecoratedImage<PixelT>::swap);
232  cls.def("writeFits", &DecoratedImage<PixelT>::writeFits, "fileName"_a,
233  "metadata"_a = std::shared_ptr<lsst::daf::base::PropertySet const>(), "mode"_a = "w");
234  cls.def("getImage", (typename std::shared_ptr<Image<PixelT>> (DecoratedImage<PixelT>::*)()) &
235  DecoratedImage<PixelT>::getImage);
236  cls.def("getGain", &DecoratedImage<PixelT>::getGain);
237  cls.def("setGain", &DecoratedImage<PixelT>::setGain);
238 }
239 
240 /* Declare ImageSlice operators separately since they are only instantiated for float double */
241 template <typename PixelT>
242 static void addImageSliceOperators(
243  py::class_<Image<PixelT>, std::shared_ptr<Image<PixelT>>, ImageBase<PixelT>> &cls) {
244  cls.def("__add__",
245  [](Image<PixelT> const &self, ImageSlice<PixelT> const &other) { return self + other; },
246  py::is_operator());
247  cls.def("__sub__",
248  [](Image<PixelT> const &self, ImageSlice<PixelT> const &other) { return self - other; },
249  py::is_operator());
250  cls.def("__mul__",
251  [](Image<PixelT> const &self, ImageSlice<PixelT> const &other) { return self * other; },
252  py::is_operator());
253  cls.def("__truediv__",
254  [](Image<PixelT> const &self, ImageSlice<PixelT> const &other) { return self / other; },
255  py::is_operator());
256  cls.def("__iadd__", [](Image<PixelT> &self, ImageSlice<PixelT> const &other) {
257  self += other;
258  return self;
259  });
260  cls.def("__isub__", [](Image<PixelT> &self, ImageSlice<PixelT> const &other) {
261  self -= other;
262  return self;
263  });
264  cls.def("__imul__", [](Image<PixelT> &self, ImageSlice<PixelT> const &other) {
265  self *= other;
266  return self;
267  });
268  cls.def("__itruediv__", [](Image<PixelT> &self, ImageSlice<PixelT> const &other) {
269  self /= other;
270  return self;
271  });
272 }
273 
274 template <typename PixelT, typename PyClass>
275 static void addGeneralizedCopyConstructors(PyClass &cls) {
276  cls.def(py::init<Image<int> const &, const bool>(), "rhs"_a, "deep"_a = false);
277  cls.def(py::init<Image<float> const &, const bool>(), "rhs"_a, "deep"_a = false);
278  cls.def(py::init<Image<double> const &, const bool>(), "rhs"_a, "deep"_a = false);
279  cls.def(py::init<Image<std::uint16_t> const &, const bool>(), "rhs"_a, "deep"_a = false);
280  cls.def(py::init<Image<std::uint64_t> const &, const bool>(), "rhs"_a, "deep"_a = false);
281 
282  cls.def("convertI", [](Image<PixelT> const &self) { return Image<int>(self, true); });
283  cls.def("convertF", [](Image<PixelT> const &self) { return Image<float>(self, true); });
284  cls.def("convertD", [](Image<PixelT> const &self) { return Image<double>(self, true); });
285  cls.def("convertU", [](Image<PixelT> const &self) { return Image<std::uint16_t>(self, true); });
286  cls.def("convertL", [](Image<PixelT> const &self) { return Image<std::uint64_t>(self, true); });
287 
288  cls.def("convertFloat", [](Image<PixelT> const &self) { return Image<float>(self, true); });
289  cls.def("convertDouble", [](Image<PixelT> const &self) { return Image<double>(self, true); });
290 }
291 
293  py::module mod("image");
294 
295  if (_import_array() < 0) {
296  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
297  return nullptr;
298  }
299 
300  py::enum_<ImageOrigin>(mod, "ImageOrigin")
301  .value("PARENT", ImageOrigin::PARENT)
302  .value("LOCAL", ImageOrigin::LOCAL)
303  .export_values();
304 
305  declareImageBase<int>(mod, "I");
306  declareImageBase<float>(mod, "F");
307  declareImageBase<double>(mod, "D");
308  declareImageBase<std::uint16_t>(mod, "U");
309  declareImageBase<std::uint64_t>(mod, "L");
310 
311  auto clsImageI = declareImage<int>(mod, "I");
312  auto clsImageF = declareImage<float>(mod, "F");
313  auto clsImageD = declareImage<double>(mod, "D");
314  auto clsImageU = declareImage<std::uint16_t>(mod, "U");
315  auto clsImageL = declareImage<std::uint64_t>(mod, "L");
316 
317  // Add generalized copy constructors
318  addGeneralizedCopyConstructors<int>(clsImageI);
319  addGeneralizedCopyConstructors<float>(clsImageF);
320  addGeneralizedCopyConstructors<double>(clsImageD);
321  addGeneralizedCopyConstructors<std::uint16_t>(clsImageU);
322  addGeneralizedCopyConstructors<std::uint64_t>(clsImageL);
323 
324  // Add slice operators only for float and double
325  addImageSliceOperators<float>(clsImageF);
326  addImageSliceOperators<double>(clsImageD);
327 
328  declareDecoratedImage<int>(mod, "I");
329  declareDecoratedImage<float>(mod, "F");
330  declareDecoratedImage<double>(mod, "D");
331  declareDecoratedImage<std::uint16_t>(mod, "U");
332  declareDecoratedImage<std::uint64_t>(mod, "L");
333 
334  // Declare constructors for casting all exposure types to to float and double
335  // (the only two types of casts that Python supports)
336  declareCastConstructor<int, float>(clsImageF);
337  declareCastConstructor<int, double>(clsImageD);
338 
339  declareCastConstructor<float, double>(clsImageD);
340 
341  declareCastConstructor<double, float>(clsImageF);
342 
343  declareCastConstructor<std::uint16_t, float>(clsImageF);
344  declareCastConstructor<std::uint16_t, double>(clsImageD);
345 
346  declareCastConstructor<std::uint64_t, float>(clsImageF);
347  declareCastConstructor<std::uint64_t, double>(clsImageD);
348 
349  mod.def("bboxFromMetadata", &bboxFromMetadata);
350 
351  return mod.ptr();
352 }
353 }
354 }
355 }
356 } // namespace lsst::afw::image::<anonymous>
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:55
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
Extent< int, 2 > Extent2I
Definition: Extent.h:341
A Function taking two arguments.
Definition: Function.h:266
An integer coordinate rectangle.
Definition: Box.h:55
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
double const _a
Definition: RandomImage.cc:77
A class used to request that array accesses be checked.
Definition: ImageBase.h:78
geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
Definition: Image.cc:672
double x
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:442
afw::table::Key< afw::table::Array< ImagePixelT > > image
Point< int, 2 > Point2I
Definition: Point.h:270