Go to the documentation of this file.00001
00002
00003 #include "bsta_int_histogram_2d.h"
00004
00005
00006 #include <vcl_cmath.h>
00007
00008 #include "bsta_gauss.h"
00009 #include "bsta_int_histogram_1d.h"
00010
00011
00012 bsta_int_histogram_2d::bsta_int_histogram_2d(const unsigned int nbins_x, const unsigned int nbins_y)
00013 {
00014 nbins_x_ = nbins_x;
00015 nbins_y_ = nbins_y;
00016 float diag_lgth = vcl_sqrt( static_cast<float>(nbins_x_*nbins_x_) + static_cast<float>(nbins_y_*nbins_y_));
00017 diag_ = static_cast<long int>(diag_lgth) + 2;
00018
00019
00020 counts_.resize(nbins_y_);
00021 for (unsigned row =0; row<nbins_y_; ++row)
00022 {
00023 vcl_vector<long int> temp(nbins_x_, 0);
00024 counts_[row]=temp;
00025 }
00026 }
00027
00028
00029 bsta_int_histogram_2d::~bsta_int_histogram_2d() {}
00030
00031
00032
00033 unsigned long int bsta_int_histogram_2d::get_area()
00034 {
00035 register unsigned long int area = 0;
00036 for (unsigned int j=0; j<nbins_y_; j++) {
00037 for (unsigned int i=0; i<nbins_x_; i++) {
00038 area = area + counts_[j][i];
00039 }
00040 }
00041 return area;
00042 }
00043
00044
00045
00046
00047 unsigned long int bsta_int_histogram_2d::get_max_val(unsigned int &imax, unsigned int &jmax)
00048 {
00049 register long int max = 0;
00050 for (unsigned int j=0; j<nbins_y_; j++)
00051 {
00052 for (unsigned int i=0; i<nbins_x_; i++)
00053 {
00054 if (counts_[j][i] > max)
00055 {
00056 max = counts_[j][i];
00057 imax = i;
00058 jmax = j;
00059 }
00060 }
00061 }
00062 return max;
00063 }
00064
00065
00066
00067 void bsta_int_histogram_2d::parzen(const float sigma)
00068 {
00069 if (sigma<=0)
00070 return;
00071 double sd = (double)sigma;
00072 double val = 0.0;
00073 int nx = nbins_x_;
00074 int ny = nbins_y_;
00075 vbl_array_2d<double> in(nx, ny), out(nx, ny);
00076 for (unsigned int j=0; j<nbins_y_; j++)
00077 {
00078 for (unsigned int i=0; i<nbins_x_; i++)
00079 {
00080 val = (double)(counts_[j][i]);
00081 in.put((int)i, (int)j, val);
00082 }
00083 }
00084 bsta_gauss::bsta_2d_gaussian(sd, in, out);
00085 for (unsigned int j=0; j<nbins_y_; j++)
00086 {
00087 for (unsigned int i=0; i<nbins_x_; i++)
00088 {
00089
00090 val = out.get((int)i, (int)j);
00091 counts_[j][i] = (unsigned int)(val +0.5);
00092 }
00093 }
00094 return;
00095 }
00096
00097
00098
00099 void bsta_int_histogram_2d::profile_histogram( bsta_int_histogram_1d &phist,
00100 bsta_int_histogram_1d &phist_x,
00101 bsta_int_histogram_1d &phist_y )
00102 {
00103
00104 float slope = static_cast<float>(nbins_y_)/static_cast<float>(nbins_x_);
00105 float inverse_slope = 1.0f/slope;
00106 #if 0 // unused variables ?!
00107 float diag_lgth = vcl_sqrt(1.0f + (inverse_slope*inverse_slope));
00108 float deltay = inverse_slope/diag_lgth;
00109 float deltax = 1.0f/diag_lgth;
00110 #endif
00111
00112 float dxintcpt = vcl_sqrt(1 + (slope*slope));
00113 float dyintcpt = vcl_sqrt(1 + (inverse_slope*inverse_slope));
00114 unsigned int xbox = static_cast<unsigned int>(vcl_ceil(dxintcpt));
00115 unsigned int ybox = static_cast<unsigned int>(vcl_ceil(dyintcpt));
00116
00117
00118
00119
00120
00121
00122
00123
00124 for (unsigned int i=0; i<diag_; i++)
00125 {
00126 phist.set_count(i, 0);
00127 float xaxis_proj = vcl_sqrt((i*i) + ((i*slope)+(i*slope)));
00128 float yaxis_proj = vcl_sqrt((i*i) + ((i/slope)+(i/slope)));
00129
00130
00131 float xpos = xaxis_proj;
00132 float ypos = 0.0;
00133
00134
00135
00136 while (xpos >= 0.0 && ypos < yaxis_proj)
00137 {
00138 unsigned int xindex = static_cast<int>(vcl_floor(xpos));
00139 unsigned int yindex = static_cast<int>(vcl_floor(ypos));
00140
00141
00142
00143
00144 for (unsigned int j=0; j<ybox-1; j++)
00145 {
00146 unsigned int yi = yindex + j;
00147 for (unsigned int k=0; k<xbox-1; k++)
00148 {
00149 unsigned int xi = xindex + k;
00150
00151
00152
00153
00154
00155 if (xi < nbins_x_ && yi < nbins_y_)
00156 {
00157 if (phist.get_count(i) < counts_[yi][xi])
00158 {
00159 phist.set_count(i, counts_[yi][xi]);
00160 phist_x.set_count(i, xi);
00161 phist_y.set_count(i, yi);
00162 }
00163 }
00164 }
00165 }
00166 }
00167 }
00168
00169 return;
00170 }
00171
00172
00173
00174
00175 bool bsta_int_histogram_2d::find_edge( unsigned int peak_y, unsigned int peak_x,
00176 float newslope, float edge_pct,
00177 unsigned int &edge_x, unsigned int &edge_y,
00178 bool front)
00179 {
00180 bool success = false;
00181
00182 long int peak_height = counts_[peak_y][peak_x];
00183 if (peak_height == 0) return success;
00184 long int limit = static_cast<long int>(peak_height * edge_pct);
00185
00186 float diag_lgth = vcl_sqrt(1.0f + (newslope*newslope));
00187 float delta_x = 1.0f / diag_lgth;
00188 float delta_y = newslope / diag_lgth;
00189
00190 float x_point = static_cast<float>(peak_x);
00191 float y_point = static_cast<float>(peak_y);
00192
00193
00194 while (x_point >= 0 && x_point < nbins_x_ && y_point >=0 && y_point < nbins_y_ )
00195 {
00196 if (front)
00197 {
00198 x_point -= delta_x;
00199 y_point += delta_y;
00200 }
00201 else
00202 {
00203 x_point += delta_x;
00204 y_point -= delta_y;
00205 }
00206 edge_x = static_cast<unsigned int>(x_point);
00207 edge_y = static_cast<unsigned int>(y_point);
00208
00209 if ( counts_[edge_y][edge_x] <= limit)
00210 {
00211 success = true;
00212 return success;
00213 }
00214 }
00215
00216
00217 return success;
00218 }