15 #ifndef _TEMPORALGAPFILLING_H_
16 #define _TEMPORALGAPFILLING_H_
22 #include "itkBinaryFunctorImageFilter.h"
27 #include <gsl/gsl_errno.h>
28 #include <gsl/gsl_spline.h>
35 template<
typename ValueType,
typename VectorType>
39 itk::VariableLengthVector<ValueType> p(v.size());
40 for(
auto i=0; i<p.Size(); ++i)
41 p[i] = ValueType{v[i]};
45 template<
typename ValueType>
47 itk::VariableLengthVector<ValueType>
vectorToPixel(
const std::vector<ValueType>& v)
49 itk::VariableLengthVector<ValueType> p(v.size());
50 for(
size_t i=0; i<p.Size(); ++i)
51 p[i] = ValueType{v[i]};
56 template <
class TInputImage1,
class TInputImage2,
class TOutputImage,
59 public itk::BinaryFunctorImageFilter< TInputImage1, TInputImage2,
60 TOutputImage, TFunctor >
64 typedef itk::BinaryFunctorImageFilter< TInputImage1, TInputImage2,
76 itkSetMacro(NumberOfOutputBands,
unsigned int);
77 itkGetConstMacro(NumberOfOutputBands,
unsigned int);
86 Superclass::GenerateOutputInformation();
87 this->GetOutput()->SetNumberOfComponentsPerPixel( m_NumberOfOutputBands );
91 void operator =(
const Self&) =
delete;
100 template <
typename PixelType>
102 std::tuple<std::vector<typename PixelType::ValueType>,
103 std::vector<typename PixelType::ValueType>,
bool>
105 typename PixelType::ValueType valid_value)
107 using PValueType =
typename PixelType::ValueType;
108 using PVectorType =
typename std::vector<PValueType>;
111 PVectorType l_valid(nbDates,0);
112 PVectorType n_valid(nbDates,0);
115 for(
auto i=0; i<nbDates; i++)
117 if(mask[i]==(valid_value)) lv=i;
119 auto j = nbDates-1-i;
120 if(mask[j]==(valid_value)) nv=j;
123 return std::make_tuple(l_valid, n_valid, (lv==-1 && nv==nbDates));
130 template <
typename PixelType>
132 std::tuple<PixelType, PixelType, PixelType >
134 const PixelType mask,
137 typename PixelType::ValueType valid_value)
140 return std::make_tuple(pix, mask, odv);
142 unsigned int nbDates = idv.GetSize() + odv.GetSize();
143 PixelType opix{nbDates};
144 PixelType omask{nbDates};
145 PixelType dv{nbDates};
147 unsigned int dcount = 0;
148 unsigned int icount = 0;
149 unsigned int ocount = 0;
151 while(dcount < nbDates)
153 if(icount < idv.GetSize() &&
154 (ocount == odv.GetSize() ||
155 idv[icount] <= odv[ocount]))
157 opix[dcount] = pix[icount];
158 omask[dcount] = mask[icount];
159 dv[dcount] = idv[icount];
164 opix[dcount] =
typename PixelType::ValueType{0};
165 omask[dcount] = valid_value+1;
166 dv[dcount] = odv[ocount];
171 return std::make_tuple(opix, omask, dv);
176 template <
typename PixelType>
185 unsigned int nbDates = odv.GetSize();
186 unsigned int nbInDates = dv.GetSize();
188 if(nbDates > nbInDates)
189 throw std::invalid_argument(
"There are more output dates than input dates\n");
190 PixelType result{nbDates};
192 unsigned int in_count = 0;
193 unsigned int out_count = 0;
195 while(in_count < dv.GetSize() && out_count < nbDates)
197 if(dv[in_count] == odv[out_count])
199 result[out_count] = pix[in_count];
208 template <
typename PixelType>
217 if(pix.GetSize() != mask.GetSize())
218 throw std::invalid_argument(
"Pixel and mask have different sizes\n");
224 return (this->dates != a.dates) || (this->
dv != a.
dv) ;
229 return !(*
this != a);
237 template <
typename PixelType>
257 unsigned int nbDates = local_dv.GetSize();
258 if(nbDates == 0) nbDates = pix.GetSize();
259 if(nbDates != mask.GetSize())
260 throw std::invalid_argument(
"Pixel and mask have different sizes\n");
261 if(local_dv.GetSize()!=0 && nbDates != local_dv.GetSize())
263 std::stringstream errmessg;
264 errmessg <<
"Pixel and date vector have different sizes: "
265 << nbDates <<
" vs " << local_dv.GetSize() <<
"\n";
267 std::invalid_argument(errmessg.str());
269 if(local_dv.GetSize()==0)
272 for(
unsigned i=0; i<nbDates; i++)
275 if(local_odv.GetSize()==0) local_odv = local_dv;
277 unsigned int nbOutputDates = local_odv.GetSize();
278 PixelType validpix{nbDates};
280 PixelType invalidpix{nbOutputDates};
284 if(mask == validpix && local_dv == local_odv)
return pix;
286 PixelType tmp_pix, tmp_mask, tmp_dates;
287 std::tie(tmp_pix, tmp_mask, tmp_dates) =
294 std::tie(last_valid, next_valid, invalid_pixel) =
301 tmp_dates, last_valid,
303 tmp_dates, local_odv);
308 return (this->
dv != a.
dv) ;
313 return !(*
this != a);
318 PixelType
interpolate(
const PixelType& p,
const PixelType& m,
const PixelType& d,
321 unsigned int nbDates = p.GetSize();
322 PixelType result(nbDates);
323 for(
size_t i=0; i<nbDates; i++)
335 else if(nvp==nbDates)
344 double a = (y2-y1)/(x2-x1);
345 double b = ((y1+y2)*(x2-x1)-(y2-y1)*(x2+x1))/(2*(x2-x1));
347 result[i] = a*d[i]+b;
361 template <
typename PixelType>
383 unsigned int nbDates = local_dv.GetSize();
384 if(nbDates == 0) nbDates = pix.GetSize();
385 if(nbDates != mask.GetSize())
386 throw std::invalid_argument(
"Pixel and mask have different sizes\n");
387 if(local_dv.GetSize()!=0 && nbDates != local_dv.GetSize())
389 std::stringstream errmessg;
390 errmessg <<
"Pixel and date vector have different sizes: "
391 << nbDates <<
" vs " << local_dv.GetSize() <<
"\n";
393 std::invalid_argument(errmessg.str());
395 if(local_dv.GetSize()==0)
398 for(
size_t i=0; i<nbDates; i++)
401 if(local_odv.GetSize()==0) local_odv = local_dv;
403 unsigned int nbOutputDates = local_odv.GetSize();
404 PixelType validpix{nbDates};
406 PixelType invalidpix{nbOutputDates};
410 if(mask == validpix && local_dv == local_odv)
return pix;
412 PixelType tmp_pix, tmp_mask, tmp_dates;
413 std::tie(tmp_pix, tmp_mask, tmp_dates) =
420 std::tie(last_valid, next_valid, invalid_pixel) =
427 tmp_dates, last_valid,
429 tmp_dates, local_odv);
434 return (this->
dv != a.
dv) ;
439 return !(*
this != a);
448 unsigned int nbDates = p.GetSize();
450 double* x =
new double[nbDates];
451 double* y =
new double[nbDates];
452 std::size_t nbValidDates{0};
453 for(
size_t i = 0; i < nbDates; i++)
457 x[nbValidDates] = d[i];
460 (x[nbValidDates]-x[nbValidDates-1])<std::numeric_limits<double>::epsilon())
463 x[nbValidDates] = x[nbValidDates-1] + 0.1;
465 y[nbValidDates] = p[i];
473 gsl_interp_accel* acc = gsl_interp_accel_alloc();
475 if(spline ==
nullptr)
return p;
476 gsl_spline_init(spline, x, y, nbValidDates);
478 PixelType result(nbDates);
479 for(
size_t i=0; i<nbDates; i++)
491 else if(nvp==nbDates)
496 result[i] = gsl_spline_eval(spline, d[i], acc);
500 gsl_spline_free(spline);
501 gsl_interp_accel_free(acc);
515 return gsl_spline_alloc(gsl_interp_linear, nbDates);
519 return gsl_spline_alloc(gsl_interp_cspline, nbDates);
522 return gsl_spline_alloc(gsl_interp_akima, nbDates);
536 template <
typename PixelType,
typename FunctorType>
542 const auto nbComponents = p1.GetSize();
549 unsigned int outNbDates{0};
552 auto tmp1 = PixelType(nbDates);
553 auto tmp2 = PixelType(nbDates);
554 for(
size_t date=0; date<nbDates; date++)
560 if(p1.GetSize() == p2.GetSize())
562 for(
size_t date=0; date<nbDates; date++)
570 outNbDates = tmp_res.GetSize();
571 if(outNbDates > result.GetSize())
573 std::stringstream errmessg;
574 errmessg <<
"The result pixel has too many components: "
575 << outNbDates <<
" instead of expected max of "
576 << result.GetSize() << std::endl;
578 std::invalid_argument(errmessg.str());
580 for(
size_t date=0; date<outNbDates; date++)
586 result.SetSize(output_size);
591 const auto nbComponents = p1.GetSize();
595 std::stringstream errmessg;
597 <<
" components per date, but pixel has only "
598 << nbComponents <<
"\n";
600 std::invalid_argument(errmessg.str());
602 if(p1.GetSize()!=p2.GetSize() && p2.GetSize()!=nbDates)
604 std::stringstream errmessg;
605 errmessg <<
"p2 has to have either the same size as p1 "
606 <<
"or one component per date\n"
607 <<
"p1 is " << p1.GetSize() <<
"\n"
608 <<
"p2 is " << p2.GetSize() <<
"\n"
609 <<
"nbDates is " << nbDates <<
"\n";
611 std::invalid_argument(errmessg.str());
649 template <
typename ImageType,
typename FunctorType,
650 typename MultiComponentFunctorType =
654 typename ImageType::Pointer maskIma,
656 ImageType, ImageType, FunctorType>::Pointer filter,
658 ImageType, ImageType,
659 MultiComponentFunctorType>::Pointer filter_mc,
660 size_t components_per_date = 1,
661 typename ImageType::PixelType dv=
typename ImageType::PixelType{},
662 typename ImageType::PixelType odv=
typename ImageType::PixelType{})
665 inIma->UpdateOutputInformation();
666 maskIma->UpdateOutputInformation();
668 using TPixel =
typename ImageType::PixelType;
670 auto number_of_input_components = inIma->GetNumberOfComponentsPerPixel();
671 unsigned int number_of_output_components{number_of_input_components};
674 number_of_output_components = components_per_date*dv.GetSize();
678 number_of_output_components = components_per_date*odv.GetSize();
681 filter->SetNumberOfOutputBands(number_of_output_components);
683 filter_mc->SetNumberOfOutputBands(number_of_output_components);
685 if(components_per_date==1)
689 else if(dv != TPixel{})
690 filter->SetFunctor(FunctorType(dv));
691 filter->SetInput(0, inIma);
692 filter->SetInput(1, maskIma);
693 filter->UpdateOutputInformation();
699 (filter_mc->GetFunctor()).SetFunctor(FunctorType(dv, odv));
700 (filter_mc->GetFunctor()).SetMaxNumberOfOutputDates(odv.GetSize());
702 else if(dv != TPixel{})
703 (filter_mc->GetFunctor()).SetFunctor(FunctorType(dv));
704 (filter_mc->GetFunctor()).SetNumberOfComponentsPerDate(components_per_date);
705 filter_mc->SetInput(0, inIma);
706 filter_mc->SetInput(1, maskIma);
707 filter_mc->UpdateOutputInformation();