-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbin.cc
More file actions
361 lines (295 loc) · 8.6 KB
/
bin.cc
File metadata and controls
361 lines (295 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <cmath>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <math.h>
#include "point.hh"
#include "misc.hh"
#include "bin.hh"
namespace
{
// work out integerised radius
inline unsigned unsigned_radius(const int x, const int y)
{
return unsigned( std::sqrt( double(x*x + y*y) ) );
}
}
////////////////////////////////////////////////////////////////////////////
bin_helper::bin_helper( const image_float* in_image,
const image_float* smoothed_image,
image_long* bins_image,
double threshold )
: _in_image(in_image),
_smoothed_image(smoothed_image),
_bins_image(bins_image),
_threshold(threshold),
_xw( in_image->xw() ), _yw( in_image->yw() ),
_back_image( 0 ),
_expmap_image( 0 ),
_bg_expmap_image( 0 ),
_noisemap_image( 0 ),
_mask_image( _xw, _yw, 1 ),
_max_annuli( unsigned_radius(_xw, _yw) + 1 ),
_bin_counter( 0 ),
_constrain_fill( false ),
_constrain_val( 4 ),
_scrub_large_bins( -1 )
{
precalculate_annuli();
precalculate_areas();
}
void bin_helper::precalculate_annuli()
{
_annuli_points.clear();
_annuli_points.resize( _max_annuli );
// identify which pixels lie at a particular radius
for(int y=-int(_yw-1); y<int(_yw); ++y)
{
for(int x=-int(_xw-1); x<int(_xw); ++x)
{
const unsigned r = unsigned_radius(x, y);
// add pixel to appropriate radius
_annuli_points[r].emplace_back(x, y);
}
}
}
void bin_helper::precalculate_areas()
{
_areas.resize( _max_annuli );
unsigned total = 0;
for(unsigned radius=0; radius<_max_annuli; ++radius)
{
const unsigned area = _annuli_points[radius].size();
total += area;
_areas[radius] = total;
}
}
///////////////////////////////////////////////////////////////////////
// find an initial pixel for the bin
// search for the nearest pixel with the highest flux
bin::bin( bin_helper* helper )
: _helper(helper),
_bin_no( _helper->bin_counter() ),
_aimval( -1 ),
_fg_sum(0), _bg_sum(0),
_bg_sum_weight(0), _noisemap_2_sum(0),_expratio_sum_2(0),
_centroid_sum(0, 0), _centroid_weight(0), _count(0)
{
}
void bin::drop_bin( )
{
// drop all points from bin
_fg_sum = 0;
_bg_sum = 0;
_bg_sum_weight = 0;
_noisemap_2_sum = 0;
_expratio_sum_2 = 0;
_centroid_sum.x() = 0;
_centroid_sum.y() = 0;
_centroid_weight = 0;
_count = 0;
_all_points.clear();
}
void bin::remove_point( const int x, const int y )
{
// get rid of point from lists
{
const point_int P(x,y);
_Pt_container::iterator pt = std::find( _all_points.begin(),
_all_points.end(),
P );
assert( pt != _all_points.end() );
_all_points.erase( pt );
{
_Pt_container::iterator ept = std::find( _edge_points.begin(),
_edge_points.end(),
P );
if( ept != _edge_points.end() )
_edge_points.erase( ept );
}
}
image_long* const bins_image = _helper->bins_image();
// now get rid of the counts
_fg_sum -= (*_helper->in_image())(x, y);
_count--;
(*bins_image) (x, y) = -1;
if( _helper->back_image() != 0 )
{
const double bs = (*_helper->expmap_image())(x, y) /
(*_helper->bg_expmap_image())(x, y);
const double bg = (*_helper->back_image())(x, y);
_bg_sum -= bg;
_bg_sum_weight -= bg*bs;
_expratio_sum_2 -= bs*bs;
}
// remove from noisemap sum (if any supplied)
if( _helper->noisemap_image() != 0 )
{
_noisemap_2_sum -= square( (*_helper->noisemap_image())(x, y) );
}
// add points that are on the edge of this point in this bin into the
// edge list
const int xw = _helper->xw();
const int yw = _helper->yw();
for(size_t n = 0; n != bin_no_neigh; ++n)
{
const int xp = x + bin_neigh_x[n];
const int yp = y + bin_neigh_y[n];
// if neighbour is in this bin, mark as edge
// if not already marked so
if( xp >= 0 && yp >= 0 && xp < xw && yp < yw &&
(*bins_image)(xp, yp) == _bin_no )
{
if( std::find( _edge_points.begin(), _edge_points.end(),
point_int(xp, yp) ) == _edge_points.end() )
{
_edge_points.emplace_back(xp, yp);
}
}
} // loop over neighbours
}
void bin::add_point(const int x, const int y)
{
point_int pt(x, y);
_all_points.push_back(pt);
double signal = (*_helper->in_image())(x, y);
_fg_sum += signal;
_count++;
(*_helper->bins_image()) (x, y) = _bin_no;
if( _helper->back_image() != 0 )
{
const double bs = (*_helper->expmap_image())(x, y) /
(*_helper->bg_expmap_image())(x, y);
const double back = (*_helper->back_image())(x, y);
_bg_sum += back;
_bg_sum_weight += back*bs;
_expratio_sum_2 += bs*bs;
signal -= back*bs;
}
// add to noisemap sum (if any supplied)
if( _helper->noisemap_image() != 0 )
{
_noisemap_2_sum += square( (*_helper->noisemap_image())(x, y) );
}
// update centroid
{
const double cs = signal < 1e-7 ? 1e-7 : signal;
_centroid_sum += point_dbl( x, y ) * cs;
_centroid_weight += cs;
}
// put into edge (it might not be, but it will get flushed out)
if( std::find(_edge_points.begin(), _edge_points.end(), pt) ==
_edge_points.end() )
{
_edge_points.push_back(pt);
}
}
// paint bin onto bins_image
void bin::paint_bins_image() const
{
image_long& bins_image = * _helper->bins_image();
typedef _Pt_container::const_iterator CI;
const CI e = _all_points.end();
for( CI pix = _all_points.begin(); pix != e; ++pix )
{
bins_image( pix->x(), pix->y() ) = _bin_no;
}
}
// adds the next pixel to the bin!
bool bin::add_next_pixel()
{
// easier access to images
const int xw = _helper->xw();
const int yw = _helper->yw();
const image_short& mask_image = *_helper->mask_image();
const image_long& bins_image = *_helper->bins_image();
const image_float& smoothed_image = *_helper->smoothed_image();
const bool constrain_fill = _helper->constrain_fill();
// find pixel nearest in value to aimval, looking around the edge pixels
// of the bin
double delta = 1e99;
int bestx = -1;
int besty = -1;
// iterate over the proper edge points
//const bool toolarge = calc_length_ratio() > constrain_val;
size_t pix = 0;
while( pix < _edge_points.size() )
{
// where the pixel is
const int x = _edge_points[pix].x();
const int y = _edge_points[pix].y();
// check whether point is edge
bool is_edge = false;
// iterate over neighbours, looking for any better than we have
for( size_t n = 0; n != bin_no_neigh; ++n )
{
const int xp = x + bin_neigh_x[n];
const int yp = y + bin_neigh_y[n];
if( xp >= 0 && yp >= 0 && xp < xw && yp < yw )
{
const long bin = bins_image(xp, yp);
if( bin != _bin_no )
is_edge = true;
// this pixel isn't taken
if( bin < 0 && mask_image(xp, yp) == 1 )
{
if ( ! constrain_fill || check_constraint(xp, yp) )
{
const double newdelta = fabs( smoothed_image(xp, yp) -
_aimval );
if( newdelta < delta )
{
delta = newdelta;
bestx = xp; besty = yp;
}
}
} // pixel not taken
} // if in image
} // loop over neighbours
// go to next point (removing current point if not edge)
if( is_edge )
++pix;
else
_edge_points.erase(_edge_points.begin() + pix);
} // loop over edge pixels in bin
// we didn't find any nice neighbours
if( bestx == -1 )
{
return false;
}
// update stuff
add_point( bestx, besty );
return true;
}
// do the binning until the threshold reached
void bin::do_binning(const unsigned x, const unsigned y)
{
_aimval = (*_helper->smoothed_image())(x, y);
add_point(x, y);
const double sn_threshold_2 = _helper->threshold()*_helper->threshold();
// keep adding pixels until add_next_pixel complains, or s/n reached
while( sn_2() < sn_threshold_2 )
{
if( ! add_next_pixel() )
break;
}
}
// is the constraint still satisfied if we add this pixel?
bool bin::check_constraint(const unsigned x, const unsigned y) const
{
// if( _count < 20 )
// {
// return true;
// }
const point_dbl c = _centroid_sum / _centroid_weight;
const double dx = c.x() - x;
const double dy = c.y() - y;
const double r2 = dx*dx + dy*dy;
// get radius for area
const unsigned circradius = _helper->get_radius_for_area(_count)+1;
// std::cout << _count << ' ' << circradius << ' ' <<
// _count / (circradius*circradius*M_PI) << '\n';
// estimate of radius of circle sqd, A = PI r^2
// const double circ_radius_2 = _count / M_PI;
return (r2 / (circradius*circradius)) < square(_helper->constrain_val());
}