ImgAlgos/src/AlgImgProc.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: AlgImgProc.cpp 12920 2016-11-30 01:47:09Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class AlgImgProc
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "ImgAlgos/AlgImgProc.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <cmath>     // floor, ceil
00022 #include <iomanip>   // for std::setw
00023 #include <sstream>   // for stringstream
00024 
00025 namespace ImgAlgos {
00026 
00027 //----------------
00028 // Constructors --
00029 //----------------
00030 
00031 AlgImgProc::AlgImgProc ( const size_t&   seg
00032                        , const size_t&   rowmin
00033                        , const size_t&   rowmax
00034                        , const size_t&   colmin
00035                        , const size_t&   colmax
00036                        , const unsigned& pbits
00037                        , const unsigned& npksmax
00038                        )
00039   : m_pbits(pbits)
00040   , m_npksmax(npksmax)
00041   , m_seg(seg)
00042   , m_init_son_is_done(false)
00043   , m_init_bkgd_is_done(false)
00044   , m_r0(7.0)
00045   , m_dr(2.0)
00046   , m_sonres_def()
00047   , m_peak_npix_min(0)
00048   , m_peak_npix_max(1e6)
00049   , m_peak_amax_thr(0)
00050   , m_peak_atot_thr(0)
00051   , m_peak_son_min(0)
00052   , m_do_preselect(true)
00053 {
00054   if(m_pbits & 512) MsgLog(_name(), info, "in c-tor AlgImgProc, seg=" << m_seg);
00055   m_win.set(seg, rowmin, rowmax, colmin, colmax);
00056 
00057   if(m_pbits & 2) printInputPars();
00058 }
00059 
00060 //--------------------
00061 
00062 AlgImgProc::~AlgImgProc() 
00063 {
00064   if(m_pbits & 512) MsgLog(_name(), info, "in d-tor ~AlgImgProc, seg=" << m_seg);
00065   //v_peaks.resize(0); 
00066   //v_peaks_work.resize(0);
00067 }
00068 
00069 //--------------------
00070 
00071 void 
00072 AlgImgProc::_reserveVectorOfPeaks()
00073 {
00074   if(v_peaks_work.capacity() == m_npksmax) return;
00075 
00076   v_peaks_work.reserve(m_npksmax);
00077   v_peaks.reserve(m_npksmax);
00078   v_peaks_sel.reserve(m_npksmax);
00079 }
00080 
00081 //--------------------
00082 
00083 void 
00084 AlgImgProc::printInputPars()
00085 {
00086   std::stringstream ss; 
00087   ss << "printInputPars:\n"
00088      << "\npbits   : " << m_pbits
00089      << "\nnpksmax : " << m_npksmax
00090      << "\nwindow  : " << m_win
00091      << '\n';
00092   //ss << "\nrmin    : " << m_r0
00093   //   << "\ndr      : " << m_dr
00094   MsgLog(_name(), info, ss.str()); 
00095 
00096   if(m_pbits & 512) printMatrixOfRingIndexes();
00097   if(m_pbits & 512) printVectorOfRingIndexes();
00098 }
00099 
00100 //--------------------
00101 
00102 void 
00103 AlgImgProc::_makeMapOfConnectedPixels()
00104 {
00105   //if(m_conmap.size()==0) 
00106   if(m_conmap.empty()) 
00107      m_conmap = make_ndarray<conmap_t>(m_pixel_status.shape()[0], m_pixel_status.shape()[1]);
00108 
00109   std::fill_n(m_conmap.data(), int(m_pixel_status.size()), conmap_t(0));
00110   m_numreg=0;
00111 
00112   for(int r = (int)m_win.rowmin; r<(int)m_win.rowmax; r++) {
00113     for(int c = (int)m_win.colmin; c<(int)m_win.colmax; c++) {
00114 
00115       if(!(m_pixel_status[r][c] & 1)) continue;
00116       ++ m_numreg;
00117       //if(m_numreg == m_npksmax) break;
00118       _findConnectedPixels(r, c);
00119     }
00120   }
00121 
00122   if(m_pbits & 512) MsgLog(_name(), info, "in _makeMapOfConnectedPixels, seg=" << m_seg << ", m_numreg=" <<  m_numreg);
00123 }
00124 
00125 //--------------------
00126 
00127 void 
00128 AlgImgProc::_findConnectedPixels(const int& r, const int& c)
00129 {
00130   //if(m_pbits & 512) MsgLog(_name(), info, "in _findConnectedPixels, seg=" << m_seg);
00131 
00132   if(! (m_pixel_status[r][c] & 1)) return;
00133 
00134   m_pixel_status[r][c] ^= 1; // set the 1st bit to zero.
00135   m_conmap[r][c] = m_numreg;
00136 
00137   // cout  << "ZZZ:  r:" << r << " c:" << c << '\n';
00138 
00139   if(  r+1 < (int)m_win.rowmax ) _findConnectedPixels(r+1, c);
00140   if(  c+1 < (int)m_win.colmax ) _findConnectedPixels(r, c+1);
00141   if(!(r-1 < (int)m_win.rowmin)) _findConnectedPixels(r-1, c);
00142   if(!(c-1 < (int)m_win.colmin)) _findConnectedPixels(r, c-1);  
00143 }
00144 
00145 //--------------------
00146 // Templated recursive method with data for fast termination
00147 // Returns true / false for pixel is ok / m_reg_a0 is not a local maximum
00148 template <typename T>
00149 bool
00150 AlgImgProc::_findConnectedPixelsInRegionV1(const ndarray<const T,2>& data, const int& r, const int& c)
00151 {
00152   //if(m_pbits & 512) MsgLog(_name(), info, "in _findConnectedPixelsInRegionV1, seg=" << m_seg);
00153 
00154   pixel_status_t pstat = m_pixel_status[r][c];
00155   if(! pstat) return true;    // pstat=0 - masked
00156   if(pstat & 28) return true; // pstat=4/8/16 : a<thr_low/used in recursion/used in map
00157 
00158   if(data[r][c] > m_reg_a0) return false; // initial point is not a local maximum - TERMINATE
00159 
00160   m_pixel_status[r][c] |= 8; // mark this pixel as used in this recursion to get rid of cycling
00161   v_ind_pixgrp.push_back(TwoIndexes(r,c));
00162 
00163   if(  r+1 < m_reg_rmax)  if(! _findConnectedPixelsInRegionV1<T>(data, r+1, c)) return false;
00164   if(  c+1 < m_reg_cmax)  if(! _findConnectedPixelsInRegionV1<T>(data, r, c+1)) return false;
00165   if(!(r-1 < m_reg_rmin)) if(! _findConnectedPixelsInRegionV1<T>(data, r-1, c)) return false;
00166   if(!(c-1 < m_reg_cmin)) if(! _findConnectedPixelsInRegionV1<T>(data, r, c-1)) return false;  
00167 
00168   return true;
00169 }
00170 
00171 //--------------------
00172 // Templated recursive method with data for fast termination
00173 // Returns true / false for pixel is ok / m_reg_a0 is not a local maximum
00174 // V2 - find a group connected pixels for already found local maximum
00175 //     - so do not need to terminate recursion like in V1 
00176 
00177 template <typename T>
00178 void
00179 AlgImgProc::_findConnectedPixelsInRegionV2(const ndarray<const T,2>& data,
00180                                            const ndarray<const mask_t,2>& mask, 
00181                                            const int& r, const int& c)
00182 {
00183   //if(m_pbits & 512) MsgLog(_name(), info, "in _findConnectedPixelsInRegionV2, r=" << r << " c=" << c);
00184   //     << " data=" << data[r][c] << " m_reg_thr=" << m_reg_thr <<);
00185 
00186   if(! mask[r][c]) return;   // - masked
00187   if(m_conmap[r][c]) return; // - pixel is already used
00188   if(data[r][c] < m_reg_thr) return; // discard pixel below threshold if m_reg_thr != 0 
00189 
00190   //if(m_pixel_status[r][c] & 8) return; // pstat=4/8/16 : a<thr_low/used in recursion/used in map
00191   //m_pixel_status[r][c] |= 8; // mark this pixel as used in this recursion to get rid of cycling  
00192 
00193   m_conmap[r][c] = m_numreg; // mark pixel on map
00194 
00195   v_ind_pixgrp.push_back(TwoIndexes(r,c));
00196 
00197   if(  r+1 < m_reg_rmax)  _findConnectedPixelsInRegionV2<T>(data, mask, r+1, c);
00198   if(  c+1 < m_reg_cmax)  _findConnectedPixelsInRegionV2<T>(data, mask, r, c+1);
00199   if(!(r-1 < m_reg_rmin)) _findConnectedPixelsInRegionV2<T>(data, mask, r-1, c);
00200   if(!(c-1 < m_reg_cmin)) _findConnectedPixelsInRegionV2<T>(data, mask, r, c-1);  
00201 }
00202 
00203 //--------------------
00204 // NON-Templated recursive method doing full flood filling in the region.
00205 void 
00206 AlgImgProc::_findConnectedPixelsInRegion(const int& r, const int& c)
00207 {
00208   //if(m_pbits & 512) MsgLog(_name(), info, "in _findConnectedPixelsInRegion, seg=" << m_seg);
00209 
00210   pixel_status_t pstat = m_pixel_status[r][c];
00211   if(! pstat) return;    // pstat=0 - masked
00212   if(pstat & 28) return; // pstat=4/8/16 : a<thr_low/used in recursion/used in map
00213   //if(m_conmap[r][c]) return; // pixel belongs to any other group - it is also marked as used
00214 
00215   //cout  << "ZZZ:  v_ind_pixgrp.push_back r:" << r << " c:" << c << '\n';
00216 
00217   m_pixel_status[r][c] |= 8; // mark this pixel as used in this recursion 
00218   v_ind_pixgrp.push_back(TwoIndexes(r,c));
00219 
00220   if(  r+1 < m_reg_rmax)  _findConnectedPixelsInRegion(r+1, c);
00221   if(  c+1 < m_reg_cmax)  _findConnectedPixelsInRegion(r, c+1);
00222   if(!(r-1 < m_reg_rmin)) _findConnectedPixelsInRegion(r-1, c);
00223   if(!(c-1 < m_reg_cmin)) _findConnectedPixelsInRegion(r, c-1);  
00224 }
00225 //--------------------
00226 
00227 void 
00228 AlgImgProc::_addPixGroupToMap()
00229 {
00230   if(m_pbits & 512) MsgLog(_name(), info, "in _addPixGroupToMap, seg=" << m_seg << " numreg=" << m_numreg);
00231   for(vector<TwoIndexes>::const_iterator ij  = v_ind_pixgrp.begin();
00232                                          ij != v_ind_pixgrp.end(); ij++) {
00233     m_conmap[ij->i][ij->j] = m_numreg;
00234     m_pixel_status[ij->i][ij->j] |= 16; // mark pixel as used on map (set bit)  
00235   }
00236 }
00237 
00238 //--------------------
00239 
00240 void 
00241 AlgImgProc::_clearStatusOfUnusedPixels() {
00242   for(vector<TwoIndexes>::const_iterator ij  = v_ind_pixgrp.begin();
00243                                          ij != v_ind_pixgrp.end(); ij++) {
00244     m_pixel_status[ij->i][ij->j] &=~8; // mark pixel as un-used (clear bit)
00245   }
00246 }
00247 
00248 //--------------------
00249 
00250 bool
00251 AlgImgProc::_peakWorkIsPreSelected(const PeakWork& pw)
00252 {
00253   if (! m_do_preselect) return true;
00254   if (pw.peak_npix < m_peak_npix_min) return false;
00255   if (pw.peak_npix > m_peak_npix_max) return false;
00256   if (pw.peak_amax < m_peak_amax_thr) return false;
00257   if (pw.peak_atot < m_peak_atot_thr) return false;
00258   return true;
00259 }
00260 
00261 //--------------------
00262 
00263 bool
00264 AlgImgProc::_peakIsPreSelected(const Peak& peak)
00265 {
00266   if (! m_do_preselect) return true;
00267   if (peak.npix    < m_peak_npix_min) return false;
00268   if (peak.npix    > m_peak_npix_max) return false;
00269   if (peak.amp_max < m_peak_amax_thr) return false;
00270   if (peak.amp_tot < m_peak_atot_thr) return false;
00271   return true;
00272 }
00273 
00274 //--------------------
00275 
00276 bool
00277 AlgImgProc::_peakIsSelected(const Peak& peak)
00278 {
00279   if (peak.son     < m_peak_son_min)  return false;
00280   if (peak.npix    < m_peak_npix_min) return false;
00281   if (peak.npix    > m_peak_npix_max) return false;
00282   if (peak.amp_max < m_peak_amax_thr) return false;
00283   if (peak.amp_tot < m_peak_atot_thr) return false;
00284   return true;
00285 }
00286 
00287 //--------------------
00288 
00289 void
00290 AlgImgProc::_makeVectorOfPeaks()
00291 {
00292   if(m_pbits & 512) MsgLog(_name(), info, "in _makeVectorOfPeaks, seg=" << m_seg << " m_numreg=" << m_numreg);
00293   //m_peaks = make_ndarray<Peak>(m_numreg);
00294 
00295   if(m_numreg==0) return;
00296 
00297   //v_peaks.reserve(m_numreg+1); // this does not always work
00298   _reserveVectorOfPeaks();
00299   v_peaks.clear();
00300 
00301   for(unsigned i=0; i<min(m_numreg, m_npksmax); i++) {
00302 
00303     PeakWork& pw = v_peaks_work[i+1]; // region number begins from 1
00304 
00305     if(! _peakWorkIsPreSelected(pw)) continue;
00306 
00307     Peak   peak; // = v_peaks[i];
00308 
00309     peak.seg       = m_seg;
00310     peak.npix      = pw.peak_npix;
00311     peak.row       = pw.peak_row;
00312     peak.col       = pw.peak_col;
00313     peak.amp_max   = pw.peak_amax;
00314     peak.amp_tot   = pw.peak_atot;
00315 
00316     if (pw.peak_atot>0) {
00317       peak.row_cgrav = pw.peak_ar1/pw.peak_atot;
00318       peak.col_cgrav = pw.peak_ac1/pw.peak_atot;
00319       peak.row_sigma = std::sqrt(pw.peak_ar2/pw.peak_atot - peak.row_cgrav * peak.row_cgrav);
00320       peak.col_sigma = std::sqrt(pw.peak_ac2/pw.peak_atot - peak.col_cgrav * peak.col_cgrav);
00321     }
00322     else {
00323       peak.row_cgrav = pw.peak_row;
00324       peak.col_cgrav = pw.peak_col;
00325       peak.row_sigma = 0;
00326       peak.col_sigma = 0;
00327     }
00328 
00329     peak.row_min   = pw.peak_rmin;
00330     peak.row_max   = pw.peak_rmax;
00331     peak.col_min   = pw.peak_cmin;
00332     peak.col_max   = pw.peak_cmax;
00333     peak.bkgd  = 0;
00334     peak.noise = 0;
00335     peak.son   = 0;
00336 
00337     v_peaks.push_back(peak);
00338   }
00339 }
00340 //--------------------
00341 
00342 void
00343 AlgImgProc::_makeVectorOfSelectedPeaks()
00344 {
00345   if(v_peaks_sel.capacity() != m_npksmax) v_peaks_sel.reserve(m_npksmax);
00346      v_peaks_sel.clear();
00347 
00348   //std::vector<Peak>::iterator it;
00349   for(std::vector<Peak>::iterator it=v_peaks.begin(); it!=v_peaks.end(); ++it) { 
00350     Peak& peak = (*it);
00351     if(_peakIsSelected(peak)) v_peaks_sel.push_back(peak);
00352   }
00353   if(m_pbits & 512) MsgLog(_name(), info, "in _makeVectorOfSelectedPeaks, seg=" << m_seg 
00354                            << "  #peaks raw=" << v_peaks.size() 
00355                            << "  sel=" << v_peaks_sel.size());
00356 }
00357 
00358 //--------------------
00359 
00360 void 
00361 AlgImgProc::_evaluateDiagIndexes(const size_t& rank)
00362 {
00363   if(m_pbits & 512) MsgLog(_name(), info, "in _evaluateDiagIndexes, seg=" << m_seg << " rank=" << rank);
00364 
00365   m_rank = rank;
00366   int indmax =  m_rank;
00367   int indmin = -m_rank;
00368   unsigned npixmax = (2*rank+1)*(2*rank+1);
00369   if(v_inddiag.capacity() < npixmax) v_inddiag.reserve(npixmax);
00370   v_inddiag.clear();
00371 
00372   for (int i = indmin; i <= indmax; ++ i) {
00373     for (int j = indmin; j <= indmax; ++ j) {
00374 
00375       // use rectangular region of radius = rank
00376       // remove already tested central row and column
00377       if (i==0 || j==0) continue;
00378       // use ring region (if un-commented)
00379       //if (m_rank>2 && floor(std::sqrt(float(i*i + j*j)))>(int)m_rank) continue;
00380       //TwoIndexes inds = {i,j};
00381       TwoIndexes inds(i,j);
00382       v_inddiag.push_back(inds);
00383     }
00384   }
00385 
00386   if(m_pbits & 2) printMatrixOfDiagIndexes();
00387 }
00388 
00389 //--------------------
00390 
00391 void 
00392 AlgImgProc::_evaluateRingIndexes(const float& r0, const float& dr)
00393 {
00394   if(m_pbits & 512) MsgLog(_name(), info, "in _evaluateRingIndexes, seg=" << m_seg << " r0=" << r0 << " dr=" << dr);
00395 
00396   m_r0 = r0;
00397   m_dr = dr;
00398 
00399   int indmax = (int)std::ceil(m_r0 + m_dr);
00400   int indmin = -indmax;
00401   unsigned npixmax = (2*indmax+1)*(2*indmax+1);
00402   if(v_indexes.capacity() < npixmax) v_indexes.reserve(npixmax);
00403   v_indexes.clear();
00404 
00405   for (int i = indmin; i <= indmax; ++ i) {
00406     for (int j = indmin; j <= indmax; ++ j) {
00407 
00408       float r = std::sqrt( float(i*i + j*j) );
00409       if ( r < m_r0 || r > m_r0 + m_dr ) continue;
00410       //TwoIndexes inds = {i,j};
00411       TwoIndexes inds(i,j);
00412       v_indexes.push_back(inds);
00413     }
00414   }
00415 
00416   if(m_pbits & 2) printMatrixOfRingIndexes();
00417   if(m_pbits & 4) printVectorOfRingIndexes();
00418 }
00419 
00420 //--------------------
00421 
00422 void 
00423 AlgImgProc::_fillCrossIndexes()
00424 {
00425   v_indcross.push_back(TwoIndexes(-1, 0));
00426   v_indcross.push_back(TwoIndexes( 1, 0));
00427   v_indcross.push_back(TwoIndexes( 0,-1));
00428   v_indcross.push_back(TwoIndexes( 0, 1));
00429 }
00430 
00431 //--------------------
00432 
00433 void 
00434 AlgImgProc::setSoNPars(const float& r0, const float& dr)
00435 { 
00436   if(m_pbits & 512) MsgLog(_name(), info, "in setSoNPars, seg=" << m_seg << " r0=" << r0 << " dr=" << dr);
00437 
00438   if(r0==m_r0 && dr==m_dr) return;
00439   _evaluateRingIndexes(r0, dr);
00440 }
00441 
00442 //--------------------
00443 
00444 void
00445 AlgImgProc::setPeakSelectionPars(const float& npix_min, const float& npix_max,
00446                                  const float& amax_thr, const float& atot_thr, const float& son_min)
00447 {
00448   if(m_pbits & 512) MsgLog(_name(), info, "in setPeakSelectionPars, seg=" << m_seg);
00449   m_peak_npix_min = npix_min;
00450   m_peak_npix_max = npix_max;
00451   m_peak_amax_thr = amax_thr;
00452   m_peak_atot_thr = atot_thr;
00453   m_peak_son_min  = son_min;
00454 }
00455 
00456 //--------------------
00457 
00458 void
00459 AlgImgProc::_mergeConnectedPixelCouples(const fphoton_t& thr_on_max, const fphoton_t& thr_on_tot, const bool& DO_TEST)
00460 {
00461   // Available ndarrays:
00462   // m_nphoton - uint number of photons
00463   // m_fphoton - fractional number of photons [0,1)
00464   // m_mphoton - for test output only
00465   // m_local_maximums - map of local maximums
00466   // m_pixel_status - is not used
00467   // m_conmap - map of connected pixels (coupled pixels marked by the same group number) - busy pixels
00468 
00469   const unsigned* shape = m_fphoton.shape();
00470 
00471   if(m_pbits & 512) MsgLog(_name(), info, "in _mergeConnectedPixelCouples, seg=" << m_seg << "\n    in window: " << m_win);
00472 
00473   if(DO_TEST) {
00474     if(m_mphoton.empty()) 
00475        m_mphoton = make_ndarray<nphoton_t>(shape[0], shape[1]);
00476     std::fill_n(&m_mphoton[0][0], int(m_nphoton.size()), nphoton_t(0));
00477   }
00478 
00479   if(m_conmap.empty()) 
00480      m_conmap = make_ndarray<conmap_t>(shape[0], shape[1]);
00481   std::fill_n(m_conmap.data(), int(m_fphoton.size()), conmap_t(0));
00482 
00483   m_numreg=0;
00484 
00485   //if(m_pixel_status.empty()) 
00486   //   m_pixel_status = make_ndarray<pixel_status_t>(shape[0], shape[1]);
00487   //std::fill_n(&m_pixel_status[0][0], int(m_fphoton.size()), pixel_status_t(0));
00488 
00489   /*
00490   int rmin = (int)m_win.rowmin;
00491   int rmax = (int)m_win.rowmax;                           
00492   int cmin = (int)m_win.colmin;
00493   int cmax = (int)m_win.colmax;
00494   */
00495 
00496   if(v_indcross.empty()) _fillCrossIndexes();
00497 
00498   // loop over internal image pixels ignoring first and last rows and columns
00499   for(unsigned r = m_win.rowmin+1; r<m_win.rowmax-1; r++) {
00500     for(unsigned c = m_win.colmin+1; c<m_win.colmax-1; c++) {
00501 
00502       if(m_local_maximums[r][c] != 3)  continue; // check local maximums only
00503 
00504       if(m_fphoton[r][c] < thr_on_max) continue; // apply threshold on max
00505 
00506       fphoton_t vmax = 0.0;
00507       TwoIndexes ijmax(0,0);
00508 
00509       // find not-busy neighbor pixel with maximal intensity
00510       for(vector<TwoIndexes>::const_iterator ij  = v_indcross.begin();
00511                                              ij != v_indcross.end(); ij++) {
00512          int ir = r + (ij->i);
00513          int ic = c + (ij->j);
00514 
00515          if(m_conmap[ir][ic]) continue; // pixel is already used for other pair
00516 
00517          if(m_fphoton[ir][ic] < vmax) continue; // not a maximal neighbor
00518 
00519          vmax = m_fphoton[ir][ic];
00520          ijmax = *ij;
00521       }
00522 
00523       fphoton_t  vtot = m_fphoton[r][c];
00524       if(vmax>0) vtot += m_fphoton[r + ijmax.i][c + ijmax.j];
00525 
00526       if(vtot < thr_on_tot) continue; // if pair intensity is below total threshold
00527 
00528       m_numreg ++;
00529       m_conmap[r][c] = m_numreg;
00530       m_conmap[r + ijmax.i][c + ijmax.j] = m_numreg;
00531         
00532       // DO MERGE FOR A COUPLE OF SELECTED PIXELS      
00533       m_nphoton[r][c] ++; // increment number of photons
00534 
00535       if(DO_TEST) m_mphoton[r][c] = 1; // vtot*100;
00536 
00537     } // column loop
00538   } // row loop
00539 }
00540 
00541 //--------------------
00542 //--------------------
00543 //--------------------
00544 //--------------------
00545 
00546 void 
00547 AlgImgProc::printMatrixOfRingIndexes()
00548 {
00549   int indmax = (int)std::ceil(m_r0 + m_dr);
00550   int indmin = -indmax;
00551   unsigned counter = 0;
00552   std::stringstream ss; 
00553   ss << "printMatrixOfRingIndexes(), seg=" << m_seg << "  r0=" << m_r0 << "  dr=" << m_dr << '\n';
00554 
00555   for (int i = indmin; i <= indmax; ++ i) {
00556     for (int j = indmin; j <= indmax; ++ j) {
00557 
00558       float r = std::sqrt( float(i*i + j*j) );
00559       int status = ( r < m_r0 || r > m_r0 + m_dr ) ? 0 : 1;
00560       if (status) counter++;
00561       if (i==0 && j==0) ss << " +";
00562       else              ss << " " << status;
00563     }
00564     ss << '\n';
00565   }
00566   ss << "Number of pixels to estimate background = " << counter << '\n';
00567   MsgLog(_name(), info, ss.str());
00568 }
00569 
00570 //--------------------
00571 
00572 void 
00573 AlgImgProc::printVectorOfRingIndexes()
00574 {
00575   if(v_indexes.empty()) _evaluateRingIndexes(m_r0, m_dr);
00576 
00577   std::stringstream ss; 
00578   ss << "In printVectorOfRingIndexes:\n Vector size: " << v_indexes.size() << '\n';
00579   int n_pairs_in_line=0;
00580   for( vector<TwoIndexes>::const_iterator ij  = v_indexes.begin();
00581                                           ij != v_indexes.end(); ij++ ) {
00582     ss << " (" << ij->i << "," << ij->j << ")";
00583     if ( ++n_pairs_in_line > 9 ) {ss << "\n"; n_pairs_in_line=0;}
00584   }   
00585   MsgLog(_name(), info, ss.str());
00586 }
00587 
00588 //--------------------
00589 
00590 void 
00591 AlgImgProc::printMatrixOfDiagIndexes()
00592 {
00593   int indmax =  m_rank;
00594   int indmin = -m_rank;
00595 
00596   std::stringstream ss; 
00597   ss << "In printMatrixOfDiagIndexes, seg=" << m_seg << "  rank=" << m_rank << '\n';
00598 
00599   for (int i = indmin; i <= indmax; ++ i) {
00600     for (int j = indmin; j <= indmax; ++ j) {
00601       int status = 1;
00602       if (i==0 || j==0) status = 0;
00603       //if (m_rank>2 && floor(std::sqrt(float(i*i + j*j)))>(int)m_rank) status = 0;
00604       if (i==0 && j==0) ss << " +";
00605       else              ss << " " << status;
00606     }
00607     ss << '\n';
00608   }
00609 
00610   MsgLog(_name(), info, ss.str());
00611 }
00612 
00613 //--------------------
00614 
00615 void 
00616 AlgImgProc::printVectorOfDiagIndexes()
00617 {
00618   if(v_inddiag.empty()) _evaluateDiagIndexes(m_rank);
00619 
00620   std::stringstream ss; 
00621   ss << "In printVectorOfDiagIndexes:\n Vector size: " << v_inddiag.size() << '\n';
00622   int n_pairs_in_line=0;
00623   for( vector<TwoIndexes>::const_iterator ij  = v_inddiag.begin();
00624                                           ij != v_inddiag.end(); ij++ ) {
00625     ss << " (" << ij->i << "," << ij->j << ")";
00626     if ( ++n_pairs_in_line > 9 ) {ss << "\n"; n_pairs_in_line=0;}
00627   }   
00628 
00629   MsgLog(_name(), info, ss.str());
00630 }
00631 
00632 //--------------------
00633 
00634 void 
00635 AlgImgProc::_printStatisticsOfLocalExtremes()
00636 {
00637   std::stringstream ss; 
00638   ss << "In _printStatisticsOfLocalExtremes: seg=" << m_seg << "  rank=" << m_rank << '\n';
00639   ss << "1=c 2=r 4=rect" << std::right << '\n';
00640   unsigned hismax[8] = {}; // all zeros
00641   unsigned hismin[8] = {}; // all zeros
00642   unsigned totmax = 0;
00643   unsigned totmin = 0;
00644   ndarray<pixel_maximums_t, 2>::iterator itx;
00645   ndarray<pixel_minimums_t, 2>::iterator itn;
00646   for(itx=m_local_maximums.begin(); itx!=m_local_maximums.end(); ++itx) {hismax[*itx]++; if(*itx) totmax++;}
00647   for(itn=m_local_minimums.begin(); itn!=m_local_minimums.end(); ++itn) {hismin[*itn]++; if(*itn) totmin++;}
00648 
00649   ss << "bin#    : "; for(int i=0; i<8; i++) ss << std::setw(8) << i;         ss << "     total\n";
00650   ss << "maximums: "; for(int i=0; i<8; i++) ss << std::setw(8) << hismax[i]; ss << "  " << std::setw(8) << totmax << '\n';
00651   ss << "minimums: "; for(int i=0; i<8; i++) ss << std::setw(8) << hismin[i]; ss << "  " << std::setw(8) << totmin << '\n';
00652 
00653   MsgLog(_name(), info, ss.str());
00654 }
00655 
00656 //--------------------
00657   std::ostream& 
00658   operator<<(std::ostream& os, const Peak& p) 
00659   {
00660     os << fixed
00661        << "Seg:"      << std::setw( 3) << std::setprecision(0) << p.seg
00662        << " Row:"     << std::setw( 4) << std::setprecision(0) << p.row              
00663        << " Col:"     << std::setw( 4) << std::setprecision(0) << p.col               
00664        << " Npix:"    << std::setw( 3) << std::setprecision(0) << p.npix    
00665        << " Imax:"    << std::setw( 7) << std::setprecision(1) << p.amp_max                   
00666        << " Itot:"    << std::setw( 7) << std::setprecision(1) << p.amp_tot           
00667        << " CGrav r:" << std::setw( 6) << std::setprecision(1) << p.row_cgrav         
00668        << " c:"       << std::setw( 6) << std::setprecision(1) << p.col_cgrav                 
00669        << " Sigma r:" << std::setw( 5) << std::setprecision(2) << p.row_sigma         
00670        << " c:"       << std::setw( 5) << std::setprecision(2) << p.col_sigma         
00671        << " Rows["    << std::setw( 4) << std::setprecision(0) << p.row_min           
00672        << ":"         << std::setw( 4) << std::setprecision(0) << p.row_max           
00673        << "] Cols["   << std::setw( 4) << std::setprecision(0) << p.col_min           
00674        << ":"         << std::setw( 4) << std::setprecision(0) << p.col_max          
00675        << "] B:"      << std::setw( 5) << std::setprecision(1) << p.bkgd              
00676        << " N:"       << std::setw( 5) << std::setprecision(1) << p.noise            
00677        << " S/N:"     << std::setw( 5) << std::setprecision(1) << p.son;
00678     return os;
00679   }
00680 
00681 //--------------------
00682   std::ostream& 
00683   operator<<(std::ostream& os, const BkgdAvgRms& b) 
00684   {
00685     os << fixed
00686        << " Bkgd avg:" << std::setw( 7) << std::setprecision(1) << b.avg
00687        << " RMS:"      << std::setw( 7) << std::setprecision(1) << b.rms
00688        << " Npix:"     << std::setw( 4) << std::setprecision(0) << b.npx;
00689     return os;
00690   }
00691 
00692 //--------------------
00693 //--------------------
00694 //-- NON-CLASS METHODS
00695 //--------------------
00696 
00697 ndarray<const AlgImgProc::pixel_maximums_t, 2>
00698 mapOfLocalMaximumsRank1Cross(const ndarray<const AlgImgProc::fphoton_t,2> fphoton)
00699 {
00700   AlgImgProc* algo = new AlgImgProc(0); // , 0, 1e6, 0, 1e6, 1023);
00701   algo->validate_window(fphoton.shape());
00702   algo->_makeMapOfLocalMaximumsRank1Cross<AlgImgProc::fphoton_t>(fphoton);
00703   return algo->mapOfLocalMaximums();
00704 }
00705 
00706 //--------------------
00707 
00708 template bool AlgImgProc::_findConnectedPixelsInRegionV1<float>(const ndarray<const float,2>&, const int&, const int&);
00709 template bool AlgImgProc::_findConnectedPixelsInRegionV1<double>(const ndarray<const double,2>&, const int&, const int&);
00710 template bool AlgImgProc::_findConnectedPixelsInRegionV1<int>(const ndarray<const int,2>&, const int&, const int&);
00711 template bool AlgImgProc::_findConnectedPixelsInRegionV1<int16_t>(const ndarray<const int16_t,2>&, const int&, const int&);
00712 template bool AlgImgProc::_findConnectedPixelsInRegionV1<uint16_t>(const ndarray<const uint16_t,2>&, const int&, const int&);
00713 
00714 //--------------------
00715 
00716 template void AlgImgProc::_findConnectedPixelsInRegionV2<float>(const ndarray<const float,2>&, const ndarray<const mask_t,2>&, const int&, const int&);
00717 template void AlgImgProc::_findConnectedPixelsInRegionV2<double>(const ndarray<const double,2>&, const ndarray<const mask_t,2>&, const int&, const int&);
00718 template void AlgImgProc::_findConnectedPixelsInRegionV2<int>(const ndarray<const int,2>&, const ndarray<const mask_t,2>&, const int&, const int&);
00719 template void AlgImgProc::_findConnectedPixelsInRegionV2<int16_t>(const ndarray<const int16_t,2>&, const ndarray<const mask_t,2>&, const int&, const int&);
00720 template void AlgImgProc::_findConnectedPixelsInRegionV2<uint16_t>(const ndarray<const uint16_t,2>&, const ndarray<const mask_t,2>&, const int&, const int&);
00721 
00722 //--------------------
00723 //--------------------
00724 } // namespace ImgAlgos
00725 //--------------------
00726 

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7