1 | #include "FilterLed.h"
|
---|
2 |
|
---|
3 | #include <memory.h> // memset
|
---|
4 | #include <iostream.h> // cout
|
---|
5 |
|
---|
6 | #include "Led.h"
|
---|
7 | #include "Leds.h"
|
---|
8 | #include "Ring.h"
|
---|
9 |
|
---|
10 | #include "MGMap.h"
|
---|
11 |
|
---|
12 | ClassImp(FilterLed);
|
---|
13 |
|
---|
14 | class ClusterFinder
|
---|
15 | {
|
---|
16 | private:
|
---|
17 | byte *fImg;
|
---|
18 |
|
---|
19 | UInt_t fW;
|
---|
20 | UInt_t fH;
|
---|
21 |
|
---|
22 | Int_t fX0;
|
---|
23 | Int_t fX1;
|
---|
24 |
|
---|
25 | Int_t fY0;
|
---|
26 | Int_t fY1;
|
---|
27 |
|
---|
28 | UInt_t fLimitingSize;
|
---|
29 |
|
---|
30 | UInt_t fCount;
|
---|
31 | Float_t fSumX;
|
---|
32 | Float_t fSumY;
|
---|
33 |
|
---|
34 | Float_t FindCluster(Int_t x, Int_t y)
|
---|
35 | {
|
---|
36 | // if edge is touched stop finding cluster
|
---|
37 | if (x<fX0 || x>=fX1 || y<fY0 || y>=fY1)
|
---|
38 | return -1;
|
---|
39 |
|
---|
40 | if (fCount>fLimitingSize)
|
---|
41 | return -2;
|
---|
42 |
|
---|
43 | // get the value
|
---|
44 | Float_t val = fImg[y*fW+x];
|
---|
45 |
|
---|
46 | // if its empty we have found the border of the cluster
|
---|
47 | if (val==0)
|
---|
48 | return 0;
|
---|
49 |
|
---|
50 | // mark the point as processed
|
---|
51 | fImg[y*fW+x] = 0;
|
---|
52 |
|
---|
53 | fSumX += x*val; // sumx
|
---|
54 | fSumY += y*val; // sumy
|
---|
55 | fCount++;
|
---|
56 |
|
---|
57 | Float_t rc[4];
|
---|
58 | rc[0] = FindCluster(x+1, y );
|
---|
59 | rc[1] = FindCluster(x, y+1);
|
---|
60 | rc[2] = FindCluster(x-1, y );
|
---|
61 | rc[3] = FindCluster(x, y-1);
|
---|
62 |
|
---|
63 | for (int i=0; i<4; i++)
|
---|
64 | {
|
---|
65 | if (rc[i]<0) // check if edge is touched
|
---|
66 | return rc[i];
|
---|
67 |
|
---|
68 | val += rc[i];
|
---|
69 | }
|
---|
70 |
|
---|
71 | return val;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public:
|
---|
75 | ClusterFinder(byte *img, UInt_t w, UInt_t h) : fLimitingSize(999)
|
---|
76 | {
|
---|
77 | fW = w;
|
---|
78 | fH = h;
|
---|
79 |
|
---|
80 | fX0 = 0;
|
---|
81 | fY0 = 0;
|
---|
82 | fX1 = fW;
|
---|
83 | fY1 = fH;
|
---|
84 |
|
---|
85 | fImg = new byte[fW*fH];
|
---|
86 |
|
---|
87 | memcpy(fImg, img, fW*fH);
|
---|
88 | }
|
---|
89 |
|
---|
90 | ~ClusterFinder()
|
---|
91 | {
|
---|
92 | delete fImg;
|
---|
93 | }
|
---|
94 | Double_t GetSumX() const { return fSumX; }
|
---|
95 | Double_t GetSumY() const { return fSumY; }
|
---|
96 |
|
---|
97 | UInt_t GetCount() const { return fCount; }
|
---|
98 |
|
---|
99 | void SetLimitingSize(UInt_t lim) { fLimitingSize=lim; }
|
---|
100 |
|
---|
101 | Float_t FindClusterAt(Int_t x, Int_t y)
|
---|
102 | {
|
---|
103 | fCount = 0;
|
---|
104 | fSumX = 0;
|
---|
105 | fSumY = 0;
|
---|
106 |
|
---|
107 | return FindCluster(x, y);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void SetRange(Int_t x0=0, Int_t y0=0, Int_t x1=0, Int_t y1=0)
|
---|
111 | {
|
---|
112 | fX0 = x0;
|
---|
113 | fY0 = y0;
|
---|
114 | fX1 = x1==0?fW:x1;
|
---|
115 | fY1 = y1==0?fH:y1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void FindCluster(Leds &leds, Int_t x0=0, Int_t y0=0, Int_t x1=0, Int_t y1=0)
|
---|
119 | {
|
---|
120 | fX0 = x0;
|
---|
121 | fY0 = y0;
|
---|
122 | fX1 = x1==0?fW:x1;
|
---|
123 | fY1 = y1==0?fH:y1;
|
---|
124 |
|
---|
125 | for (Int_t x=fX0; x<fX1; x++)
|
---|
126 | for (Int_t y=fY0; y<fY1; y++)
|
---|
127 | {
|
---|
128 | const byte &b = fImg[y*fW+x];
|
---|
129 | if (b==0)
|
---|
130 | continue;
|
---|
131 |
|
---|
132 | const Float_t mag = FindClusterAt(x, y);
|
---|
133 | if (fCount>999)
|
---|
134 | {
|
---|
135 | cout << "ERROR - Spot with Size>999 detected..." << endl;
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (mag>0 && fCount>6)
|
---|
140 | leds.Add(fSumX/mag, fSumY/mag, 0, 0, mag);
|
---|
141 | }
|
---|
142 | leds.Compress();
|
---|
143 | }
|
---|
144 | };
|
---|
145 |
|
---|
146 |
|
---|
147 | void FilterLed::DrawBox(const int x1, const int y1,
|
---|
148 | const int x2, const int y2,
|
---|
149 | const int col) const
|
---|
150 | {
|
---|
151 | MGMap::DrawBox(fImg, 768, 576, x1, y1, x2, y2, col);
|
---|
152 | }
|
---|
153 |
|
---|
154 | void FilterLed::MarkPoint(Float_t px, Float_t py, Float_t mag) const
|
---|
155 | {
|
---|
156 | const int x = (int)(px+.5);
|
---|
157 | const int y = (int)(py+.5);
|
---|
158 | const int m = (int)(mag);
|
---|
159 |
|
---|
160 | DrawBox(x-8, y, x-5, y, m);
|
---|
161 | DrawBox(x, y+5, x, y+8, m);
|
---|
162 | DrawBox(x+5, y, x+8, y, m);
|
---|
163 | DrawBox(x, y-8, x, y-5, m);
|
---|
164 | }
|
---|
165 |
|
---|
166 | void FilterLed::MarkPoint(const Led &led) const
|
---|
167 | {
|
---|
168 | /*
|
---|
169 | Int_t M = (int)(log(led.GetMag())*20);
|
---|
170 |
|
---|
171 | cout << led.GetMag() << endl;
|
---|
172 |
|
---|
173 | if (M>0xff)
|
---|
174 | M=0xff;
|
---|
175 | if (M<0xc0)
|
---|
176 | M=0xc0;
|
---|
177 | */
|
---|
178 |
|
---|
179 | const int x = (int)(led.GetX()+.5);
|
---|
180 | const int y = (int)(led.GetY()+.5);
|
---|
181 |
|
---|
182 | MarkPoint(x, y, 0xff);
|
---|
183 | }
|
---|
184 |
|
---|
185 | void FilterLed::DrawCircle(float cx, float cy, float r, byte col) const
|
---|
186 | {
|
---|
187 | MGMap::DrawCircle(fImg, 768, 576, cx, cy, r, col);
|
---|
188 | }
|
---|
189 |
|
---|
190 | void FilterLed::DrawHexagon(float cx, float cy, float r, byte col) const
|
---|
191 | {
|
---|
192 | MGMap::DrawHexagon(fImg, 768, 576, cx, cy, r, col);
|
---|
193 | }
|
---|
194 |
|
---|
195 | void FilterLed::DrawCircle(const Ring &l, byte col) const
|
---|
196 | {
|
---|
197 | DrawCircle(l.GetX(), l.GetY(), l.GetR(), col);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void FilterLed::DrawCircle(const Ring &l, double r, byte col) const
|
---|
201 | {
|
---|
202 | DrawCircle(l.GetX(), l.GetY(), r, col);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void FilterLed::DrawHexagon(const Ring &l, double r, byte col) const
|
---|
206 | {
|
---|
207 | DrawHexagon(l.GetX(), l.GetY(), r, col);
|
---|
208 | }
|
---|
209 |
|
---|
210 | void FilterLed::GetMinMax(const int offset, byte *min, byte *max) const
|
---|
211 | {
|
---|
212 | *min = fImg[0];
|
---|
213 | *max = fImg[0];
|
---|
214 |
|
---|
215 | byte *s = (byte*)fImg;
|
---|
216 | const byte *e0 = s+fW*fH;
|
---|
217 |
|
---|
218 | //
|
---|
219 | // calculate mean value (speed optimized)
|
---|
220 | //
|
---|
221 | while (s<e0)
|
---|
222 | {
|
---|
223 | const byte *e = s+fH-offset;
|
---|
224 | s += offset;
|
---|
225 |
|
---|
226 | while (s<e)
|
---|
227 | {
|
---|
228 | if (*s>*max)
|
---|
229 | {
|
---|
230 | *max = *s;
|
---|
231 | if (*max-*min==255)
|
---|
232 | return;
|
---|
233 | }
|
---|
234 | if (*s<*min)
|
---|
235 | {
|
---|
236 | *min = *s;
|
---|
237 | if (*max-*min==255)
|
---|
238 | return;
|
---|
239 | }
|
---|
240 | s++;
|
---|
241 | }
|
---|
242 | s+=offset;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | int FilterLed::GetMeanPosition(const int x, const int y,
|
---|
247 | const int box, float &mx, float &my, unsigned int &sum) const
|
---|
248 | {
|
---|
249 | unsigned int sumx=0;
|
---|
250 | unsigned int sumy=0;
|
---|
251 |
|
---|
252 | sum=0;
|
---|
253 | for (int dx=x-box; dx<x+box+1; dx++)
|
---|
254 | for (int dy=y-box; dy<y+box+1; dy++)
|
---|
255 | {
|
---|
256 | const byte &m = fImg[dy*fW+dx];
|
---|
257 |
|
---|
258 | sumx += m*dx;
|
---|
259 | sumy += m*dy;
|
---|
260 | sum += m;
|
---|
261 | }
|
---|
262 |
|
---|
263 | mx = (float)sumx/sum;
|
---|
264 | my = (float)sumy/sum;
|
---|
265 |
|
---|
266 | return (int)my*fW + (int)mx;
|
---|
267 | }
|
---|
268 |
|
---|
269 | int FilterLed::GetMeanPosition(const int x, const int y, const int box) const
|
---|
270 | {
|
---|
271 | float mx, my;
|
---|
272 | unsigned int sum;
|
---|
273 | return GetMeanPosition(x, y, box, mx, my, sum);
|
---|
274 | }
|
---|
275 |
|
---|
276 | int FilterLed::GetMeanPositionBox(const int x, const int y,
|
---|
277 | const int box, float &mx,
|
---|
278 | float &my, unsigned int &sum) const
|
---|
279 | {
|
---|
280 | //-------------------------------
|
---|
281 | // Improved algorithm:
|
---|
282 | // 1. Look for the largest five-pixel-cross signal inside the box
|
---|
283 | int x0 = TMath::Max(x-box+1, 0);
|
---|
284 | int y0 = TMath::Max(y-box+1, 0);
|
---|
285 |
|
---|
286 | int x1 = TMath::Min(x+box+1-1, fW);
|
---|
287 | int y1 = TMath::Min(y+box+1-1, fH);
|
---|
288 |
|
---|
289 | int maxx=0;
|
---|
290 | int maxy=0;
|
---|
291 |
|
---|
292 | unsigned int max =0;
|
---|
293 | for (int dx=x0; dx<x1; dx++)
|
---|
294 | {
|
---|
295 | for (int dy=y0; dy<y1; dy++)
|
---|
296 | {
|
---|
297 | const unsigned int sumloc =
|
---|
298 | fImg[(dy+0)*fW + (dx-1)] +
|
---|
299 | fImg[(dy+0)*fW + (dx+1)] +
|
---|
300 | fImg[(dy+1)*fW + dx] +
|
---|
301 | fImg[(dy+0)*fW + dx] +
|
---|
302 | fImg[(dy-1)*fW + dx];
|
---|
303 |
|
---|
304 | if(sumloc<=max)
|
---|
305 | continue;
|
---|
306 |
|
---|
307 | maxx=dx;
|
---|
308 | maxy=dy;
|
---|
309 | max =sumloc;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | // 2. Calculate mean position inside a circle around
|
---|
314 | // the highst cross-signal with radius of 6 pixels.
|
---|
315 | ClusterFinder find(fImg, fW, fH);
|
---|
316 | find.SetLimitingSize(9999);
|
---|
317 | find.SetRange(x0, y0, x1, y1);
|
---|
318 |
|
---|
319 | const Float_t mag = find.FindClusterAt(maxx, maxy);
|
---|
320 |
|
---|
321 | mx = find.GetSumX()/mag;
|
---|
322 | my = find.GetSumY()/mag;
|
---|
323 |
|
---|
324 | sum = (int)(mag+0.5);
|
---|
325 |
|
---|
326 | return (int)my*fW + (int)mx;
|
---|
327 | }
|
---|
328 |
|
---|
329 | int FilterLed::GetMeanPositionBox(const int x, const int y,
|
---|
330 | const int box) const
|
---|
331 | {
|
---|
332 | float mx, my;
|
---|
333 | unsigned int sum;
|
---|
334 | return GetMeanPositionBox(x, y, box, mx, my, sum);
|
---|
335 | }
|
---|
336 |
|
---|
337 | void FilterLed::ExecuteAndMark(Leds &leds, int xc, int yc) const
|
---|
338 | {
|
---|
339 | const Int_t first = leds.GetEntriesFast();
|
---|
340 |
|
---|
341 | Execute(leds, xc, yc);
|
---|
342 |
|
---|
343 | // Mark Stars in image
|
---|
344 | for (int i=first; i<leds.GetEntriesFast(); i++)
|
---|
345 | MarkPoint(leds(i));
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | void FilterLed::ExecuteAndMark(Leds &leds, int xc, int yc, double &bright) const
|
---|
350 | {
|
---|
351 | const Int_t first = leds.GetEntriesFast();
|
---|
352 |
|
---|
353 | Execute(leds, xc, yc, bright);
|
---|
354 |
|
---|
355 | // Mark Stars in image
|
---|
356 | for (int i=first; i<leds.GetEntriesFast(); i++)
|
---|
357 | MarkPoint(leds(i));
|
---|
358 | }
|
---|
359 |
|
---|
360 | void FilterLed::Execute(int xc, int yc) const
|
---|
361 | {
|
---|
362 | Leds leds;
|
---|
363 | ExecuteAndMark(leds, xc, yc);
|
---|
364 | }
|
---|
365 |
|
---|
366 | void FilterLed::Execute(Leds &leds, int xc, int yc) const
|
---|
367 | {
|
---|
368 | double bright;
|
---|
369 | Execute(leds, xc, yc, bright);
|
---|
370 | }
|
---|
371 |
|
---|
372 | void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const
|
---|
373 | {
|
---|
374 | const int x0 = TMath::Max(xc-fBox, 0);
|
---|
375 | const int y0 = TMath::Max(yc-fBox, 0);
|
---|
376 | const int x1 = TMath::Min(xc+fBox, fW);
|
---|
377 | const int y1 = TMath::Min(yc+fBox, fH);
|
---|
378 |
|
---|
379 | const int wx = x1-x0;
|
---|
380 | const int hy = y1-y0;
|
---|
381 |
|
---|
382 | double sum = 0;
|
---|
383 | double sq = 0;
|
---|
384 |
|
---|
385 | for (int x=x0; x<x1; x++)
|
---|
386 | for (int y=y0; y<y1; y++)
|
---|
387 | {
|
---|
388 | byte &b = fImg[y*fW+x];
|
---|
389 |
|
---|
390 | // Skip saturating pixels
|
---|
391 | if (b>0xf0)
|
---|
392 | continue;
|
---|
393 |
|
---|
394 | sum += b;
|
---|
395 | sq += b*b;
|
---|
396 | }
|
---|
397 |
|
---|
398 | sum /= wx*hy;
|
---|
399 | sq /= wx*hy;
|
---|
400 |
|
---|
401 | bright=sum;
|
---|
402 |
|
---|
403 |
|
---|
404 | // 254 because b<=max and not b<max
|
---|
405 | const double sdev = TMath::Sqrt(sq-sum*sum);
|
---|
406 | const byte max = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);
|
---|
407 |
|
---|
408 | //
|
---|
409 | // clean image from noise
|
---|
410 | // (FIXME: A lookup table could accelerate things...
|
---|
411 | //
|
---|
412 | for (int x=x0; x<x1; x++)
|
---|
413 | for (int y=y0; y<y1; y++)
|
---|
414 | {
|
---|
415 | byte &b = fImg[y*fW+x];
|
---|
416 | if (b<=max)
|
---|
417 | b = 0;
|
---|
418 | }
|
---|
419 |
|
---|
420 | ClusterFinder find(fImg, fW, fH);
|
---|
421 | find.FindCluster(leds, x0, y0, x1, y1);
|
---|
422 | }
|
---|
423 |
|
---|
424 | void FilterLed::FindStar(Leds &leds, int xc, int yc, bool box) const
|
---|
425 | {
|
---|
426 | // fBox: radius of the inner (signal) box
|
---|
427 | // Radius of the outer box is fBox*sqrt(2)
|
---|
428 |
|
---|
429 | //
|
---|
430 | // Define inner box in which to search the signal
|
---|
431 | //
|
---|
432 | const int x0 = TMath::Max(xc-fBox, 0);
|
---|
433 | const int y0 = TMath::Max(yc-fBox, 0);
|
---|
434 | const int x1 = TMath::Min(xc+fBox, fW);
|
---|
435 | const int y1 = TMath::Min(yc+fBox, fH);
|
---|
436 |
|
---|
437 | //
|
---|
438 | // Define outer box (excluding inner box) having almost
|
---|
439 | // the same number of pixels in which the background
|
---|
440 | // is calculated
|
---|
441 | //
|
---|
442 | const double sqrt2 = sqrt(2.);
|
---|
443 |
|
---|
444 | const int xa = TMath::Max(xc-(int)rint(fBox*sqrt2), 0);
|
---|
445 | const int ya = TMath::Max(yc-(int)rint(fBox*sqrt2), 0);
|
---|
446 | const int xb = TMath::Min(xc+(int)rint(fBox*sqrt2), fW);
|
---|
447 | const int yb = TMath::Min(yc+(int)rint(fBox*sqrt2), fH);
|
---|
448 |
|
---|
449 | //
|
---|
450 | // Calculate average and sdev for a square
|
---|
451 | // excluding the inner part were we expect
|
---|
452 | // the signal to be.
|
---|
453 | //
|
---|
454 | double sum = 0;
|
---|
455 | double sq = 0;
|
---|
456 |
|
---|
457 | int n=0;
|
---|
458 | for (int x=xa; x<xb; x++)
|
---|
459 | for (int y=ya; y<yb; y++)
|
---|
460 | {
|
---|
461 | if (x>=x0 && x<x1 && y>=y0 && y<y1)
|
---|
462 | continue;
|
---|
463 |
|
---|
464 | byte &b = fImg[y*fW+x];
|
---|
465 |
|
---|
466 | sum += b;
|
---|
467 | sq += b*b;
|
---|
468 | n++;
|
---|
469 | }
|
---|
470 |
|
---|
471 | sum /= n;
|
---|
472 | sq /= n;
|
---|
473 |
|
---|
474 | // 254 because b<=max and not b<max
|
---|
475 | const double sdev = sqrt(sq-sum*sum);
|
---|
476 | const byte max = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);
|
---|
477 |
|
---|
478 | //
|
---|
479 | // clean image from noise
|
---|
480 | // (FIXME: A lookup table could accelerate things...
|
---|
481 | //
|
---|
482 | n=0;
|
---|
483 | for (int x=x0; x<x1; x++)
|
---|
484 | for (int y=y0; y<y1; y++)
|
---|
485 | {
|
---|
486 | byte &b = fImg[y*fW+x];
|
---|
487 | if (b<=max)
|
---|
488 | b = 0;
|
---|
489 | else
|
---|
490 | n++;
|
---|
491 | }
|
---|
492 |
|
---|
493 | //
|
---|
494 | // Mark the background region
|
---|
495 | //
|
---|
496 | for (int x=xa; x<xb; x+=2)
|
---|
497 | {
|
---|
498 | fImg[ya*fW+x]=0xf0;
|
---|
499 | fImg[yb*fW+x]=0xf0;
|
---|
500 | }
|
---|
501 | for (int y=ya; y<yb; y+=2)
|
---|
502 | {
|
---|
503 | fImg[y*fW+xa]=0xf0;
|
---|
504 | fImg[y*fW+xb]=0xf0;
|
---|
505 | }
|
---|
506 |
|
---|
507 | //
|
---|
508 | // Check if any pixel found...
|
---|
509 | //
|
---|
510 | if (n<5)
|
---|
511 | return;
|
---|
512 |
|
---|
513 | //
|
---|
514 | // Get the mean position of the star
|
---|
515 | //
|
---|
516 | float mx, my;
|
---|
517 | unsigned int mag;
|
---|
518 | int pos = box ? GetMeanPositionBox(xc, yc, fBox-1, mx, my, mag) : GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
|
---|
519 |
|
---|
520 | if (pos<0 || pos>=fW*fH || fImg[pos]<sum+fCut*sdev)
|
---|
521 | return;
|
---|
522 |
|
---|
523 | // cout << "Mean=" << sum << " SDev=" << sdev << " : ";
|
---|
524 | // cout << "Sum/n = " << sum << "/" << n << " = " << (n==0?0:mag/n) << endl;
|
---|
525 |
|
---|
526 | leds.Add(mx, my, 0, 0, -2.5*log10((float)mag)+13.7);
|
---|
527 | }
|
---|
528 |
|
---|
529 | void FilterLed::Stretch() const
|
---|
530 | {
|
---|
531 | byte min, max;
|
---|
532 | GetMinMax(25, &min, &max);
|
---|
533 |
|
---|
534 | if (min==max || max-min>230) // 255/230=1.1
|
---|
535 | return;
|
---|
536 |
|
---|
537 | const float scale = 255./(max-min);
|
---|
538 |
|
---|
539 | byte *b = fImg;
|
---|
540 | const byte *e = fImg+fW*fH;
|
---|
541 |
|
---|
542 | while (b<e)
|
---|
543 | {
|
---|
544 | if (*b<min)
|
---|
545 | {
|
---|
546 | *b++=0;
|
---|
547 | continue;
|
---|
548 | }
|
---|
549 | if (*b>max)
|
---|
550 | {
|
---|
551 | *b++=255;
|
---|
552 | continue;
|
---|
553 | }
|
---|
554 | *b = (byte)((*b-min)*scale);
|
---|
555 | b++;
|
---|
556 | }
|
---|
557 | }
|
---|