11 namespace ModelFitting {
13 template<
typename ImageType>
26 template<
typename ImageType>
29 #if defined(__APPLE__)
31 #elif defined(_GNU_SOURCE)
46 return scale * rotation * m_inv_jacobian *
pixel_scale;
49 template<
typename ImageType>
50 template<
typename ModelEvaluator>
54 float x_model = (x - 0.5 + (ix+1) * 1.0 / (subsampling+1));
56 float y_model = (y - 0.5 + (iy+1) * 1.0 / (subsampling+1));
57 acc += model_eval.evaluateModel(x_model, y_model);
61 return acc / (subsampling*subsampling);
64 template<
typename ImageType>
65 template<
typename ModelEvaluator>
71 for (
unsigned int i=0; i<samples; i++) {
72 acc += model_eval.evaluateModel(
double(x) + distribution(generator),
double(y) + distribution(generator));
80 template<
typename ImageType>
81 template<
typename ModelEvaluator>
83 unsigned int steps[] = {1,3,5,7,11,15,23,31,47,63,95,127};
84 float value = samplePixel(model_eval, x,y, 1);
85 for (
unsigned int i=2; i < (
sizeof(steps)/
sizeof(steps[0])) && steps[i] <= max_subsampling; i++) {
86 float newValue = samplePixel(model_eval, x,y, steps[i] + (max_subsampling % 2));
88 double diff =
fabs(newValue - value);
89 if (diff <= threshold * value) {
101 template<
typename ImageType>
103 double a = (x2-x1) * y1 - (y2-y1) * x1;
104 return a * a / ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
107 template<
typename ImageType>
112 double x = size_x * 0.95f / 2.f;
113 double y = size_y * 0.95f / 2.f;
115 double p0_x = x * transform[0] + y * transform[1];
116 double p0_y = x * transform[2] + y * transform[3];
117 double p1_x = -x * transform[0] + y * transform[1];
118 double p1_y = -x * transform[2] + y * transform[3];
119 double p2_x = x * transform[0] - y * transform[1];
120 double p2_y = x * transform[2] - y * transform[3];
124 return std::min(computeSqrDistanceLineToOrigin(p0_x, p0_y, p1_x, p1_y),
125 computeSqrDistanceLineToOrigin(p0_x, p0_y, p2_x, p2_y));
128 template<
typename ImageType>
139 acc += Traits::at(image,
x,
y);
144 double scale = flux / acc;
147 Traits::at(image,
x,
y) = Traits::at(image,
x,
y) * scale;
float samplePixel(const ModelEvaluator &model_eval, int x, int y, unsigned int subsampling) const
double getMaxRadiusSqr(std::size_t size_x, std::size_t size_y, const Mat22 &transform) const
Mat22 getCombinedTransform(double pixel_scale) const
float adaptiveSamplePixel(const ModelEvaluator &model_eval, int x, int y, unsigned int max_subsampling, float threshold=1.1) const
void renormalize(ImageType &image, double flux) const
float sampleStochastic(const ModelEvaluator &model_eval, int x, int y, unsigned int samples=100) const
double computeSqrDistanceLineToOrigin(double x1, double y1, double x2, double y2) const
CompactModelBase(std::shared_ptr< BasicParameter > x_scale, std::shared_ptr< BasicParameter > y_scale, std::shared_ptr< BasicParameter > rotation, double width, double height, std::shared_ptr< BasicParameter > x, std::shared_ptr< BasicParameter > y, std::tuple< double, double, double, double > transform)