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