File: otbPixelSuppressionByDirectionImageFilter.hxx

package info (click to toggle)
otb 7.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,005,476 kB
  • sloc: cpp: 270,143; xml: 128,722; ansic: 4,367; sh: 1,768; python: 1,084; perl: 92; makefile: 72
file content (285 lines) | stat: -rw-r--r-- 9,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef otbPixelSuppressionByDirectionImageFilter_hxx
#define otbPixelSuppressionByDirectionImageFilter_hxx

#include "otbPixelSuppressionByDirectionImageFilter.h"

#include "itkDataObject.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkNeighborhoodInnerProduct.h"
#include "itkImageRegionIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkConstantBoundaryCondition.h"
#include "itkOffset.h"
#include "itkProgressReporter.h"
#include "otbMath.h"

namespace otb
{

/**
 *
 */
template <class TInputImage, class TOutputImage>
PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::PixelSuppressionByDirectionImageFilter()
{

  m_Radius.Fill(1);
  m_AngularBeam = static_cast<double>(0.);
}

template <class TInputImage, class TOutputImage>
void PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::SetInputImage(const InputImageType* input)
{
  this->SetInput(0, input);
}

template <class TInputImage, class TOutputImage>
void PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::SetInputImageDirection(const InputImageType* input)
{
  this->SetInput(1, input);
}

template <class TInputImage, class TOutputImage>
const typename PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::InputImageType*
PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::GetInputImage(void)
{
  if (this->GetNumberOfInputs() < 1)
  {
    return nullptr;
  }

  return static_cast<const TInputImage*>(this->GetInput(0));
}

template <class TInputImage, class TOutputImage>
const typename PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::InputImageType*
PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::GetInputImageDirection(void)
{
  if (this->GetNumberOfInputs() < 1)
  {
    return nullptr;
  }

  return static_cast<const TInputImage*>(this->GetInput(1));
}

template <class TInputImage, class TOutputImage>
void PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion()
{
  // call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();

  // get pointers to the input and output
  typename Superclass::InputImagePointer  inputPtr  = const_cast<TInputImage*>(this->GetInputImageDirection());
  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();

  if (!inputPtr || !outputPtr)
  {
    return;
  }

  // get a copy of the input requested region (should equal the output
  // requested region)
  typename TInputImage::RegionType inputRequestedRegion;
  inputRequestedRegion = inputPtr->GetRequestedRegion();

  // pad the input requested region by the operator radius
  inputRequestedRegion.PadByRadius(m_Radius);

  // crop the input requested region at the input's largest possible region
  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
  {
    inputPtr->SetRequestedRegion(inputRequestedRegion);
    return;
  }
  else
  {
    // Couldn't crop the region (requested region is outside the largest
    // possible region).  Throw an exception.

    // store what we tried to request (prior to trying to crop)
    inputPtr->SetRequestedRegion(inputRequestedRegion);

    // build an exception
    itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
    std::ostringstream               msg;
    msg << static_cast<const char*>(this->GetNameOfClass()) << "::GenerateInputRequestedRegion()";
    e.SetLocation(msg.str());
    e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
    e.SetDataObject(inputPtr);
    throw e;
  }
}

template <class TInputImage, class TOutputImage>
void PixelSuppressionByDirectionImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
                                                                                             itk::ThreadIdType threadId)
{

  itk::ConstantBoundaryCondition<InputImageType> cbc;
  const InputPixelType                           cvalue = 255;
  cbc.SetConstant(cvalue);

  itk::ConstNeighborhoodIterator<InputImageType> bit;
  itk::ImageRegionConstIterator<InputImageType>  itin;
  itk::ImageRegionIterator<OutputImageType>      itout;

  // Allocate output
  typename OutputImageType::Pointer     output         = this->GetOutput();
  typename InputImageType::ConstPointer input          = this->GetInputImage();
  typename InputImageType::ConstPointer inputDirection = this->GetInputImageDirection();

  // Find the data-set boundary "faces"
  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType           faceList;
  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;

  itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
  faceList = bC(inputDirection, outputRegionForThread, m_Radius);

  // support progress methods/callbacks
  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());

  // typename TInputImage::IndexType     bitIndex;

  //---------------------------------------------------------------------------

  InputPixelType PixelValue;

  // Location of the central pixel in the input image
  //  int Xc, Yc;

  // Pixel location in the system axis of the region
  int x, y;

  // Pixel location in the system axis of the region after rotation of theta
  // where theta is the direction of the cantral pixel

  // Pixel Direction
  double ThetaXcYc, Thetaxtyt;

  //---------------------------------------------------------------------------

  // Process each of the boundary faces.  These are N-d regions which border
  // the edge of the buffer.
  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
  {
    bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, inputDirection, *fit);

    itin  = itk::ImageRegionConstIterator<InputImageType>(input, *fit);
    itout = itk::ImageRegionIterator<OutputImageType>(output, *fit);

    bit.OverrideBoundaryCondition(&cbc);
    bit.GoToBegin();

    while (!bit.IsAtEnd())
    {

      /*// Location of the central pixel of the region in the input image
      bitIndex = bit.GetIndex();

      Xc = bitIndex[0];
      Yc = bitIndex[1];
      */
      // Get Pixel Direction from the image of directions
      ThetaXcYc = static_cast<double>(bit.GetCenterPixel());

      // Pixel intensity in the input image
      PixelValue = itin.Get();

      bool IsLine = false;

      typename itk::ConstNeighborhoodIterator<InputImageType>::OffsetType off;
      // Loop on the region
      for (unsigned int i = 0; i < 2 * m_Radius[0] + 1; ++i)
        for (unsigned int j = 0; j < 2 * m_Radius[1] + 1; ++j)
        {

          off[0] = i - m_Radius[0];
          off[1] = j - m_Radius[1];

          x = off[0];
          y = off[1];

          // No calculation on the central pixel
          if ((x == 0) && (y == 0))
            continue;

          Thetaxtyt = std::atan2(static_cast<double>(y), static_cast<double>(x)); // result is [-PI, PI]
          while (Thetaxtyt < 0)
            Thetaxtyt = CONST_PI + Thetaxtyt; // Theta is now [0, PI] as is
          // the result of detectors
          while (Thetaxtyt > CONST_PI_2)
            Thetaxtyt = Thetaxtyt - CONST_PI; // Theta is now [-PI/2, PI/2]

          if ((std::abs(std::cos(Thetaxtyt - ThetaXcYc)) >= std::cos(m_AngularBeam)) // this
              // pixel
              // is
              // in
              // the
              // angular beam
              && (std::abs(std::cos(bit.GetPixel(off) - ThetaXcYc)) >= std::cos(m_AngularBeam))) // and
          // its
          // direction
          // is
          // also
          // in
          // the beam
          {
            IsLine = true;
            continue;
          }
        }

      // end of the loop on the pixels of the region

      // Assignment of this value to the output pixel
      if (IsLine == true)
      {
        itout.Set(static_cast<OutputPixelType>(PixelValue));
      }
      else
      {
        itout.Set(static_cast<OutputPixelType>(0.));
      }

      ++bit;
      ++itin;
      ++itout;
      progress.CompletedPixel();
    }
  }
}

/**
 * Standard "PrintSelf" method
 */
template <class TInputImage, class TOutput>
void PixelSuppressionByDirectionImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "Radius: " << m_Radius << std::endl;
}

} // end namespace otb

#endif