SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DownSampledImagePsf.cpp
Go to the documentation of this file.
1 
17 /*
18  * DownSampledImagePsf.cpp
19  *
20  * Created on: Nov 9, 2021
21  * Author: mschefer
22  */
23 
24 #include <numeric>
25 #include <iostream>
26 
29 
30 namespace SourceXtractor {
31 
33  double pixel_scale, std::shared_ptr<VectorImage<SeFloat>> image, double down_scaling)
34  : m_down_scaling(down_scaling) {
35  if (image != nullptr) {
37 
38  if (image->getWidth() != image->getHeight()) {
39  throw Elements::Exception() << "PSF kernel must be square but was "
40  << image->getWidth() << " x " << image->getHeight();
41  }
42  if (image->getWidth() % 2 == 0) {
43  throw Elements::Exception() << "PSF kernel must have odd size, but got "
44  << image->getWidth();
45  }
46 
47  if (down_scaling != 1.0) {
48  int new_size = image->getWidth() * down_scaling;
49  new_size += (new_size % 2 == 0) ? 1 : 0;
50 
51  auto new_image = Traits::factory(new_size, new_size);
52  Traits::addImageToImage(new_image, image, down_scaling, new_size / 2.0, new_size / 2.0);
53 
54  // renormalize psf
55  auto psf_sum = std::accumulate(new_image->getData().begin(), new_image->getData().end(), 0.);
56  for (auto& pixel : new_image->getData()) {
57  pixel /= psf_sum;
58  }
59 
60  m_psf = std::make_shared<ImagePsf>(pixel_scale / down_scaling, new_image);
61  } else {
62  m_psf = std::make_shared<ImagePsf>(pixel_scale, image);
63  }
64  } else {
65  m_down_scaling = 1.0;
66  }
67 }
68 
70  if (m_psf != nullptr) {
71  return m_psf->getPixelScale();
72  } else {
73  return 1.0;
74  }
75 }
76 
78  if (m_psf != nullptr) {
79  return m_psf->getWidth();
80  } else {
81  return 1;
82  }
83 }
84 
86  if (m_psf != nullptr) {
87  return m_psf->getScaledKernel(scale);
88  } else {
89  return nullptr;
90  }
91 }
92 
94  if (m_psf != nullptr) {
95  m_psf->convolve(image);
96  }
97 }
98 
100  const std::shared_ptr<const Image<SeFloat>>& model_ptr) const {
101  if (m_psf != nullptr) {
102  return m_psf->prepare(model_ptr);
103  } else {
104  return nullptr;
105  }
106 }
107 
110  if (m_psf != nullptr) {
111  m_psf->convolve(image, context);
112  }
113 }
114 
115 
116 }
117 
DownSampledImagePsf(double pixel_scale, std::shared_ptr< VectorImage< SeFloat >> image, double down_scaling=1.0)
std::shared_ptr< VectorImage< SourceXtractor::SeFloat > > getScaledKernel(SeFloat scale) const
SeFloat32 SeFloat
Definition: Types.h:32
Image implementation which keeps the pixel values in memory.
Definition: VectorImage.h:52
std::unique_ptr< DFTConvolution< SeFloat >::ConvolutionContext > prepare(const std::shared_ptr< const Image< SeFloat >> &model_ptr) const
STL class.
std::shared_ptr< ImagePsf > m_psf
static ImageInterfaceTypePtr factory(std::size_t width, std::size_t height)
Interface representing an image.
Definition: Image.h:43
T accumulate(T...args)
const double pixel_scale
Definition: TestImage.cpp:74
static void addImageToImage(ImageInterfaceTypePtr &target_image, const ImageInterfaceTypePtr &source_image, double scale_factor, double x, double y)
void convolve(std::shared_ptr< WriteableImage< float >> image) const