Orfeo Toolbox  4.0
otbLineDetectorImageFilterBase.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbLineDetectorImageFilterBase_txx
19 #define __otbLineDetectorImageFilterBase_txx
20 
22 
23 #include "itkDataObject.h"
24 
28 #include "itkImageRegionIterator.h"
32 #include "itkProgressReporter.h"
33 #include "otbMacro.h"
34 #include "otbMath.h"
35 
36 namespace otb
37 {
38 
42 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
45 {
46  this->SetNumberOfRequiredOutputs(2);
47  this->SetNumberOfRequiredOutputs(1);
48  m_Radius.Fill(1);
49  m_LengthLine = 1;
50  m_WidthLine = 0;
51  m_Threshold = 0;
52  m_NumberOfDirections = 4;
53  m_FaceList.Fill(0);
54 // THOMAS : donc contructeur de base
55 // this->SetNthOutput(0, OutputImageType::New());
56 // this->SetNthOutput(1, OutputImageType::New());
57 }
58 
59 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
60 void
62 ::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
63  {
64  // call the superclass' implementation of this method
65  Superclass::GenerateInputRequestedRegion();
66 
67  // get pointers to the input and output
68  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput());
69  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
70 
71  if (!inputPtr || !outputPtr)
72  {
73  return;
74  }
75 
76  // get a copy of the input requested region (should equal the output
77  // requested region)
78  typename TInputImage::RegionType inputRequestedRegion;
79  inputRequestedRegion = inputPtr->GetRequestedRegion();
80 
81  // Define the size of the region by the radius
82  m_Radius[1] = static_cast<unsigned int>(3 * (2 * m_WidthLine + 1) + 2);
83  m_Radius[0] = 2 * m_LengthLine + 1;
84 
85  // Define the size of the facelist by taking into account the rotation of the region
86  m_FaceList[0] =
87  static_cast<unsigned int>(vcl_sqrt(static_cast<double>(
88  (m_Radius[0] * m_Radius[0]) + (m_Radius[1] * m_Radius[1]))) + 1);
89  m_FaceList[1] = m_FaceList[0];
90 
91  otbMsgDevMacro(<< "Radius : " << m_Radius[0] << " " << m_Radius[1]);
92  otbMsgDevMacro(<< "-> FaceList : " << m_FaceList[0] << " " << m_FaceList[1]);
93 
94  // pad the input requested region by the operator radius
95  inputRequestedRegion.PadByRadius(m_FaceList);
96 
97  // crop the input requested region at the input's largest possible region
98  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
99  {
100  inputPtr->SetRequestedRegion(inputRequestedRegion);
101  return;
102  }
103  else
104  {
105  // Couldn't crop the region (requested region is outside the largest
106  // possible region). Throw an exception.
107 
108  // store what we tried to request (prior to trying to crop)
109  inputPtr->SetRequestedRegion(inputRequestedRegion);
110 
111  // build an exception
112  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
113  std::ostringstream msg;
114  msg << static_cast<const char *>(this->GetNameOfClass())
115  << "::GenerateInputRequestedRegion()";
116  e.SetLocation(msg.str().c_str());
117  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
118  e.SetDataObject(inputPtr);
119  throw e;
120  }
121  }
122 
123 /*
124  * Set up state of filter before multi-threading.
125  * InterpolatorType::SetInputImage is not thread-safe and hence
126  * has to be set up before ThreadedGenerateData
127  */
128 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
129 void
132 {
133  typename OutputImageType::Pointer output = this->GetOutput();
134  output->FillBuffer(0);
135  typename OutputImageType::Pointer outputDirection = this->GetOutputDirection();
136  outputDirection->FillBuffer(0);
137 }
138 
139 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
140 void
143  const OutputImageRegionType& outputRegionForThread,
144  itk::ThreadIdType threadId
145  )
146 {
147 
148  typename InputImageType::ConstPointer input = this->GetInput();
149 
150  InterpolatorPointer interpolator2 = InterpolatorType::New();
151  interpolator2->SetInputImage(input);
152 
158  typedef itk::ImageRegionIterator<InputImageType> InputIteratorType;
159  typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
160 
161  // Allocate output
162  typename OutputImageType::Pointer output = this->GetOutput();
163  typename OutputImageType::Pointer outputDir = this->GetOutputDirection();
164 
165  // Find the data-set boundary "faces"
168 
170  faceList = bC(input, outputRegionForThread, m_FaceList);
171 
172  // support progress methods/callbacks
173  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
174 
175  typename TInputImage::IndexType bitIndex;
176  typename InterpolatorType::ContinuousIndexType Index;
177 
178  // --------------------------------------------------------------------------
179 
180  // Number of direction
181  const unsigned int NB_DIR = this->GetNumberOfDirections();
182  // Number of zone
183  const int NB_ZONE = 3;
184  // Definition of the 4 directions
185  //double Theta[NB_DIR];
186  //ROMAIN
187  double* Theta = new double[NB_DIR];
188 
189  // La rotation nulle correspond a un contour horizontal -> 0 !!
190  for (unsigned int i = 0; i < NB_DIR; ++i)
191  {
192  Theta[i] = (CONST_PI * (i / double(NB_DIR)));
193  /* if(Theta[i]>CONST_PI)
194  Theta[i] = Theta[i]-CONST_PI;
195  if((i/double(NB_DIR))==0.5)
196  Theta[i]=0.; */
197  }
198 
199  // Number of the zone
200  unsigned int zone;
201 
202  // Intensity of the linear feature
203  double R;
204 
205  // Direction of detection
206  double Direction = 0.;
207 
208  // Pixel location in the input image
209  int X, Y;
210 
211  // Pixel location after rotation in the system axis of the region
212  double xout, yout;
213 
214  // location of the central pixel in the input image
215  int Xc, Yc;
216 
217  // location of the central pixel between zone 1 and 2 and between zone 1 and 3
218  int Yc12, Yc13;
219 
220  //---------------------------------------------------------------------------
221  otbMsgDevMacro(<< "Theta : " << Theta[0] << " " << Theta[1] << " " << Theta[2] << " " << Theta[3]);
222 
223  // Process each of the boundary faces. These are N-d regions which border
224  // the edge of the buffer.
225 
226  //bool interiorFace = true;
227 
228  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
229  {
230  bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
231  cit = itk::ConstNeighborhoodIterator<InputImageType>(m_FaceList, input, *fit);
232 
234  itdir = itk::ImageRegionIterator<OutputImageType>(outputDir, *fit);
235 
236  bit.OverrideBoundaryCondition(&nbc);
237  bit.GoToBegin();
238  cit.OverrideBoundaryCondition(&nbc);
239  cit.GoToBegin();
240 
241  otbMsgDevMacro(<< " ------------------- FaceList --------------------------");
242 
243  while ((!bit.IsAtEnd()) && (!cit.IsAtEnd()))
244  {
245  InterpolatorPointer interpolator = InterpolatorType::New();
246  // Location of the central pixel of the region
247  off.Fill(0);
248  bitIndex = bit.GetIndex(off);
249  Xc = bitIndex[0];
250  Yc = bitIndex[1];
251 
252  // JULIEN : If the processed region is the center face
253  // the input image can be used for the interpolation
254  //if (interiorFace)
255  // {
256  // interpolator->SetInputImage(input);
257  // }
258  // else we must feed the interpolator with a partial image corresponding
259  // to the boundary conditions
260  //else
261  // {
262  typename InputImageType::RegionType tempRegion;
263  typename InputImageType::SizeType tempSize;
264  tempSize[0] = 2 * m_FaceList[0] + 1;
265  tempSize[1] = 2 * m_FaceList[1] + 1;
266  tempRegion.SetSize(tempSize);
268  tempIndex[0] = off[0] - m_FaceList[0];
269  tempIndex[1] = off[1] - m_FaceList[1];
270  tempRegion.SetIndex(cit.GetIndex(tempIndex));
271  typename InputImageType::Pointer tempImage = InputImageType::New();
272  tempImage->SetRegions(tempRegion);
273  tempImage->Allocate();
274 
275  for (unsigned int p = 0; p <= 2 * m_FaceList[0]; ++p)
276  {
277  for (unsigned int q = 0; q <= 2 * m_FaceList[1]; q++)
278  {
280  index[0] = p - m_FaceList[0];
281  index[1] = q - m_FaceList[1];
282  tempImage->SetPixel(cit.GetIndex(index), cit.GetPixel(index));
283  }
284  }
285  interpolator->SetInputImage(tempImage);
286  // }
287 
288  // Location of the central pixel between zone 1 and zone 2
289  Yc12 = Yc - m_WidthLine - 1;
290 
291  // Location of the central pixel between zone 1 and zone 3
292  Yc13 = Yc + m_WidthLine + 1;
293 
294  // Contains for the 4 directions the the pixels belonging to each zone
295  //std::vector<double> PixelValues[NB_DIR][NB_ZONE];
296  // ROMAIN
297  std::vector<double>** PixelValues = NULL;
298  PixelValues = new std::vector<double>*[NB_DIR];
299  for (unsigned int i = 0; i < NB_DIR; ++i)
300  {
301  PixelValues[i] = NULL;
302  PixelValues[i] = new std::vector<double>[NB_ZONE];
303  }
304  //otbMsgDevMacro( << "\tCentre Xc/Yc="<<Xc<<" "<<Yc<<" Yc12/Yc13="<<Yc12<<" "<<Yc13);
305  // Loop on the region
306  for (unsigned int i = 0; i < m_Radius[0]; ++i)
307  for (unsigned int j = 0; j < m_Radius[1]; ++j)
308  {
309 
310  off[0] = i - m_Radius[0] / 2;
311  off[1] = j - m_Radius[1] / 2;
312 
313  bitIndex = bit.GetIndex(off);
314  X = bitIndex[0];
315  Y = bitIndex[1];
316 
317  // We determine in the horizontal direction with which zone the pixel belongs.
318  if (Y < Yc12) zone = 1;
319  else if ((Yc12 < Y) && (Y < Yc13)) zone = 0;
320  else if (Y > Yc13) zone = 2;
321  else continue;
322  //otbMsgDevMacro( << "\t\tPoint traite (i, j)=("<<i<<","<<j<<") -> X, Y="<<X<<","<<Y<<" zone="<<zone);
323  // Loop on the directions
324  for (unsigned int dir = 0; dir < NB_DIR; ++dir)
325  {
326  //ROTATION( (X-Xc), (Y-Yc), Theta[dir], xout, yout);
327 
328  xout = (X - Xc) * vcl_cos(Theta[dir]) - (Y - Yc) * vcl_sin(Theta[dir]);
329  yout = (X - Xc) * vcl_sin(Theta[dir]) + (Y - Yc) * vcl_cos(Theta[dir]);
330 
331  Index[0] = static_cast<CoordRepType>(xout + Xc);
332  Index[1] = static_cast<CoordRepType>(yout + Yc);
333 
334  PixelValues[dir][zone].push_back(static_cast<double>(interpolator->EvaluateAtContinuousIndex(Index)));
335  }
336  } // end of the loop on the pixels of the region
337 
338  R = 0.;
339  Direction = 0.;
340 
341  // Loop on the 4 directions
342 
343  for (unsigned int dir = 0; dir < NB_DIR; ++dir)
344  {
345 
346  double Rtemp = this->ComputeMeasure(&PixelValues[dir][0], &PixelValues[dir][1], &PixelValues[dir][2]);
347 
348  if (Rtemp > R)
349  {
350  R = Rtemp;
351  Direction = Theta[dir];
352  }
353 
354  } // end of the loop on the directions
355 
356  //otbMsgDevMacro( << "\t\tR, Direction : "<<R<<","<<Direction);
357  if (R >= this->GetThreshold())
358  {
359 
360  // Assignment of this value to the output pixel
361  it.Set(static_cast<OutputPixelType>(R));
362 
363  // Assignment of this value to the "outputdir" pixel
364  itdir.Set(static_cast<OutputPixelType>(Direction));
365  }
366  else
367  {
368 
370 
371  itdir.Set(static_cast<OutputPixelType>(0));
372  }
373  ++bit;
374  ++cit;
375  ++it;
376  ++itdir;
377  //interiorFace = false;
378  progress.CompletedPixel();
379 
380  // ROMAIN
381  for (unsigned int i = 0; i < NB_DIR; ++i)
382  {
383  delete[] PixelValues[i];
384  PixelValues[i] = NULL;
385  }
386  delete[] PixelValues;
387  PixelValues = NULL;
388  }
389 
390  }
391  delete[] Theta;
392 }
393 
394 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
395 double
397 ::ComputeMeasure(std::vector<double>* m1, std::vector<double>* m2, std::vector<double>* m3)
398 {
399  return 0;
400 }
401 
405 template <class TInputImage, class TOutputImage, class TOutputImageDirection, class InterpolatorType>
406 void
408 ::PrintSelf(std::ostream& os, itk::Indent indent) const
409 {
410  Superclass::PrintSelf(os, indent);
411  os << indent << "Length: " << m_LengthLine << std::endl;
412  os << indent << "Width: " << m_WidthLine << std::endl;
413 
414 }
415 
416 } // end namespace otb
417 
418 #endif

Generated at Sat Mar 8 2014 16:05:33 for Orfeo Toolbox with doxygen 1.8.3.1