SourceXtractorPlusPlus  0.10
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Example3.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 : 14.5
56 // - Y : 15.3
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("image.fits", "ELEMENTS_AUX_PATH");
72  tie(image, pixel_scale) = readImage(image_path[0].string());
73 
74  //
75  // Model creation
76  //
77  // The frame model we will use will contain a single extended model, with a
78  // single exponential component.
79 
80  // First we define the parameters of the exponential. We are going to minimize
81  // only the I0, so it is the only EngineParameter. For the engine parameters
82  // we need to use a coordinate converter. The options are:
83  // - NeutralConverter : Does no conversion
84  // - NormalizedConverter : Normalizes the parameter so the engine value is 1
85  // for a specific world value
86  // - SigmoidConverter : Converts the parameter using the sigmoid function
87  // - ExpSigmoidConverter : Converts the parameter using the exponential sigmoid function
88  auto i0 = std::make_shared<EngineParameter>(1., make_unique<ExpSigmoidConverter>(1, 100));
89  auto n = std::make_shared<ManualParameter>(1.);
90  auto k = std::make_shared<ManualParameter>(1.);
91 
92  // We create the component list of the extended model with the single exponential
93  auto reg_man = make_unique<OnlySmooth>();
94  auto exp = make_unique<SersicModelComponent>(move(reg_man), i0, n, k);
95  vector<unique_ptr<ModelComponent>> component_list {};
96  component_list.emplace_back(move(exp));
97 
98  // We create the extended model. All of its parameters will be optimized by
99  // the minimization engine.
100  auto x = std::make_shared<EngineParameter>(10, make_unique<NormalizedConverter>(150.));
101  auto y = std::make_shared<EngineParameter>(20, make_unique<NormalizedConverter>(150.));
102  auto x_scale = std::make_shared<EngineParameter>(.5, make_unique<SigmoidConverter>(0, 1));
103  auto y_scale = std::make_shared<EngineParameter>(.5, make_unique<SigmoidConverter>(0, 1));
104  auto rot_angle = std::make_shared<EngineParameter>(2., make_unique<SigmoidConverter>(0, 2*M_PI));
105 
106  // The size of the extended model (??? from the detection step ???)
107  double width = 10;
108  double height = 10;
109 
110  // We create the extended model list with a single model
112  extended_models.emplace_back(std::make_shared<ExtendedModel<cv::Mat>>(std::move(component_list), x_scale, y_scale,
113  rot_angle, width, height, x, y));
114 
115  // We read the PSF from the file
116  auto psf_path = Elements::pathSearchInEnvVariable("psf.fits", "ELEMENTS_AUX_PATH");
117  auto psf = readPsf(psf_path[0].string());
118 
119  // Finally we create the FrameModel with same pixel scale and size as the
120  // input image
121  FrameModel<OpenCvPsf, cv::Mat> frame_model {
122  pixel_scale, (size_t)image.cols, (size_t)image.rows, {}, {},
123  move(extended_models), move(psf)
124  };
125 
126  //
127  // Minimization
128  //
129 
130  // First we need to specify which parameters are optimized by the engine
131  EngineParameterManager manager {};
132  manager.registerParameter(i0);
133  manager.registerParameter(x);
134  manager.registerParameter(y);
135  manager.registerParameter(x_scale);
136  manager.registerParameter(y_scale);
137  manager.registerParameter(rot_angle);
138 
139  // Now we need to create the DataVsModelResiduals. We will set all the weights
140  // as ones and we will use the LogChiSquareComparator.
141  // Note that because we use cv::Mat as input we have to include the file
142  // ModelFitting/Engine/OpenCvDataVsModelInputTraits.h
143  cv::Mat weight = cv::Mat::ones(image.rows, image.cols, CV_64F);
144  auto data_vs_model = createDataVsModelResiduals(std::move(image), std::move(frame_model),
145  std::move(weight), LogChiSquareComparator{});
146 
147  // We create a residual estimator and we add our block provider
148  ResidualEstimator res_estimator {};
149  res_estimator.registerBlockProvider(move(data_vs_model));
150 
151  // We print the parameters before the minimization for comparison
152  cout << "I0 (12) = " << i0->getValue() << '\n';
153  cout << "X (14.5) = " << x->getValue() << '\n';
154  cout << "Y (15.3) = " << y->getValue() << '\n';
155  cout << "X_SCALE (.83) = " << x_scale->getValue() << '\n';
156  cout << "Y_SCALE (.25) = " << y_scale->getValue() << '\n';
157  cout << "angle (2.3) = " << rot_angle->getValue() << '\n';
158 
159  // Finally we create a levmar engine and we solve the problem
160  auto engine = LeastSquareEngineManager::create(engine_impl);
161  auto t1 = chrono::steady_clock::now();
162  auto solution = engine->solveProblem(manager, res_estimator);
163  auto t2 = chrono::steady_clock::now();
164 
165  // We print the results
166  cout << "\nTime of fitting: " << chrono::duration <double, milli> (t2-t1).count() << " ms" << endl;
167  cout << "\n";
168 
169  cout << "I0 (12) = " << i0->getValue() << '\n';
170  cout << "X (14.5) = " << x->getValue() << '\n';
171  cout << "Y (15.3) = " << y->getValue() << '\n';
172  cout << "X_SCALE (.83) = " << x_scale->getValue() << '\n';
173  cout << "Y_SCALE (.25) = " << y_scale->getValue() << '\n';
174  cout << "angle (2.3) = " << rot_angle->getValue() << '\n';
175 
176  printLevmarInfo(boost::any_cast<array<double,10>>(solution.underlying_framework_info));
177 
178 }
void printLevmarInfo(std::array< double, 10 > info)
Definition: utils.h:118
T tie(T...args)
T exp(T...args)
int main()
Definition: Example1.cpp:48
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
T endl(T...args)
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< T > make_unique(Args &&...args)
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)
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.
T emplace_back(T...args)