SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Example4.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;
49 
50 int main(int argc, char **argv) {
51  std::string engine_impl("levmar");
52  if (argc > 1) {
53  engine_impl = argv[1];
54  }
55 
56  auto frames_path = Elements::pathSearchInEnvVariable("multiframe.fits", "ELEMENTS_AUX_PATH");
57  auto frames = readFrames(frames_path[0].string());
58 
59  //
60  // Model creation
61  //
62  // The frame model we will use will contain a single extended model, with a
63  // single exponential component.
64 
65  // First we define the parameters of the exponential. We are going to minimize
66  // only the I0, so it is the only EngineParameter. For the engine parameters
67  // we need to use a coordinate converter. The options are:
68  // - NeutralConverter : Does no conversion
69  // - NormalizedConverter : Normalizes the parameter so the engine value is 1
70  // for a specific world value
71  // - SigmoidConverter : Converts the parameter using the sigmoid function
72  // - ExpSigmoidConverter : Converts the parameter using the exponential sigmoid function
73  auto i0 = std::make_shared<EngineParameter>(12., make_unique<ExpSigmoidConverter>(1, 100));
74  auto n = std::make_shared<ManualParameter>(1.);
75  auto k = std::make_shared<ManualParameter>(1.);
76 
77  // We create the component list of the extended model with the single exponential
78  auto reg_man = make_unique<OnlySmooth>();
79  auto exp = make_unique<SersicModelComponent>(move(reg_man), i0, n, k);
80  vector<unique_ptr<ModelComponent>> component_list {};
81  component_list.emplace_back(move(exp));
82 
83  // We create the extended model. All of its parameters will be optimized by
84  // the minimization engine.
85  auto x = std::make_shared<EngineParameter>(10, make_unique<NormalizedConverter>(30.));
86  auto y = std::make_shared<EngineParameter>(20, make_unique<NormalizedConverter>(30.));
87  auto x_scale = std::make_shared<EngineParameter>(.5, make_unique<SigmoidConverter>(0, 1));
88  auto y_scale = std::make_shared<EngineParameter>(.5, make_unique<SigmoidConverter>(0, 1));
89  auto rot_angle = std::make_shared<EngineParameter>(2., make_unique<SigmoidConverter>(0, 2*M_PI));
90 
91  // The size of the extended model (??? from the detection step ???)
92  double width = 10;
93  double height = 10;
94 
95  // We create the extended model list with a single model
96  vector<TransformedModel> extended_models {};
97  extended_models.emplace_back(std::move(component_list), x_scale, y_scale,
98  rot_angle, width, height, x, y);
99 
100  // We read the PSF from the file
101  auto psf_path = Elements::pathSearchInEnvVariable("psf.fits", "ELEMENTS_AUX_PATH");
102  auto psf = readPsf(psf_path[0].string());
103 
104  ResidualEstimator res_estimator {};
105 
106  for (auto& pair : frames) {
107  cv::Mat image;
108  double pixel_scale {};
109  tie(image, pixel_scale) = pair;
110 
111  FrameModel<OpenCvPsf, cv::Mat> frame_model {
112  pixel_scale, (size_t)image.cols, (size_t)image.rows, {}, {},
113  move(extended_models), move(psf)
114  };
115 
116  cv::Mat weight = cv::Mat::ones(image.rows, image.cols, CV_64F);
117  auto data_vs_model = createDataVsModelResiduals(std::move(image), std::move(frame_model),
118  std::move(weight), LogChiSquareComparator{});
119  res_estimator.registerBlockProvider(move(data_vs_model));
120  }
121 
122  //
123  // Minimization
124  //
125 
126  // We print the parameters before the minimization for comparison
127  cout << "I0 (12) = " << i0->getValue() << '\n';
128  cout << "X (14.5) = " << x->getValue() << '\n';
129  cout << "Y (15.3) = " << y->getValue() << '\n';
130  cout << "X_SCALE (.83) = " << x_scale->getValue() << '\n';
131  cout << "Y_SCALE (.25) = " << y_scale->getValue() << '\n';
132  cout << "angle (2.3) = " << rot_angle->getValue() << '\n';
133 
134  // First we need to specify which parameters are optimized by the engine
135  EngineParameterManager manager {};
136  manager.registerParameter(i0);
137  manager.registerParameter(x);
138  manager.registerParameter(y);
139  manager.registerParameter(x_scale);
140  manager.registerParameter(y_scale);
141  manager.registerParameter(rot_angle);
142 
143  // Finally we create a least square engine and we solve the problem
144  std::cout << "Registered engines: " << std::endl;
145  for (auto &e : LeastSquareEngineManager::getImplementations()) {
146  std::cout << '\t' << e << std::endl;
147  }
148  std::cout << "Using engine " << engine_impl << std::endl;
149  auto engine = LeastSquareEngineManager::create(engine_impl);
150  auto t1 = chrono::steady_clock::now();
151  auto solution = engine->solveProblem(manager, res_estimator);
152  auto t2 = chrono::steady_clock::now();
153 
154  // We print the results
155  cout << "\nTime of fitting: " << chrono::duration <double, milli> (t2-t1).count() << " ms" << endl;
156  cout << "\n";
157 
158  cout << "I0 (12) = " << i0->getValue() << '\n';
159  cout << "X (14.5) = " << x->getValue() << '\n';
160  cout << "Y (15.3) = " << y->getValue() << '\n';
161  cout << "X_SCALE (.83) = " << x_scale->getValue() << '\n';
162  cout << "Y_SCALE (.25) = " << y_scale->getValue() << '\n';
163  cout << "angle (2.3) = " << rot_angle->getValue() << '\n';
164 
165  printLevmarInfo(boost::any_cast<array<double,10>>(solution.underlying_framework_info));
166 
167 }
T tie(T...args)
T exp(T...args)
int main()
Definition: Example1.cpp:47
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
constexpr double e
T endl(T...args)
ModelFitting::OpenCvPsf readPsf(const std::string &filename)
Definition: utils.h:53
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
STL class.
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)
std::vector< std::pair< cv::Mat, double > > readFrames(const std::string &filename)
Definition: utils.h:95
STL class.
Class responsible for managing the parameters the least square engine minimizes.
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)
void printLevmarInfo(std::array< double, 10 > info)
Definition: utils.h:118
Provides to the LeastSquareEngine the residual values.
const double pixel_scale
Definition: TestImage.cpp:72
Data vs model comparator which computes a modified residual.
T emplace_back(T...args)