SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Example1.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <opencv2/opencv.hpp>
33 #include "utils.h"
34 
35 using namespace std;
36 using namespace ModelFitting;
37 
38 //
39 // This example demonstrates the following:
40 //
41 // - How to create parameters
42 // - How to create extended model components from a Profile
43 // - How to use decorators for scaling and rotating a model component
44 // - How to create an ExtendedModel from its model components
45 //
46 
47 int main() {
48 
49  //
50  // Creation of the first model component (exponential profile)
51  //
52 
53  // First we need to create the parameters the profile will use. We need the
54  // three parameters of the Sersic profile.
55  auto exp_i0 = std::make_shared<ManualParameter>(12.6);
56  auto exp_n = std::make_shared<ManualParameter>(1.);
57  auto exp_k = std::make_shared<ManualParameter>(1.);
58 
59  // We want to create a CircularlySymmetricModelComponent. For this reason we
60  // need a SharpRegionManager, which describes how the sharp region is computed.
61  // There are three different options:
62  // - OnlySmooth : The profile is treated as there is no sharp region.
63  // - OldSharp : The behavior of the SExtractor2. The profile MUST be zero
64  // outside the sharp radius.
65  // - AutoSharp : The sharp region is automatically calculated.
66  //
67  // For the exponential profile we know that it is smooth enough, so we use
68  // the OnlySmooth SharpRegionManager. This is increasing the performance.
69  auto exp_reg_man = make_unique<OnlySmooth>();
70 
71  // Here we create the CircularlySymmetricModelComponent. The profile type is
72  // a template parameter of the class and we want to use a SersicProfile. That
73  // means we should use the type CircularlySymmetricModelComponent<SersicProfile>.
74  // For simplicity, an alias of this type is pre-defined with the name
75  // SersicModelComponent.
76  auto exp = make_unique<SersicModelComponent>(move(exp_reg_man), exp_i0, exp_n, exp_k);
77 
78  // We use the ScaledModelComponent decorator for applying the axes scaling.
79  // For this we need two more parameters describing the scaling factors.
80  auto x_scale = std::make_shared<ManualParameter>(2.);
81  auto y_scale = std::make_shared<ManualParameter>(.5);
82  auto scaled_exp = make_unique<ScaledModelComponent>(move(exp), x_scale, y_scale);
83 
84  // Similarly we use the RotatedModelComponent decorator to rotate the already
85  // scaled component by 30 degrees
86  auto exp_rot_angle = std::make_shared<ManualParameter>(M_PI / 6.);
87  auto rotated_exp = make_unique<RotatedModelComponent>(move(scaled_exp), exp_rot_angle);
88 
89  //
90  // Creation of the second model component (De Vaucouleurs profile)
91  //
92 
93  // We perform the same steps with above to create the second model component,
94  // with the following differences:
95  // - We use the De Vaucouleurs profile instead of the exponential
96  // - We use the AutoSharp SharpRegionManager because the center of the profile
97  // is too sharp
98  // - We rotate it by 30 degrees to the opposite direction
99  // Note that because we use the same scaling factors we reuse the same parameters.
100  auto dev_i0 = std::make_shared<ManualParameter>(525.3);
101  auto dev_n = std::make_shared<ManualParameter>(4.);
102  auto dev_k = std::make_shared<ManualParameter>(7.66924944);
103  auto dev_reg_man = make_unique<AutoSharp>();
104  auto dev = make_unique<SersicModelComponent>(move(dev_reg_man), dev_i0, dev_n, dev_k);
105  auto scaled_dev = make_unique<ScaledModelComponent>(move(dev), x_scale, y_scale);
106  auto dev_rot_angle = std::make_shared<ManualParameter>(-M_PI / 6.);
107  auto rotated_dev = make_unique<RotatedModelComponent>(move(scaled_dev), dev_rot_angle);
108 
109  //
110  // Creation of the extended model
111  //
112 
113  // First we need to create the parameters required by the extended model. These
114  // are the following:
115  // - The x and y position (in pixels) at the frame. We do not care about their
116  // values in this example.
117  // - The x and y scale of the extended model. It is applied to all the components.
118  // We do not apply any scaling at this example.
119  // - The rotation of the extended model. It is applied to all the components.
120  // - The size (in arcsec) of the model (???from the detection step???)
121  auto x = std::make_shared<ManualParameter>(0);
122  auto y = std::make_shared<ManualParameter>(0);
123  auto model_scale = std::make_shared<ManualParameter>(1.);
124  auto model_angle = std::make_shared<ManualParameter>(M_PI / 4.);
125  double width = 10.;
126  double height = 10.;
127 
128  // To create the extended model we first create a vector with its model components
129  vector<unique_ptr<ModelComponent>> component_list {};
130  component_list.emplace_back(move(rotated_exp));
131  component_list.emplace_back(move(rotated_dev));
132 
133  // We finally create the extended model
134  ExtendedModel extended_model {move(component_list), model_scale, model_scale,
135  model_angle, width, height, x, y};
136 
137  //
138  // Model demonstration
139  //
140 
141  // As an example of using the extended model we are using its method for computing
142  // its rasterized image. As parameters we pass the pixel scale, the width and
143  // the height of the image we want. Note that the dimensions have to be odd
144  // numbers and that the model is always centered at the center of the image.
145  //
146  // The template parameter controls the type of the image the method returns.
147  // It can be any type for which a specialization of the ImageTraits templated
148  // class is provided. For the cv::Mat this specialization is defined in the
149  // ModelFitting/Image/OpenCvMatImageTraits.h file, which is included at the
150  // top of this file.
151  auto image = extended_model.getRasterizedImage<cv::Mat>(.1, 201, 301);
152  writeToFits(image, "example1.fits");
153 
154 }
T exp(T...args)
int main()
Definition: Example1.cpp:47
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T move(T...args)
void writeToFits(const cv::Mat &image, const std::string &filename)
Definition: utils.h:40
STL class.
T emplace_back(T...args)