1 // This is gel/vifa/vifa_norm_params.cxx
2 #include <iostream>
3 #include <algorithm>
4 #include "vifa_norm_params.h"
5 
6 #undef ROI_SUPPORTED  // No TargetJr-style ROI supported yet
7 
8 #ifdef _MSC_VER
9 #  include "vcl_msvc_warnings.h"
10 #endif
11 #include "vifa_image_histogram.h"
12 #ifdef ROI_SUPPORTED
13 #include <ImageClasses/RectROI.h>
14 #endif  // ROI_SUPPORTED
15 
vifa_norm_params(float IntLow,float ProbLow,float IntHigh,float ProbHigh)16 vifa_norm_params::vifa_norm_params(float IntLow, float ProbLow, float IntHigh,
17                                    float ProbHigh)
18     : ilow(IntLow), plow(ProbLow), ihigh(IntHigh), phigh(ProbHigh)
19 
20 {
21   calculate_clip_points();
22 }
23 
24 vifa_norm_params::
vifa_norm_params(const vifa_norm_params & old_params)25 vifa_norm_params(const vifa_norm_params&  old_params)
26   : gevd_param_mixin(), vul_timestamp(), vbl_ref_count()
27 {
28   ilow = old_params.ilow;
29   plow = old_params.plow;
30   ihigh = old_params.ihigh;
31   phigh = old_params.phigh;
32   calculate_clip_points();
33 }
34 
recompute()35 void vifa_norm_params::recompute() { calculate_clip_points(); }
36 
37 float vifa_norm_params::
normalize(float raw_intensity) const38 normalize(float raw_intensity) const
39 {
40   if (imin_ == imax_)
41     return raw_intensity;
42 
43   if (raw_intensity <= imin_)
44     return 0.0f;
45 
46   if (raw_intensity >= imax_)
47     return 1.0f;
48 
49   return raw_intensity * slope_ + b_;
50 }
51 
52 bool vifa_norm_params::
get_norm_bounds(vil_image_view_base * img,float low_bound_pcent,float high_bound_pcent,float & normal_low,float & normal_high)53 get_norm_bounds(vil_image_view_base*  img,
54                 float                 low_bound_pcent,
55                 float                 high_bound_pcent,
56                 float&                normal_low,
57                 float&                normal_high)
58 {
59   if (img && ((low_bound_pcent != 0.0f) || (high_bound_pcent != 0.0f)))
60   {
61 #ifdef ROI_SUPPORTED
62     RectROI*  roi = img->GetROI();  // save the old ROI
63     roi->Protect();
64 
65     int xsize = img->width();
66     int  ysize = img->height();
67     RectROI*  temp_roi;
68 
69     // For large images (>4M) bound the ROI to max 1K border around ROI
70     if (xsize * ysize > 4000000)
71     {
72       int  border_size = 1000;
73       int startx = std::max(roi->GetOrigX() - border_size, 0);
74       int starty = std::max(roi->GetOrigY() - border_size, 0);
75       int roi_sizex = std::min(roi->GetSizeX() + 2 * border_size, xsize);
76       int roi_sizey = std::min(roi->GetSizeY() + 2 * border_size, ysize);
77       temp_roi = new RectROI(startx, starty, roi_sizex, roi_sizey);
78     }
79     else
80       temp_roi = new RectROI(0, 0, xsize, ysize);
81 
82     img->SetROI(temp_roi);
83 #endif  // ROI_SUPPORTED
84 
85     vifa_image_histogram  hist(img, 0.5f);
86     normal_low = hist.LowClipVal(0.01f * low_bound_pcent);
87     normal_high = hist.HighClipVal(0.01f * high_bound_pcent);
88 
89 #ifdef ROI_SUPPORTED
90     // Restore the original ROI
91     img->SetROI(roi);
92 #endif  // ROI_SUPPORTED
93 
94     return true;
95   }
96   else
97     return false;
98 }
99 
print_info() const100 void vifa_norm_params::print_info() const {
101   std::cout << "vifa_norm_params:\n"
102            << "  low % thresh    = " << plow << std::endl
103            << "  high % thresh   = " << phigh << std::endl
104            << "  low int thresh  = " << ilow << std::endl
105            << "  high int thresh = " << ihigh << std::endl
106            << "  int min         = " << imin_ << std::endl
107            << "  int max         = " << imax_ << std::endl;
108 }
109 
calculate_clip_points()110 void vifa_norm_params::calculate_clip_points() {
111   imin_ = 0.0f;
112   imax_ = 0.0f;
113 
114   float int_range = ihigh - ilow;
115   if (int_range < 1e-4)
116     return;
117 
118   float p_range = phigh - plow;
119   if (p_range < 1e-6)
120     return;
121 
122   // find m and b in y=mx+b
123 
124   slope_ = p_range / int_range;
125   b_ = (plow - (slope_ * ilow));
126 
127   // solve for x when y=0, y=1
128 
129   imin_ = (0.0f - b_) / slope_;
130   imax_ = (1.0f - b_) / slope_;
131 
132   //  std::cout << "slope: " << slope_ << " b: " << b_ << " imin: " << imin_
133   //           << " imax " << imax_ << std::endl;
134 }
135