1 #include "icam_sample.h"
2 //:
3 // \file
4 #include "vil/vil_bilin_interp.h"
5 
sample(unsigned int ni_dest,unsigned int nj_dest,vil_image_view<float> const & source,icam_depth_transform const & dt,vnl_vector<double> & samples,vnl_vector<double> & mask,unsigned & n_samples)6 void icam_sample::sample( unsigned int ni_dest,  unsigned int nj_dest,
7                           vil_image_view<float> const& source,
8                           icam_depth_transform const& dt,
9                           vnl_vector<double>& samples,
10                           vnl_vector<double>& mask,
11                           unsigned& n_samples)
12 {
13   unsigned ni_source = source.ni(), nj_source = source.nj();
14   unsigned max_samples = (ni_dest-2)*(nj_dest-2);
15   samples.set_size(max_samples);
16   mask.set_size(max_samples);
17   samples.fill(0.0);
18   mask.fill(0.0);
19   double to_u, to_v;
20   unsigned index = 0;
21   n_samples = 0;
22 
23   unsigned dest_li = ni_dest-1, dest_lj = nj_dest-1;
24   unsigned src_li = ni_source-1, src_lj = nj_source-1;
25   unsigned src_istep = source.istep(), src_jstep = source.jstep();
26   const float* src_ptr = &source(0,0);
27   // scan the set of destination pixel locations (i, j) and determine
28   // their location in the source image (to_u, to_v). The mapping is
29   // provided by the depth transform, dt.
30   for (unsigned j = 1; j<dest_lj; j++)
31     for (unsigned i = 1; i<dest_li; i++)
32     {
33       if (!dt.transform(i,j,to_u, to_v))
34         continue;
35 
36       //check to see if the source is being accessed out of bounds
37       //then non bounds-checking bilinear interpolation can be called
38       if (to_u<0||to_v<0||to_u>=src_li||to_v>=src_lj) {
39         mask[index++] = 0.0f;
40         continue;
41       }
42 
43       //assume grey scale, plane = 0
44       //note that there is no bounds checking in this version
45       double v = vil_bilin_interp(to_u, to_v, src_ptr,
46                                   ni_source, nj_source,
47                                   src_istep, src_jstep);
48 
49       mask[index] = 1.0f;
50       samples[index++] = v;
51       n_samples++;
52     }
53 }
54 
resample(unsigned int ni_dest,unsigned int nj_dest,vil_image_view<float> const & source,icam_depth_transform const & dt,vil_image_view<float> & dest,vil_image_view<float> & mask,unsigned & n_samples)55 void icam_sample::resample(unsigned int ni_dest,  unsigned int nj_dest,
56                            vil_image_view<float> const& source,
57                            icam_depth_transform const& dt,
58                            vil_image_view<float>& dest,
59                            vil_image_view<float>& mask,
60                            unsigned& n_samples)
61 {
62   unsigned ni_source = source.ni(), nj_source = source.nj();
63   dest.set_size(ni_dest, nj_dest);
64   mask.set_size(ni_dest, nj_dest);
65   dest.fill(0.0f);
66   mask.fill(1.0f);
67   // scan the set of destination pixel locations (i, j) and determine
68   // their location in the source image (to_u, to_v). The mapping is
69   // provided by the depth transform, dt.
70   n_samples = 0;
71   double to_u, to_v;
72   for (unsigned j = 1; j<nj_dest-1; ++j)
73     for (unsigned i = 1; i<ni_dest-1; ++i) {
74       if (!dt.transform(i,j,to_u, to_v)) {
75         mask(i,j) = 0.0f;
76         continue;
77       }
78       if (to_u<0||to_v<0||to_u>=ni_source||to_v>=nj_source) {
79         mask(i,j) = 0.0f;
80         continue;
81       }
82       //assume grey scale, plane = 0
83       double v = vil_bilin_interp_safe(source, to_u, to_v);
84 #if 0
85       int iu = std::floor(to_u), iv = std::floor(to_v);
86       double v = source(iu, iv);
87 #endif
88       dest(i,j) = static_cast<float>(v);
89 
90       n_samples++;
91     }
92   //take care of borders
93   for (unsigned j = 0; j<nj_dest; ++j) {
94     mask(0,j) = 0.0f;
95     dest(0,j) = 0.0f;
96     mask(ni_dest-1,j) = 0.0f;
97     dest(ni_dest-1,j) = 0.0f;
98   }
99   for (unsigned i = 0; i<ni_dest; ++i) {
100     mask(i,0) = 0.0f;
101     dest(i,0) = 0.0f;
102     mask(i,nj_dest-1) = 0.0f;
103     dest(i,nj_dest-1) = 0.0f;
104   }
105 }
106 
107 
sample(vil_image_view<float> const & map_dest,vil_image_view<float> const & map_mask,vnl_vector<double> & samples,vnl_vector<double> & mask,unsigned & n_samples)108 void icam_sample::sample(vil_image_view<float> const& map_dest,
109                          vil_image_view<float> const& map_mask,
110                          vnl_vector<double>& samples,
111                          vnl_vector<double>& mask,
112                          unsigned& n_samples)
113 {
114   unsigned ni_dest = map_dest.ni(), nj_dest = map_dest.nj();
115   unsigned max_samples = (ni_dest-2)*(nj_dest-2);
116   samples.set_size(max_samples);
117   mask.set_size(max_samples);
118   samples.fill(0.0);
119   mask.fill(0.0);
120   unsigned index = 0;
121   n_samples = 0;
122   for (unsigned j = 1; j<nj_dest-1; ++j)
123     for (unsigned i = 1; i<ni_dest-1; ++i) {
124       if (map_mask(i,j) == 0.0f) {
125         mask[index++]=0.0f;
126         continue;
127       }
128       mask[index]=1.0f;
129       samples[index++] = map_dest(i,j);
130       n_samples++;
131     }
132 }
133