SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
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
30namespace SourceXtractor {
31
33 double pixel_scale, std::shared_ptr<VectorImage<SeFloat>> image, double down_scaling, bool normalize_psf)
34 : m_down_scaling(down_scaling), m_normalize_psf(normalize_psf) {
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 if (m_normalize_psf) {
56 auto psf_sum = std::accumulate(new_image->getData().begin(), new_image->getData().end(), 0.);
57 for (auto& pixel : new_image->getData()) {
58 pixel /= psf_sum;
59 }
60 } else {
61 auto original_psf_sum = std::accumulate(image->getData().begin(), image->getData().end(), 0.);
62 auto new_psf_sum = std::accumulate(new_image->getData().begin(), new_image->getData().end(), 0.);
63
64 for (auto& pixel : new_image->getData()) {
65 pixel *= original_psf_sum / new_psf_sum;
66 }
67 }
68
69 m_psf = std::make_shared<ImagePsf>(pixel_scale / down_scaling, new_image);
70 } else {
72 }
73 } else {
74 m_down_scaling = 1.0;
75 }
76}
77
79 if (m_psf != nullptr) {
80 return m_psf->getPixelScale();
81 } else {
82 return 1.0;
83 }
84}
85
87 if (m_psf != nullptr) {
88 return m_psf->getWidth();
89 } else {
90 return 1;
91 }
92}
93
95 if (m_psf != nullptr) {
96 return m_psf->getScaledKernel(scale);
97 } else {
98 return nullptr;
99 }
100}
101
103 if (m_psf != nullptr) {
104 m_psf->convolve(image);
105 }
106}
107
109 const std::shared_ptr<const Image<SeFloat>>& model_ptr) const {
110 if (m_psf != nullptr) {
111 return m_psf->prepare(model_ptr);
112 } else {
113 return nullptr;
114 }
115}
116
119 if (m_psf != nullptr) {
120 m_psf->convolve(image, context);
121 }
122}
123
124
125}
126
const double pixel_scale
Definition TestImage.cpp:74
T accumulate(T... args)
std::shared_ptr< ImagePsf > m_psf
DownSampledImagePsf(double pixel_scale, std::shared_ptr< VectorImage< SeFloat > > image, double down_scaling=1.0, bool normalize_psf=true)
std::shared_ptr< VectorImage< SourceXtractor::SeFloat > > getScaledKernel(SeFloat scale) const
std::unique_ptr< DFTConvolution< SeFloat >::ConvolutionContext > prepare(const std::shared_ptr< const Image< SeFloat > > &model_ptr) const
void convolve(std::shared_ptr< WriteableImage< float > > image) const
Interface representing an image.
Definition Image.h:44
Image implementation which keeps the pixel values in memory.
Definition VectorImage.h:52
T make_shared(T... args)
SeFloat32 SeFloat
Definition Types.h:32
ModelFitting::ImageTraits< ImageInterfaceTypePtr > Traits