SourceXtractorPlusPlus  0.11
Please provide a description of the project.
Example_gal.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <tuple>
25 #include <vector>
44 #include "utils.h"
46 
47 using namespace std;
48 using namespace ModelFitting;
50 
51 // This example demonstrates how to use the DataVsModelResiduals to perform
52 // minimization over an observed image and a FrameModel. The real parameters
53 // are:
54 // - I0 : 12.
55 // - X : 128
56 // - Y : 128
57 // - X_SCALE : 0.83
58 // - Y_SCALE : 0.25
59 // - ROT_ANGLE : 2.3
60 
61 int main(int argc, char **argv) {
62  std::string engine_impl("levmar");
63  if (argc > 1) {
64  engine_impl = argv[1];
65  }
66 
67  // We read the image from the aux dir. Note that we will use a cv:Mat type,
68  // so the ModelFitting/Image/OpenCvMatImageTraits.h must be included.
69  cv::Mat image;
70  double pixel_scale {};
71  auto image_path = Elements::pathSearchInEnvVariable("gal.fits", "ELEMENTS_AUX_PATH");
72  tie(image, pixel_scale) = readImage(image_path[0].string());
73  size_t image_cols = image.cols;
74  size_t image_rows = image.rows;
75 
76  //
77  // Model creation
78  //
79  // The frame model we will use will contain a single extended model, with a
80  // single exponential component.
81 
82  // First we define the parameters of the exponential. We are going to minimize
83  // only the I0, so it is the only EngineParameter. For the engine parameters
84  // we need to use a coordinate converter. The options are:
85  // - NeutralConverter : Does no conversion
86  // - NormalizedConverter : Normalizes the parameter so the engine value is 1
87  // for a specific world value
88  // - SigmoidConverter : Converts the parameter using the sigmoid function
89  // - ExpSigmoidConverter : Converts the parameter using the exponential sigmoid function
90  auto i0 = std::make_shared<EngineParameter>(50000., make_unique<ExpSigmoidConverter>(1, 1000000.));
91  auto n = std::make_shared<ManualParameter>(1.);
92  auto k = std::make_shared<ManualParameter>(1.);
93 
94  // We create the component list of the extended model with the single exponential
95  auto reg_man = make_unique<OnlySmooth>();
96  auto exp = make_unique<SersicModelComponent>(move(reg_man), i0, n, k);
97  vector<unique_ptr<ModelComponent>> component_list {};
98  component_list.emplace_back(move(exp));
99 
100  // We create the extended model. All of its parameters will be optimized by
101  // the minimization engine.
102  auto x = std::make_shared<EngineParameter>(120, make_unique<NormalizedConverter>(1500.));
103  auto y = std::make_shared<EngineParameter>(140, make_unique<NormalizedConverter>(1500.));
104  auto x_scale = std::make_shared<EngineParameter>(1.0, make_unique<SigmoidConverter>(0, 10.));
105  auto y_scale = std::make_shared<EngineParameter>(1.0, make_unique<SigmoidConverter>(0, 10.));
106  auto rot_angle = std::make_shared<EngineParameter>(20.0 * M_PI/180.0, make_unique<SigmoidConverter>(0, 2*M_PI));
107 
108  // The size of the extended model (??? from the detection step ???)
109  double width = 128;
110  double height = 128;
111 
112  // We create the extended model list with a single model
114  extended_models.emplace_back(std::make_shared<ExtendedModel<cv::Mat>>(std::move(component_list), x_scale, y_scale,
115  rot_angle, width, height, x, y));
116 
117  // We add a constant background
118  auto back = std::make_shared<EngineParameter>(100., make_unique<ExpSigmoidConverter>(1, 1000000.));
119  vector<ConstantModel> constant_models {};
120  constant_models.emplace_back(back);
121 
122  // We read the PSF from the file
123  auto psf_path = Elements::pathSearchInEnvVariable("psf_gal.fits", "ELEMENTS_AUX_PATH");
124  auto psf = readPsf(psf_path[0].string());
125 
126  // Finally we create the FrameModel with same pixel scale and size as the
127  // input image
128  FrameModel<OpenCvPsf, cv::Mat> frame_model {
129  pixel_scale, image_cols, image_rows, move(constant_models), {},
130  move(extended_models), move(psf)
131  };
132 
133  writeToFits(frame_model.getImage(), "example3b.fits");
134 
135  //
136  // Minimization
137  //
138 
139  // First we need to specify which parameters are optimized by the engine
140  EngineParameterManager manager {};
141  manager.registerParameter(i0);
142  manager.registerParameter(x);
143  manager.registerParameter(y);
144  manager.registerParameter(x_scale);
145  manager.registerParameter(y_scale);
146  manager.registerParameter(rot_angle);
147  manager.registerParameter(back);
148 
149  // Now we need to create the DataVsModelResiduals. We will set all the weights
150  // as ones and we will use the LogChiSquareComparator.
151  // Note that because we use cv::Mat as input we have to include the file
152  // ModelFitting/Engine/OpenCvDataVsModelInputTraits.h
153  cv::Mat weight = cv::Mat::ones(image.rows, image.cols, CV_64F);
154  auto data_vs_model = createDataVsModelResiduals(std::move(image), std::move(frame_model),
155  std::move(weight), LogChiSquareComparator{});
156 
157  // We create a residual estimator and we add our block provider
158  ResidualEstimator res_estimator {};
159  res_estimator.registerBlockProvider(move(data_vs_model));
160 
161  // We print the parameters before the minimization for comparison
162  cout << "I0 = " << i0->getValue() << '\n';
163  cout << "X = " << x->getValue() << '\n';
164  cout << "Y = " << y->getValue() << '\n';
165  cout << "X_SCALE = " << x_scale->getValue() << '\n';
166  cout << "Y_SCALE = " << y_scale->getValue() << '\n';
167  cout << "angle = " << rot_angle->getValue() << '\n';
168  cout << "Background = " << back->getValue() << '\n';
169 
170  // Finally we create a levmar engine and we solve the problem
171  auto engine = LeastSquareEngineManager::create(engine_impl);
172  auto t1 = chrono::steady_clock::now();
173  auto solution = engine->solveProblem(manager, res_estimator);
174  auto t2 = chrono::steady_clock::now();
175 
176  // We print the results
177  cout << "\nTime of fitting: " << chrono::duration <double, milli> (t2-t1).count() << " ms" << endl;
178  cout << "\n";
179 
180  cout << "I0 = " << i0->getValue() << '\n';
181  cout << "X = " << x->getValue() << '\n';
182  cout << "Y = " << y->getValue() << '\n';
183  cout << "X_SCALE = " << x_scale->getValue() << '\n';
184  cout << "Y_SCALE = " << y_scale->getValue() << '\n';
185  cout << "angle = " << rot_angle->getValue() << '\n';
186  cout << "Background = " << back->getValue() << '\n';
187 
188  printLevmarInfo(boost::any_cast<array<double,10>>(solution.underlying_framework_info));
189 
190  // We create the component list of the extended model with the single exponential
191  reg_man = make_unique<OnlySmooth>();
192  exp = make_unique<SersicModelComponent>(move(reg_man), i0, n, k);
193  component_list.clear();
194  component_list.emplace_back(move(exp));
195  extended_models.clear();
196  extended_models.emplace_back(std::make_shared<ExtendedModel<cv::Mat>>(move(component_list),x_scale, y_scale,
197  rot_angle, width, height, x, y));
198  constant_models.clear();
199  constant_models.emplace_back(back);
200  FrameModel<OpenCvPsf, cv::Mat> frame_model_after {
201  pixel_scale, image_cols, image_rows, move(constant_models), {},
202  move(extended_models), readPsf(psf_path[0].string())
203  };
204  writeToFits(frame_model_after.getImage(), "example3b2.fits");
205 
206 }
void printLevmarInfo(std::array< double, 10 > info)
Definition: utils.h:118
void writeToFits(const cv::Mat &image, const std::string &filename)
Definition: utils.h:40
T tie(T... args)
T exp(T... args)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
T endl(T... args)
STL namespace.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
STL class.
void registerBlockProvider(std::unique_ptr< ResidualBlockProvider > provider)
Registers a ResidualBlockProvider to the ResidualEstimator.
void registerParameter(std::shared_ptr< EngineParameter > parameter)
Registers an EngineParameter to the EngineParameterManager.
T move(T... args)
T count(T... args)
STL class.
ELEMENTS_API std::vector< boost::filesystem::path > pathSearchInEnvVariable(const std::string &file_name, const std::string &path_like_env_variable, SearchType search_type=SearchType::Recursive)
T make_shared(T... args)
std::unique_ptr< DataVsModelResiduals< typename std::remove_reference< DataType >::type, typename std::remove_reference< ModelType >::type, typename std::remove_reference< WeightType >::type, typename std::remove_reference< Comparator >::type > > createDataVsModelResiduals(DataType &&data, ModelType &&model, WeightType &&weight, Comparator &&comparator)
STL class.
Class responsible for managing the parameters the least square engine minimizes.
ModelFitting::OpenCvPsf readPsf(const std::string &filename)
Definition: utils.h:53
Provides to the LeastSquareEngine the residual values.
std::pair< cv::Mat, double > readImage(const std::string &filename)
Definition: utils.h:77
const double pixel_scale
Definition: TestImage.cpp:75
Data vs model comparator which computes a modified residual.
std::unique_ptr< T > make_unique(Args &&... args)
T emplace_back(T... args)
int main(int argc, char **argv)
Definition: Example_gal.cpp:61