00001 #ifndef IMGALGOS_ALGSMEARING_H
00002 #define IMGALGOS_ALGSMEARING_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <string>
00017 #include <iostream>
00018 #include <cstddef>
00019 #include <cstring>
00020
00021 using namespace std;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "MsgLogger/MsgLogger.h"
00033 #include "ndarray/ndarray.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 namespace ImgAlgos {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 class AlgSmearing {
00122 public:
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 AlgSmearing ( const double& sigma = 2
00143 , const int& nsm = 5
00144 , const double& thr_low = -1e10
00145 , const unsigned& opt = 1
00146 , const unsigned& pbits = 0
00147 , const size_t& seg = -1
00148 , const size_t& rowmin = 0
00149 , const size_t& rowmax = 1e6
00150 , const size_t& colmin = 0
00151 , const size_t& colmax = 1e6
00152 , const double& value = 0
00153 ) ;
00154
00155
00156 virtual ~AlgSmearing () {}
00157
00158
00159
00160
00161
00162
00163
00164 void evaluateWeights();
00165
00166
00167 void printWeights();
00168
00169
00170 void printInputPars();
00171
00172
00173
00174
00175
00176
00177
00178 double weight(int dr, int dc) { return m_weights[abs(dr)][abs(dc)]; }
00179
00180
00181 const size_t& segind(){ return m_seg; }
00182
00183
00184 AlgSmearing ( const AlgSmearing& ) ;
00185 AlgSmearing& operator = ( const AlgSmearing& ) ;
00186
00187
00188 protected:
00189
00190 private:
00191
00192 double m_sigma;
00193 int m_nsm;
00194 int m_nsm1;
00195 double m_thr_low;
00196 unsigned m_opt;
00197 unsigned m_pbits;
00198 size_t m_seg;
00199 size_t m_rowmin;
00200 size_t m_rowmax;
00201 size_t m_colmin;
00202 size_t m_colmax;
00203 double m_value;
00204
00205 ndarray<double,2> m_weights;
00206
00207
00208 std::string _name(){return std::string("ImgAlgos::AlgSmearing");}
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 template <typename T>
00220 double _smearPixAmp(const ndarray<const T,2>& nda_raw, const size_t& r0, const size_t& c0)
00221 {
00222 const T *p_raw = nda_raw.data();
00223 size_t nrows = nda_raw.shape()[0];
00224 size_t ncols = nda_raw.shape()[1];
00225
00226 double sum_aw = 0;
00227 double sum_w = 0;
00228 double w = 0;
00229 unsigned ind = 0;
00230
00231 int rmin = std::max(0, int(r0-m_nsm));
00232 int rmax = std::min(nrows, r0+m_nsm+1);
00233 int cmin = std::max(0, int(c0-m_nsm));
00234 int cmax = std::min(ncols, c0+m_nsm+1);
00235
00236 for (int r = rmin; r < rmax; r++) {
00237 for (int c = cmin; c < cmax; c++) {
00238
00239 ind = r*ncols + c;
00240 w = weight(r-r0, c-c0);
00241 sum_w += w;
00242 sum_aw += p_raw[ind] * w;
00243
00244
00245 }
00246 }
00247 return (sum_w>0)? sum_aw / sum_w : m_value;
00248 }
00249
00250
00251
00252
00253
00254
00255 public:
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 template <typename T>
00268 void smearing(const ndarray<const T,2>& nda_raw, ndarray<T,2>& nda_sme)
00269 {
00270 const T* p_raw = nda_raw.data();
00271 T* p_sme = nda_sme.data();
00272
00273 if(m_sigma == 0) { std::memcpy(p_sme, p_raw, nda_raw.size()*sizeof(T)); return; }
00274
00275 T thr = (T) m_thr_low;
00276 T val = (T) m_value;
00277 size_t nrows = nda_raw.shape()[0];
00278 size_t ncols = nda_raw.shape()[1];
00279
00280 size_t rmin = std::max(0, int(m_rowmin));
00281 size_t rmax = std::min(nrows, m_rowmax+1);
00282 size_t cmin = std::max(0, int(m_colmin));
00283 size_t cmax = std::min(ncols, m_colmax+1);
00284
00285 if(m_pbits & 128) MsgLog(_name(), info, " seg:" << m_seg
00286 << " nrows:" << nrows
00287 << " ncols:" << ncols
00288 << " rmin:" << rmin
00289 << " rmax:" << rmax
00290 << " cmin:" << cmin
00291 << " cmax:" << cmax
00292 << " nda_raw.size:" << nda_raw.size()
00293 << " nda_sme.size:" << nda_sme.size()
00294 );
00295
00296 if (m_opt==0) std::fill_n(p_sme, int(nda_raw.size()), T(m_value));
00297 else if(m_opt==1) std::memcpy(p_sme, p_raw, nda_raw.size()*sizeof(T));
00298
00299 for (size_t r = rmin; r < rmax; r++) {
00300 for (size_t c = cmin; c < cmax; c++) {
00301 unsigned ind = r*ncols + c;
00302 p_sme[ind] = (p_raw[ind]>thr) ? (T)_smearPixAmp<T>(nda_raw, r, c) : val;
00303 }
00304 }
00305 }
00306
00307
00308
00309 };
00310
00311 }
00312
00313 #endif // IMGALGOS_ALGSMEARING_H