Changeset 7788 for trunk/MagicSoft/Cosy/videodev
- Timestamp:
- 07/12/06 03:00:16 (18 years ago)
- Location:
- trunk/MagicSoft/Cosy/videodev
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/videodev/FilterLed.cc
r7230 r7788 125 125 float &my, unsigned int &sum) const 126 126 { 127 //------------------------------- 128 // Improved algorithm: 129 // 1. Look for the largest five-pixel-cross signal inside the box 130 int x0 = TMath::Max(x-box+1, 0); 131 int y0 = TMath::Max(y-box+1, 0); 132 133 int x1 = TMath::Min(x+box+1-1, fW); 134 int y1 = TMath::Min(y+box+1-1, fH); 135 136 int maxx=0; 137 int maxy=0; 138 139 unsigned int max =0; 140 for (int dx=x0; dx<x1; dx++) 141 { 142 for (int dy=y0; dy<y1; dy++) 143 { 144 const unsigned int sumloc = 145 fImg[(dy+0)*fW + (dx-1)] + 146 fImg[(dy+0)*fW + (dx+1)] + 147 fImg[(dy+1)*fW + dx] + 148 fImg[(dy+0)*fW + dx] + 149 fImg[(dy-1)*fW + dx]; 150 151 if(sumloc<=max) 152 continue; 153 154 maxx=dx; 155 maxy=dy; 156 max =sum; 157 } 158 } 159 160 // 2. Calculate mean position inside a circle around 161 // the highst cross-signal with radius of 6 pixels. 127 162 unsigned int sumx=0; 128 163 unsigned int sumy=0; 129 164 130 //------------------------------- 131 // Improved algorithm: 132 // 1. Look for the largest four-pixel signal inside box 133 134 int thissum=0, maxsum=0; 135 int maxx=0, maxy=0; 136 for (int dx=x-box; dx<x+box+1; dx++) 137 for (int dy=y-box; dy<y+box+1; dy++) 138 { 139 thissum = fImg[dy*fW+dx]+fImg[(dy+1)*fW+dx]+ 140 fImg[dy*fW+(dx+1)]+fImg[(dy+1)*fW+(dx+1)]; 141 if(thissum>maxsum) 142 { 143 maxx=dx; 144 maxy=dy; 145 maxsum=thissum; 146 } 147 } 148 149 // 2. Calculate mean position inside a circle around 150 // the highst four-pixel signal with radius of 5 pixels. 165 const int rad = 17; 166 167 x0 = TMath::Max(x-box, maxx-rad); 168 y0 = TMath::Max(y-box, maxy-rad); 169 170 x1 = TMath::Min(x+box+1, maxx+rad+1); 171 y1 = TMath::Min(y+box+1, maxy+rad+1); 151 172 152 173 sum=0; 153 for (int dx=x -box; dx<x+box+1; dx++)154 for (int dy=y -box; dy<y+box+1; dy++)174 for (int dx=x0; dx<x1; dx++) 175 for (int dy=y0; dy<y1; dy++) 155 176 { 156 177 const byte &m = fImg[dy*fW+dx]; 157 178 158 // Circle 159 if(sqrt((dx-maxx)*(dx-maxx)+ 160 (dy-maxy)*(dy-maxy)) <= 6) 161 { 162 sumx += m*dx; 163 sumy += m*dy; 164 sum += m; 165 } 179 sumx += m*dx; 180 sumy += m*dy; 181 sum += m; 166 182 } 167 183 … … 171 187 return (int)my*fW + (int)mx; 172 188 } 173 174 175 189 176 190 int FilterLed::GetMeanPositionCircle(const int x, const int y, … … 323 337 void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const 324 338 { 325 int x0 = xc-fBox; 326 int x1 = xc+fBox; 327 int y0 = yc-fBox; 328 int y1 = yc+fBox; 329 330 if (x0<0) x0=0; 331 if (y0<0) y0=0; 332 if (x1>fW) x1=fW; 333 if (y1>fH) y1=fH; 339 const int x0 = TMath::Max(xc-fBox, 0); 340 const int y0 = TMath::Max(yc-fBox, 0); 341 const int x1 = TMath::Min(xc+fBox, fW); 342 const int y1 = TMath::Min(yc+fBox, fH); 334 343 335 344 const int wx = x1-x0; … … 430 439 431 440 RemoveTwinsInterpol(leds, first, 5); 432 433 434 } 435 436 void FilterLed::FindStar(Leds &leds, int xc, int yc) const 441 } 442 443 void FilterLed::FindStar(Leds &leds, int xc, int yc, bool circle) const 437 444 { 438 445 // fBox: radius of the inner (signal) box … … 442 449 // Define inner box in which to search the signal 443 450 // 444 int x0 = xc-fBox; 445 int x1 = xc+fBox; 446 int y0 = yc-fBox; 447 int y1 = yc+fBox; 448 449 if (x0<0) x0=0; 450 if (y0<0) y0=0; 451 if (x1>fW) x1=fW; 452 if (y1>fH) y1=fH; 451 const int x0 = TMath::Max(xc-fBox, 0); 452 const int y0 = TMath::Max(yc-fBox, 0); 453 const int x1 = TMath::Min(xc+fBox, fW); 454 const int y1 = TMath::Min(yc+fBox, fH); 453 455 454 456 // … … 459 461 const double sqrt2 = sqrt(2.); 460 462 461 int xa = xc-(int)rint(fBox*sqrt2); 462 int xb = xc+(int)rint(fBox*sqrt2); 463 int ya = yc-(int)rint(fBox*sqrt2); 464 int yb = yc+(int)rint(fBox*sqrt2); 465 466 if (xa<0) xa=0; 467 if (ya<0) ya=0; 468 if (xb>fW) xb=fW; 469 if (yb>fH) yb=fH; 463 const int xa = TMath::Max(xc-(int)rint(fBox*sqrt2), 0); 464 const int ya = TMath::Max(yc-(int)rint(fBox*sqrt2), 0); 465 const int xb = TMath::Min(xc+(int)rint(fBox*sqrt2), fW); 466 const int yb = TMath::Min(yc+(int)rint(fBox*sqrt2), fH); 470 467 471 468 // … … 538 535 float mx, my; 539 536 unsigned int mag; 540 int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);541 542 if (pos<0 || pos>=fW*fH &&fImg[pos]<sum+fCut*sdev)537 int pos = circle ? GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag) : GetMeanPosition(xc, yc, fBox-1, mx, my, mag); 538 539 if (pos<0 || pos>=fW*fH || fImg[pos]<sum+fCut*sdev) 543 540 return; 544 541 … … 549 546 } 550 547 551 void FilterLed::FindStarCircle(Leds &leds, int xc, int yc) const552 {553 // fBox: radius of the inner (signal) box554 // Radius of the outer box is fBox*sqrt(2)555 556 //557 // Define inner box in which to search the signal558 //559 int x0 = xc-fBox;560 int x1 = xc+fBox;561 int y0 = yc-fBox;562 int y1 = yc+fBox;563 564 if (x0<0) x0=0;565 if (y0<0) y0=0;566 if (x1>fW) x1=fW;567 if (y1>fH) y1=fH;568 569 //570 // Define outer box (excluding inner box) having almost571 // the same number of pixels in which the background572 // is calculated573 //574 const double sqrt2 = sqrt(2.);575 576 int xa = xc-(int)rint(fBox*sqrt2);577 int xb = xc+(int)rint(fBox*sqrt2);578 int ya = yc-(int)rint(fBox*sqrt2);579 int yb = yc+(int)rint(fBox*sqrt2);580 581 if (xa<0) xa=0;582 if (ya<0) ya=0;583 if (xb>fW) xb=fW;584 if (yb>fH) yb=fH;585 586 //587 // Calculate average and sdev for a square588 // excluding the inner part were we expect589 // the signal to be.590 //591 double sum = 0;592 double sq = 0;593 594 int n=0;595 for (int x=xa; x<xb; x++)596 for (int y=ya; y<yb; y++)597 {598 if (x>=x0 && x<x1 && y>=y0 && y<y1)599 continue;600 601 byte &b = fImg[y*fW+x];602 603 sum += b;604 sq += b*b;605 n++;606 }607 608 sum /= n;609 sq /= n;610 611 // 254 because b<=max and not b<max612 const double sdev = sqrt(sq-sum*sum);613 const byte max = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);614 615 //616 // clean image from noise617 // (FIXME: A lookup table could accelerate things...618 //619 n=0;620 for (int x=x0; x<x1; x++)621 for (int y=y0; y<y1; y++)622 {623 byte &b = fImg[y*fW+x];624 if (b<=max)625 b = 0;626 else627 n++;628 }629 630 //631 // Mark the background region632 //633 for (int x=xa; x<xb; x+=2)634 {635 fImg[ya*fW+x]=0xf0;636 fImg[yb*fW+x]=0xf0;637 }638 for (int y=ya; y<yb; y+=2)639 {640 fImg[y*fW+xa]=0xf0;641 fImg[y*fW+xb]=0xf0;642 }643 644 //645 // Check if any pixel found...646 //647 if (n<5)648 return;649 650 //651 // Get the mean position of the star652 //653 float mx, my;654 unsigned int mag;655 656 // int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);657 658 // try new method659 int pos = GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag);660 661 if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev)662 return;663 664 cout << "Mean=" << sum << " SDev=" << sdev << " : ";665 cout << "Sum/n = " << sum << "/" << n << " = " << (n==0?0:mag/n) << endl;666 667 leds.Add(mx, my, 0, 0, -2.5*log10((float)mag)+13.7);668 }669 670 671 548 void FilterLed::Stretch() const 672 549 { … … 698 575 } 699 576 } 700 -
trunk/MagicSoft/Cosy/videodev/FilterLed.h
r7230 r7788 49 49 void SetBox(int box) { fBox = box; } 50 50 void SetCut(float cut) { fCut = cut; } 51 void FindStar(Leds &leds, int xc, int yc) const; 52 void FindStarCircle(Leds &leds, int xc, int yc) const; 51 void FindStar(Leds &leds, int xc, int yc, bool circle=false) const; 53 52 54 53 void Execute(Leds &leds, int xc, int yc, double &bright) const;
Note:
See TracChangeset
for help on using the changeset viewer.