1 // This is brl/bpro/core/vil_pro/processes/vil_threshold_image_process.cxx
2 #include <bprb/bprb_func_process.h>
3 //:
4 // \file
5 
6 #include <bprb/bprb_parameters.h>
7 #include "vil/vil_image_view.h"
8 #include "vil/vil_convert.h"
9 #include <vil/algo/vil_threshold.h>
10 
11 //: Constructor
vil_threshold_image_process_cons(bprb_func_process & pro)12 bool vil_threshold_image_process_cons(bprb_func_process& pro)
13 {
14     //input
15     bool ok=false;
16     std::vector<std::string> input_types;
17     input_types.emplace_back("vil_image_view_base_sptr");
18     input_types.emplace_back("float");     // threshold
19     input_types.emplace_back("bool");      // whether to threshold above or below, if true thresholds above, i.e. dest(i,j) = true if src(i,j) >= threshold
20     input_types.emplace_back("unsigned");  // the desired value of positive pixels in the output image
21     ok = pro.set_input_types(input_types);
22     if (!ok) return ok;
23 
24     //output
25     std::vector<std::string> output_types;
26     output_types.emplace_back("vil_image_view_base_sptr");
27     ok = pro.set_output_types(output_types);
28     if (!ok) return ok;
29     return true;
30 }
31 
32 //: Execute the process
vil_threshold_image_process(bprb_func_process & pro)33 bool vil_threshold_image_process(bprb_func_process& pro)
34 {
35     // Sanity check
36     if (pro.n_inputs()< 3) {
37         std::cout << "vil_threshold_image_process: The input number should be 3" << std::endl;
38         return false;
39     }
40 
41     unsigned i=0;
42     //Retrieve image from input
43     vil_image_view_base_sptr image = pro.get_input<vil_image_view_base_sptr>(i++);
44 
45     auto thres = pro.get_input<float>(i++);
46     bool thres_above = pro.get_input<bool>(i++);
47     auto positive_id = pro.get_input<unsigned>(i++);  // if passed as zero, return the bool image!
48 
49     // retrieve float image
50     vil_image_view_base_sptr fimage = vil_convert_cast(float(), image);
51     vil_image_view<float> fimg = *fimage;
52 
53     auto* temp = new vil_image_view<bool>;
54     if (thres_above) {
55         //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)>=t
56         vil_threshold_above(fimg, *temp, thres);
57     }
58     else {
59         //: Apply threshold such that dest(i,j,p)=true if src(i,j,p)<=t
60         vil_threshold_below(fimg, *temp, thres);
61     }
62     if (!positive_id) {
63         pro.set_output_val<vil_image_view_base_sptr>(0, temp);
64         return true;
65     }
66     auto* out = new vil_image_view<unsigned char>(temp->ni(),temp->nj());
67     for (unsigned k = 0 ; k < out->ni(); k++)
68     {
69         for (unsigned l = 0 ; l < out->nj(); l++)
70         {
71             if ((*temp)(k,l) )
72                 (*out)(k,l) =(unsigned char)(positive_id);
73             else
74                 (*out)(k,l) =0;
75         }
76     }
77 
78     pro.set_output_val<vil_image_view_base_sptr>(0, out);
79     return true;
80 }
81 
82 
83 
84 //: Constructor
vil_threshold_max_image_process_cons(bprb_func_process & pro)85 bool vil_threshold_max_image_process_cons(bprb_func_process& pro)
86 {
87     //input
88     bool ok=false;
89     std::vector<std::string> input_types;
90     input_types.emplace_back("vil_image_view_base_sptr");
91     input_types.emplace_back("float");   // fmax or threshold
92     ok = pro.set_input_types(input_types);
93     if (!ok) return ok;
94 
95     //output
96     std::vector<std::string> output_types;
97     output_types.emplace_back("vil_image_view_base_sptr");
98     ok = pro.set_output_types(output_types);
99     if (!ok) return ok;
100     return true;
101 }
102 
103 //: Execute the process
vil_threshold_max_image_process(bprb_func_process & pro)104 bool vil_threshold_max_image_process(bprb_func_process& pro)
105 {
106     // Sanity check
107     if (pro.n_inputs()< 2) {
108         std::cout << "vil_threshold_image_process: The input number should be 3" << std::endl;
109         return false;
110     }
111 
112     unsigned i=0;
113     //Retrieve image from input
114     vil_image_view_base_sptr image = pro.get_input<vil_image_view_base_sptr>(i++);
115     auto thres = pro.get_input<float>(i++);
116 
117     // retrieve float image
118     vil_image_view_base_sptr fimage = vil_convert_cast(float(), image);
119     vil_image_view<float> fimg = *fimage;
120 
121     float fmin = 0.0f;
122     float fmax = 0.0f;
123 
124 
125     vil_math_value_range<float>(fimg, fmin,fmax);
126 
127     auto* out = new vil_image_view<unsigned char>(fimage->ni(),fimage->nj());
128     out->fill(255);
129 
130     if(fmax > fmin)
131     {
132 
133         for (unsigned k = 0 ; k < out->ni(); k++)
134             for (unsigned l = 0 ; l < out->nj(); l++)
135                 if (fimg(k,l) > 0.9*fmax || fimg(k,l) > thres    )
136                     (*out)(k,l) = 0;
137     }
138 
139     pro.set_output_val<vil_image_view_base_sptr>(0, out);
140     return true;
141 }
142 
143 
144 
145 //: threshold an image such that dest(i,j,p)=true if min_thres<=src(i,j,p)<=max_thres if inside is true or dest(i,j,p)=true if src(i,j,p)<=min_thres or src(i,j,p)>=t1
vil_threshold_image_region_process_cons(bprb_func_process & pro)146 bool vil_threshold_image_region_process_cons(bprb_func_process& pro)
147 {
148     // input
149     bool ok=false;
150     std::vector<std::string> input_types(4);
151     input_types[0] = "vil_image_view_base_sptr";  // input image
152     input_types[1] = "float";                     // min threshold
153     input_types[2] = "float";                     // max threshold
154     input_types[3] = "bool";                      // whether to threshold inside or outside, if true threshold inside, i.e. dest(i,j,p)=true if t0<=src(i,j,p)<=t1
155     ok = pro.set_input_types(input_types);
156     if (!ok) return ok;
157 
158     // output
159     std::vector<std::string> output_types(1);
160     output_types[0] = "vil_image_view_base_sptr";
161     ok = pro.set_output_types(output_types);
162     if (!ok) return ok;
163     return true;
164 }
165 
166 //: execute the process
vil_threshold_image_region_process(bprb_func_process & pro)167 bool vil_threshold_image_region_process(bprb_func_process& pro)
168 {
169     // sanity check
170     if (!pro.verify_inputs())
171       return false;
172 
173     // get input
174     unsigned i = 0;
175     vil_image_view_base_sptr image = pro.get_input<vil_image_view_base_sptr>(i++);
176     auto min_thres = pro.get_input<float>(i++);
177     auto max_thres = pro.get_input<float>(i++);
178     bool thres_inside = pro.get_input<bool>(i++);
179 
180     // retrieve float image
181     vil_image_view_base_sptr fimage = vil_convert_cast(float(), image);
182     vil_image_view<float> fimg = *fimage;
183 
184     auto* temp = new vil_image_view<bool>;
185     if (thres_inside) {
186       // apply thresholds such that dest(i,j,p)=true if t0<=src(i,j,p)<=t1
187       vil_threshold_inside(fimg, *temp, min_thres, max_thres);
188     }
189     else {
190       // apply thresholds such that dest(i,j,p)=true if src(i,j,p)<=t0 or src(i,j,p)>=t1
191       vil_threshold_outside(fimg, *temp, min_thres, max_thres);
192     }
193 
194     auto* out = new vil_image_view<unsigned char>(temp->ni(), temp->nj());
195     for (unsigned k = 0 ; k < out->ni(); k++)
196     {
197         for (unsigned l = 0 ; l < out->nj(); l++)
198         {
199             if ((*temp)(k,l) )
200                 (*out)(k,l) =255;
201             else
202                 (*out)(k,l) =0;
203         }
204     }
205 
206     // output
207     pro.set_output_val<vil_image_view_base_sptr>(0, out);
208     return true;
209 }
210