OTB  9.0.0
Orfeo Toolbox
otbStreamingFeatherMosaicFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  * Copyright (C) 2016-2019 IRSTEA
5  *
6  * This file is part of Orfeo Toolbox
7  *
8  * https://www.orfeo-toolbox.org/
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 #ifndef __StreamingFeatherMosaicFilter_hxx
23 #define __StreamingFeatherMosaicFilter_hxx
24 
26 
27 namespace otb
28 {
29 
30 template <class TInputImage, class TOutputImage, class TDistanceImage, class TInternalValueType>
32 {
33  m_FeatheringTransitionDistance = 500;
34  m_FeatheringSmoothness = 1.5;
35 }
36 
40 template <class TInputImage, class TOutputImage, class TDistanceImage, class TInternalValueType>
42  const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
43 {
44 
45  // Debug info
46  itkDebugMacro(<< "Actually executing thread " << threadId << " in region " << outputRegionForThread);
47 
48  // Support progress methods/callbacks
49  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
50 
51  // Get output pointer
52  OutputImageType* mosaicImage = this->GetOutput();
53 
54  // Get number of used inputs
55  const unsigned int nbOfUsedInputImages = Superclass::GetNumberOfUsedInputImages();
56 
57  // Get number of bands
58  const unsigned int nBands = Superclass::GetNumberOfBands();
59 
60  // Iterate through the thread region
61  IteratorType outputIt(mosaicImage, outputRegionForThread);
62 
63  // Prepare input pointers, interpolators, and valid regions (input images)
64  typename std::vector<InputImageType*> currentImage;
65  typename std::vector<InterpolatorPointerType> interp;
66  Superclass::PrepareImageAccessors(currentImage, interp);
67 
68  // Prepare input pointers, interpolators, and valid regions (distances images)
69  typename std::vector<DistanceImageType*> currentDistanceImage;
70  typename std::vector<DistanceImageInterpolatorPointer> distanceInterpolator;
71  Superclass::PrepareDistanceImageAccessors(currentDistanceImage, distanceInterpolator);
72 
73  // Temporary pixels
74  InternalPixelType interpolatedMathPixel, tempOutputPixel;
75  interpolatedMathPixel.SetSize(nBands);
76  tempOutputPixel.SetSize(nBands);
77 
78  InputImagePixelType interpolatedPixel;
79  InternalValueType pixelValue, distanceImagePixel;
80  bool isDataInCurrentOutputPixel;
81 
82  // Temporary coordinates
83  OutputImagePointType geoPoint;
84 
85  unsigned int band, i;
86 
87  for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt)
88  {
89  // Current pixel --> Geographical point
90  mosaicImage->TransformIndexToPhysicalPoint(outputIt.GetIndex(), geoPoint);
91 
92  // Presence of at least one non-null pixel of the used input images
93  isDataInCurrentOutputPixel = false;
94 
95  // Transition pixels
96  tempOutputPixel.Fill(0.0);
97 
98  // Loop on used input images
99  for (i = 0; i < nbOfUsedInputImages; i++)
100  {
101 
102  // Check if the point is inside the transformed thread region
103  // (i.e. the region in the current input image which match the thread
104  // region)
105  if (interp[i]->IsInsideBuffer(geoPoint))
106  {
107 
108  // Compute the interpolated pixel value
109  interpolatedPixel = interp[i]->Evaluate(geoPoint);
110 
111  // Check that interpolated pixel is not empty
112  if (Superclass::IsPixelNotEmpty(interpolatedPixel))
113  {
114 
115  // Geographical point (current pixel) --> Continuous index (from
116  // current input image)
117  if (distanceInterpolator[i]->IsInsideBuffer(geoPoint))
118  {
119  distanceImagePixel = distanceInterpolator[i]->Evaluate(geoPoint);
120  distanceImagePixel -= Superclass::GetDistanceOffset();
121 
122  // Check that the distance is positive (i.e. we are inside the valid
123  // area of the image)
124  if (distanceImagePixel > 0 || !isDataInCurrentOutputPixel)
125  {
126 
127  // Two possibility: distance < buffer or not
128  InternalValueType coef1, coef2;
129  if (distanceImagePixel < m_FeatheringTransitionDistance && isDataInCurrentOutputPixel)
130  {
131  // Feather the image
132  coef1 = distanceImagePixel / m_FeatheringTransitionDistance;
133  coef1 = vcl_pow(coef1, m_FeatheringSmoothness);
134  coef2 = 1.0 - coef1;
135  }
136  else
137  {
138  // Do not feather, put the image foreground
139  coef1 = 1.0;
140  coef2 = 0.0;
141  }
142 
143  // Average-Weight pixels (current pixel, and temporary pixel)
144  const unsigned int inputImageIndex = Superclass::GetUsedInputImageIndice(i);
145  for (band = 0; band < nBands; band++)
146  {
147  // Cast the interpolated pixel to a internal pixel type
148  interpolatedMathPixel[band] = static_cast<InternalValueType>(interpolatedPixel[band]);
149 
150  // Shift-scale the input pixel value
151  if (this->GetShiftScaleInputImages())
152  {
153  this->ShiftScaleValue(interpolatedMathPixel[band], inputImageIndex, band);
154  }
155 
156  // Average-Weight
157  tempOutputPixel[band] = coef1 * interpolatedMathPixel[band] + coef2 * tempOutputPixel[band];
158 
159  } // loop on pixels band
160 
161  // Update the presence of data for this pixel
162  isDataInCurrentOutputPixel = true;
163 
164  } // Distance value is positive
165 
166  } // Interpolated distance pixel not empty
167 
168  else
169  {
170  itkWarningMacro(<< "Unable to evaluate distance at point " << geoPoint);
171  distanceImagePixel = 0;
172  }
173 
174  } // Interpolated pixel is not empty
175  } // point inside buffer
176  } // next image
177 
178  // Prepare output pixel
179  OutputImagePixelType outputPixel(Superclass::GetNoDataOutputPixel());
180 
181  if (isDataInCurrentOutputPixel)
182  {
183 
184  // Compute output pixel
185  for (band = 0; band < nBands; band++)
186  {
187 
188  // Normalize & cast
189  pixelValue = tempOutputPixel[band];
190  Superclass::NormalizePixelValue(pixelValue);
191  outputPixel[band] = static_cast<OutputImageInternalPixelType>(pixelValue);
192  }
193 
194  } // if data presence
195 
196  // Update output pixel value
197  outputIt.Set(outputPixel);
198 
199  // Update progress
200  progress.CompletedPixel();
201 
202  } // next output pixel
203 }
204 
205 } // end namespace gtb
206 
207 #endif
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::OutputImageType
Superclass::OutputImageType OutputImageType
Definition: otbStreamingFeatherMosaicFilter.h:81
otb::StreamingFeatherMosaicFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbStreamingFeatherMosaicFilter.hxx:41
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::InternalValueType
Superclass::InternalValueType InternalValueType
Definition: otbStreamingFeatherMosaicFilter.h:92
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::IteratorType
Superclass::IteratorType IteratorType
Definition: otbStreamingFeatherMosaicFilter.h:99
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::OutputImagePixelType
Superclass::OutputImagePixelType OutputImagePixelType
Definition: otbStreamingFeatherMosaicFilter.h:84
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::InputImagePixelType
Superclass::InputImagePixelType InputImagePixelType
Definition: otbStreamingFeatherMosaicFilter.h:73
otbStreamingFeatherMosaicFilter.h
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::OutputImageRegionType
Superclass::OutputImageRegionType OutputImageRegionType
Definition: otbStreamingFeatherMosaicFilter.h:89
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::InternalPixelType
Superclass::InternalPixelType InternalPixelType
Definition: otbStreamingFeatherMosaicFilter.h:98
otb::StreamingFeatherMosaicFilter::StreamingFeatherMosaicFilter
StreamingFeatherMosaicFilter()
Definition: otbStreamingFeatherMosaicFilter.hxx:31
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::OutputImageInternalPixelType
Superclass::OutputImageInternalPixelType OutputImageInternalPixelType
Definition: otbStreamingFeatherMosaicFilter.h:88
otb::StreamingFeatherMosaicFilter< TInputImage, TOutputImage, TDistanceImage >::OutputImagePointType
Superclass::OutputImagePointType OutputImagePointType
Definition: otbStreamingFeatherMosaicFilter.h:83