RSLightFields
Disparity map estimator from 3D light fields
rslf_interpolation.hpp
1 #ifndef _RSLF_INTERPOLATION
2 #define _RSLF_INTERPOLATION
3 
4 
5 #include <rslf_types.hpp>
6 
7 
8 namespace rslf
9 {
10 
11 
12 //~ enum InterpolationType
13 //~ {
14  //~ NEAREST_NEIGHBOUR = 0,
15  //~ LINEAR = 1
16 //~ };
17 
18 /*
19  * *****************************************************************
20  * INTERPOLATION CLASSES
21  * *****************************************************************
22  */
23 
27 template<typename DataType>
29 {
30  public:
34  virtual DataType interpolate(const Mat& line_matrix, float index) = 0;
35 
40  virtual void interpolate_mat(const Mat& data_matrix, const Mat& indices, Mat& res, Mat& card_non_nan) = 0;
41 };
42 
46 template<typename DataType>
48 {
49  public:
51  DataType interpolate(const Mat& line_matrix, float index);
52  void interpolate_mat(const Mat& data_matrix, const Mat& indices, Mat& res, Mat& card_non_nan);
53 };
54 
58 template<typename DataType>
60 {
61  public:
63  DataType interpolate(const Mat& line_matrix, float index);
64  void interpolate_mat(const Mat& data_matrix, const Mat& indices, Mat& res, Mat& card_non_nan);
65 };
66 
67 
68 
71 
72 
73 
74 /*
75  * *****************************************************************
76  * IMPLEMENTATION
77  * Interpolation
78  * *****************************************************************
79  */
80 
81 template<typename DataType>
83 (
84  const Mat& line_matrix,
85  float index
86 )
87 {
88  int rounded_index = (int)std::round(index);
89  if (rounded_index < 0 || rounded_index > line_matrix.cols - 1)
90  return nan_type<DataType>();
91  return line_matrix.at<DataType>(0, rounded_index);
92 }
93 
94 template<typename DataType>
96 (
97  const Mat& data_matrix,
98  const Mat& indices,
99  Mat& res,
100  Mat& card_non_nan
101 )
102 {
103  // TODO is there a better way to vectorize?
104  //~ assert(indices.rows == data_matrix.rows);
105  //~ if (res.empty() || res.size != data_matrix.size || res.type() != data_matrix.type())
106  //~ res = cv::Mat::zeros(indices.rows, indices.cols, data_matrix.type());
107 
108  //~ // Round indices
109  //~ Mat round_indices_matrix;
110  //~ indices.convertTo(round_indices_matrix, CV_32SC1, 1.0, 0.0);
111  //~ res.setTo(zero_scalar<DataType>());
112  card_non_nan.setTo(0.0);
113 
114  // For each row
115  for (int r=0; r<indices.rows; r++) {
116  const DataType* data_ptr = data_matrix.ptr<DataType>(r);
117  DataType* res_ptr = res.ptr<DataType>(r);
118  const int* ind_ptr = indices.ptr<int>(r);
119  // For each col
120  for (int c=0; c<indices.cols; c++) {
121  int rounded_index = (int)std::round(ind_ptr[c]);
122  if (rounded_index > -1 && rounded_index < data_matrix.cols)
123  {
124  res_ptr[c] = data_ptr[rounded_index];
125  card_non_nan.at<float>(c) += 1.0;
126  }
127  else
128  {
129  res_ptr[c] = nan_type<DataType>();
130  }
131  }
132  }
133 }
134 
135 template<typename DataType>
137 (
138  const Mat& line_matrix,
139  float index
140 )
141 {
142  int rounded_index_inf = (int)std::floor(index);
143  int rounded_index_sup = (int)std::ceil(index);
144 
145  if (rounded_index_sup < 0 || rounded_index_inf > line_matrix.cols - 1)
146  return nan_type<DataType>();
147  if (rounded_index_sup == 0)
148  return line_matrix.at<DataType>(0, 0);
149  if (rounded_index_inf == line_matrix.cols - 1)
150  return line_matrix.at<DataType>(0, line_matrix.cols - 1);
151 
152  // Linear interpolation
153  float t = index - rounded_index_inf;
154  return line_matrix.at<DataType>(0, rounded_index_inf) * (1 - t) + line_matrix.at<DataType>(0, rounded_index_sup) * t;
155 }
156 
157 template<typename DataType>
159 (
160  const Mat& data_matrix,
161  const Mat& indices,
162  Mat& res,
163  Mat& card_non_nan
164 )
165 {
166  // TODO is there a better way to vectorize?
167  //~ assert(indices.rows == data_matrix.rows);
168  //~ if (res.empty() || res.size != data_matrix.size || res.type() != data_matrix.type())
169  //~ res = cv::Mat::zeros(indices.rows, indices.cols, data_matrix.type());
170  //~ res.setTo(zero_scalar<DataType>());
171  card_non_nan.setTo(0.0);
172 
173  // For each row
174  for (int r=0; r<indices.rows; r++) {
175  const DataType* data_ptr = data_matrix.ptr<DataType>(r);
176  DataType* res_ptr = res.ptr<DataType>(r);
177  const float* ind_ptr = indices.ptr<float>(r);
178  // For each col
179  for (int c=0; c<indices.cols; c++) {
180  int ind_i = (int)std::floor(ind_ptr[c]);
181  int ind_s = (int)std::ceil(ind_ptr[c]);
182  float ind_residue = ind_ptr[c] - ind_i;
183  if (!(ind_i < 0 || ind_s > data_matrix.cols - 1))
184  {
185  res_ptr[c] = (1-ind_residue)*data_ptr[ind_i] + ind_residue*data_ptr[ind_s];
186  card_non_nan.at<float>(c) += 1.0;
187  }
188  else
189  {
190  res_ptr[c] = nan_type<DataType>();
191  }
192  }
193  }
194 }
195 
196 }
197 
198 
199 #endif
virtual void interpolate_mat(const Mat &data_matrix, const Mat &indices, Mat &res, Mat &card_non_nan)=0
Get the matrix with interpolation value at the index on the provided line for each element...
Template linear interpolation class.
Definition: rslf_interpolation.hpp:59
Definition: rslf_depth_computation.hpp:14
virtual DataType interpolate(const Mat &line_matrix, float index)=0
Get the interpolation value at the requested index on the provided line.
Implement a toolbox of utilitary functions.
void interpolate_mat(const Mat &data_matrix, const Mat &indices, Mat &res, Mat &card_non_nan)
Get the matrix with interpolation value at the index on the provided line for each element...
Definition: rslf_interpolation.hpp:159
Nearest neighbour interpolation class.
Definition: rslf_interpolation.hpp:47
cv::Mat Mat
RSLF Matrix class (cv::Mat)
Definition: rslf_types.hpp:26
DataType interpolate(const Mat &line_matrix, float index)
Get the interpolation value at the requested index on the provided line.
Definition: rslf_interpolation.hpp:83
Pure virtual class implementing a generic interpolation instance.
Definition: rslf_interpolation.hpp:28
DataType interpolate(const Mat &line_matrix, float index)
Get the interpolation value at the requested index on the provided line.
Definition: rslf_interpolation.hpp:137
void interpolate_mat(const Mat &data_matrix, const Mat &indices, Mat &res, Mat &card_non_nan)
Get the matrix with interpolation value at the index on the provided line for each element...
Definition: rslf_interpolation.hpp:96