contrib/gel/vifa/vifa_norm_params.cxx
Go to the documentation of this file.
00001 // This is gel/vifa/vifa_norm_params.cxx
00002 #include "vifa_norm_params.h"
00003 
00004 #undef ROI_SUPPORTED  // No TargetJr-style ROI supported yet
00005 
00006 #include <vcl_algorithm.h>
00007 #include <vcl_iostream.h>
00008 #include "vifa_image_histogram.h"
00009 #ifdef ROI_SUPPORTED
00010 #include <ImageClasses/RectROI.h>
00011 #endif  // ROI_SUPPORTED
00012 
00013 
00014 vifa_norm_params::
00015 vifa_norm_params(float  IntLow,
00016                  float  ProbLow,
00017                  float  IntHigh,
00018                  float  ProbHigh) :
00019   ilow(IntLow),
00020   plow(ProbLow),
00021   ihigh(IntHigh),
00022   phigh(ProbHigh),
00023   imin_(0.0f),
00024   imax_(0.0f),
00025   slope_(0.0f),
00026   b_(0.0f)
00027 {
00028   calculate_clip_points();
00029 }
00030 
00031 vifa_norm_params::
00032 vifa_norm_params(const vifa_norm_params&  old_params)
00033   : gevd_param_mixin(), vul_timestamp(), vbl_ref_count()
00034 {
00035   ilow = old_params.ilow;
00036   plow = old_params.plow;
00037   ihigh = old_params.ihigh;
00038   phigh = old_params.phigh;
00039   calculate_clip_points();
00040 }
00041 
00042 void vifa_norm_params::
00043 recompute(void)
00044 {
00045   calculate_clip_points();
00046 }
00047 
00048 float vifa_norm_params::
00049 normalize(float raw_intensity)
00050 {
00051   if (imin_ == imax_)
00052     return raw_intensity;
00053 
00054   if (raw_intensity <= imin_)
00055     return 0.0f;
00056 
00057   if (raw_intensity >= imax_)
00058     return 1.0f;
00059 
00060   return raw_intensity * slope_ + b_;
00061 }
00062 
00063 bool vifa_norm_params::
00064 get_norm_bounds(vil_image_view_base*  img,
00065                 float                 low_bound_pcent,
00066                 float                 high_bound_pcent,
00067                 float&                normal_low,
00068                 float&                normal_high)
00069 {
00070   if (img && ((low_bound_pcent != 0.0f) || (high_bound_pcent != 0.0f)))
00071   {
00072 #ifdef ROI_SUPPORTED
00073     RectROI*  roi = img->GetROI();  // save the old ROI
00074     roi->Protect();
00075 
00076     int xsize = img->width();
00077     int  ysize = img->height();
00078     RectROI*  temp_roi;
00079 
00080     // For large images (>4M) bound the ROI to max 1K border around ROI
00081     if (xsize * ysize > 4000000)
00082     {
00083       int  border_size = 1000;
00084       int startx = vcl_max(roi->GetOrigX() - border_size, 0);
00085       int starty = vcl_max(roi->GetOrigY() - border_size, 0);
00086       int roi_sizex = vcl_min(roi->GetSizeX() + 2 * border_size, xsize);
00087       int roi_sizey = vcl_min(roi->GetSizeY() + 2 * border_size, ysize);
00088       temp_roi = new RectROI(startx, starty, roi_sizex, roi_sizey);
00089     }
00090     else
00091       temp_roi = new RectROI(0, 0, xsize, ysize);
00092 
00093     img->SetROI(temp_roi);
00094 #endif  // ROI_SUPPORTED
00095 
00096     vifa_image_histogram  hist(img, 0.5f);
00097     normal_low = hist.LowClipVal(0.01f * low_bound_pcent);
00098     normal_high = hist.HighClipVal(0.01f * high_bound_pcent);
00099 
00100 #ifdef ROI_SUPPORTED
00101     // Restore the original ROI
00102     img->SetROI(roi);
00103 #endif  // ROI_SUPPORTED
00104 
00105     return true;
00106   }
00107   else
00108     return false;
00109 }
00110 
00111 void vifa_norm_params::
00112 print_info(void)
00113 {
00114   vcl_cout << "vifa_norm_params:\n"
00115            << "  low % thresh    = " << plow << vcl_endl
00116            << "  high % thresh   = " << phigh << vcl_endl
00117            << "  low int thresh  = " << ilow << vcl_endl
00118            << "  high int thresh = " << ihigh << vcl_endl
00119            << "  int min         = " << imin_ << vcl_endl
00120            << "  int max         = " << imax_ << vcl_endl;
00121 }
00122 
00123 void vifa_norm_params::
00124 calculate_clip_points(void)
00125 {
00126   imin_ = 0.0f;
00127   imax_ = 0.0f;
00128 
00129   float int_range = ihigh - ilow;
00130   if (int_range < 1e-4)
00131     return;
00132 
00133   float p_range = phigh - plow;
00134   if (p_range < 1e-6)
00135     return;
00136 
00137   // find m and b in y=mx+b
00138 
00139   slope_ = p_range / int_range;
00140   b_ = (plow - (slope_ * ilow));
00141 
00142   // solve for x when y=0, y=1
00143 
00144   imin_ = (0.0f - b_) / slope_;
00145   imax_ = (1.0f - b_) / slope_;
00146 
00147   //  vcl_cout << "slope: " << slope_ << " b: " << b_ << " imin: " << imin_
00148   //           << " imax " << imax_ << vcl_endl;
00149 }