1 // This is brl/bpro/core/vil_pro/processes/vil_fill_holes_in_regions_process.cxx
2 #include <iostream>
3 #include <algorithm>
4 #include <bprb/bprb_func_process.h>
5 //:
6 // \file
7 
8 #include "vil/vil_image_view.h"
9 #include <vil/algo/vil_structuring_element.h>
10 #include <vil/algo/vil_blob.h>
11 #include <vil/algo/vil_binary_closing.h>
12 #ifdef _MSC_VER
13 #  include "vcl_msvc_warnings.h"
14 #endif
15 
16 //: Constructor
vil_fill_holes_in_regions_process_cons(bprb_func_process & pro)17 bool vil_fill_holes_in_regions_process_cons(bprb_func_process& pro)
18 {
19   //this process takes one input: the filename
20   bool ok=false;
21   std::vector<std::string> input_types;
22   input_types.emplace_back("vil_image_view_base_sptr");
23 
24   ok = pro.set_input_types(input_types);
25   if (!ok) return ok;
26 
27   std::vector<std::string> output_types;
28   output_types.emplace_back("vil_image_view_base_sptr");  // label image
29   ok = pro.set_output_types(output_types);
30   if (!ok) return ok;
31 
32   return true;
33 }
34 
35 //: Execute the process
vil_fill_holes_in_regions_process(bprb_func_process & pro)36 bool vil_fill_holes_in_regions_process(bprb_func_process& pro)
37 {
38   // Sanity check
39   if (pro.n_inputs()< 1) {
40     std::cout << "vil_fill_holes_in_regions_process: The number of inputs should be 1" << std::endl;
41     return false;
42   }
43 
44   // get the inputs
45   unsigned i=0;
46   vil_image_view_base_sptr in_img_ptr = pro.get_input<vil_image_view_base_sptr>(i++);
47 
48   if (auto *view=dynamic_cast<vil_image_view<unsigned char>* > (in_img_ptr.ptr()))
49   {
50     unsigned int ni = view->ni();
51     unsigned int nj = view->nj();
52 
53 
54     for (unsigned k = 0; k< ni; k++)
55     {
56       if (k % 10 == 0)
57         std::cout<<k<<std::endl;
58       for (unsigned l = 0; l< nj; l++)
59       {
60         if ((*view)(k,l) == 0)
61         {
62           unsigned char labels[256]={0};
63 
64           for (unsigned int s = k; (int)s >= std::max((int)k-10,0) ; s-- )
65             if ( (*view)(s,l) > 0 )
66             {
67               labels[(int)((*view)(s,l))]++;
68               break;
69             }
70           for (unsigned int s = k; (int)s < std::min((int)k+10,(int)ni) ; s++ )
71           if ( (*view)(s,l) > 0 )
72           {
73             labels[(int)((*view)(s,l))]++;
74             break;
75           }
76           unsigned int lmax = (unsigned int)std::max((int)l-10,(int)0);
77           for (unsigned int s = l; s > lmax  ; s-- )
78           if ( (*view)(k,s) > 0 )
79           {
80             labels[(int)((*view)(k,s))]++;
81             break;
82           }
83           for (unsigned int s = l; (int)s < std::min((int)l+10,(int)nj) ; s++ )
84           if ( (*view)(k,s) > 0 )
85           {
86             labels[(int)((*view)(k,s))]++;
87             break;
88           }
89 
90           for (unsigned int s = 0; s <256; s++)
91           {
92             if (labels[s] >= 3)
93             {
94               (*view)(k,l) = s;
95               break;
96             }
97           }
98         }
99       }
100     }
101     pro.set_output_val<vil_image_view_base_sptr>(0, view);
102     return true;
103   }
104 
105   std::cerr<<"Error! Require a Byte image\n";
106   return false;
107 }
108