otbLabelImageSmallRegionMergingFilter.h 10.2 KB
Newer Older
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
/*
 * Copyright (C) 2005-2017 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 otbLabelImageSmallRegionMergingFilter_h
#define otbLabelImageSmallRegionMergingFilter_h

#include "otbImage.h"
#include "otbVectorImage.h"
#include "itkImageToImageFilter.h"

#include "otbPersistentImageFilter.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include <set>

namespace otb
{

/** \class PersistentLabelImageSmallRegionMergingFilter
 *
 *
 * This class merges regions in the input label image according to the input
 * image of spectral values and the RangeBandwidth parameter.
 *
 *
 * \ingroup ImageSegmentation
 *
 * \ingroup OTBConversion
 */
template <class TInputLabelImage, class TInputSpectralImage >
class ITK_EXPORT PersistentLabelImageSmallRegionMergingFilter
  : public PersistentImageFilter<TInputLabelImage, TInputLabelImage>
{
public:
  /** Standard class typedef */
  typedef PersistentLabelImageSmallRegionMergingFilter                                Self;
  typedef PersistentImageFilter<TInputLabelImage, TInputLabelImage> Superclass;
  typedef itk::SmartPointer<Self>                                      Pointer;
  typedef itk::SmartPointer<const Self>                                ConstPointer;

  /** Type macro */
  itkTypeMacro(PersistentLabelImageSmallRegionMergingFilter, PersistentImageFilter);
  itkNewMacro(Self);

  /** Template parameters typedefs */

  typedef TInputLabelImage                        InputImageType;
  typedef typename InputImageType::Pointer        InputImagePointerType;
  typedef typename InputImageType::PixelType      InputPixelType;
  typedef typename InputImageType::IndexType      InputIndexType;
  typedef typename InputImageType::SizeType       InputSizeType;
  typedef typename InputImageType::IndexValueType InputIndexValueType;
  typedef typename InputImageType::PointType      PointType;
  typedef typename InputImageType::RegionType     RegionType;
  typedef typename InputImageType::SizeType       SizeType;
  
  typedef TInputLabelImage                        InputLabelImageType;
  typedef typename InputLabelImageType::PixelType InputLabelType;

  typedef TInputSpectralImage                     InputSpectralImageType;
  typedef typename TInputSpectralImage::PixelType SpectralPixelType;
  
  typedef itk::VariableLengthVector<double>                             RealVectorPixelType;
Cédric Traizet's avatar
Cédric Traizet committed
80
81
82
83
  //typedef std::map<InputLabelType, double>                                    LabelPopulationMapType;
  typedef std::vector<double>                                    LabelPopulationMapType;
  
  
84
  typedef std::map<InputLabelType, std::set<InputLabelType> >                 NeigboursMapType;
85
86
87
88
  //typedef std::map<InputLabelType, RealVectorPixelType >                        LabelStatisticMapType;
  typedef std::vector<RealVectorPixelType >                        LabelStatisticMapType;
  
  
Cédric Traizet's avatar
Cédric Traizet committed
89
90
91
92
93
  //typedef std::map<InputLabelType, InputLabelType>                      CorrespondanceMapType;
  typedef std::vector<double>                      CorrespondanceMapType;
  
  
  
94
95
96
97
98
99
100
101
102
103
104
105
106
  /** Sets the input image where the value of a pixel is the region id */
  void SetInputLabelImage( const InputLabelImageType * labelImage);
  /** Sets the input image representing spectral values */
  void SetInputSpectralImage( const InputSpectralImageType * spectralImage);
  /** Returns input label image */
  InputLabelImageType * GetInputLabelImage();
  /** Returns input spectral image */
  InputSpectralImageType * GetInputSpectralImage();
  
  /** Set/Get size of polygon to be merged */
  itkGetMacro(Size , unsigned int);
  itkSetMacro(Size , unsigned int);

107
  /** Set/Get the Label population map and initialize the correspondance map*/
Cédric Traizet's avatar
Cédric Traizet committed
108
  /*void SetLabelPopulation( LabelPopulationMapType const & labelPopulation )
109
110
  {
    m_LabelPopulation = labelPopulation; 
111
112
113
114
115
    // Initialize m_CorrespondingMap to the identity (i.e. m[label] = label)
    for (auto label : labelPopulation)
    {
      m_CorrespondanceMap[ label.first ] = label.first;
    }
116
  }
Cédric Traizet's avatar
Cédric Traizet committed
117
118
119
120
121
122
123
124
125
126
127
  */
  void SetLabelPopulation( LabelPopulationMapType const & labelPopulation )
  {
    m_LabelPopulation = labelPopulation; 
    // Initialize m_CorrespondingMap to the identity (i.e. m[label] = label)
    m_CorrespondanceMap.resize( labelPopulation.size() );
    for (int i =0; i <labelPopulation.size(); i++)
    {
      m_CorrespondanceMap[ i ] = i;
    }
  }
128
129
130
131
132
133
134
135
  
  LabelPopulationMapType const & GetLabelPopulation() const
  {
    return m_LabelPopulation;
  }

  void SetLabelStatistic( LabelStatisticMapType const & labelStatistic )
  {
136
    m_LabelStatistic = labelStatistic;
137
138
139
140
  }
  
  LabelStatisticMapType const & GetLabelStatistic() const
  {
141
142
143
144
145
146
    return m_LabelStatistic;
  }
  
  CorrespondanceMapType const & GetCorrespondanceMap() const
  {
    return m_CorrespondanceMap;
147
148
149
150
151
152
153
154
155
156
157
158
159
  }
  
  virtual void Reset(void);
  virtual void Synthetize(void);

protected:
  //void EnlargeOutputRequestedRegion( itk::DataObject *output ) override;

   void GenerateOutputInformation(void) override;

   void ThreadedGenerateData(const RegionType&
                outputRegionForThread, itk::ThreadIdType threadId) override;

160
161
162
163

  // Use m_CorrespondanceMap recurively to find the label corresponding to the input label
  InputLabelType FindCorrespondingLabel( InputLabelType label);

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  /** Constructor */
  PersistentLabelImageSmallRegionMergingFilter();

  /** Destructor */
  ~PersistentLabelImageSmallRegionMergingFilter() override;

  /** PrintSelf method */
  void PrintSelf(std::ostream& os, itk::Indent indent) const override;

private:
  PersistentLabelImageSmallRegionMergingFilter(const Self &) = delete;
  void operator =(const Self&) = delete;
  
  unsigned int m_Size;
  LabelPopulationMapType m_LabelPopulation;
  
180
  LabelStatisticMapType m_LabelStatistic;
181
182
183
  
  // Neigbours maps for each thread
  std::vector <NeigboursMapType > m_NeighboursMapsTmp;
184
185
186
  
  CorrespondanceMapType m_CorrespondanceMap;
  //NeigboursMapType m_NeighboursMap;
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
};

/** \class LabelImageSmallRegionMergingFilter
 *
 *
 * This class merges regions in the input label image according to the input
 * image of spectral values and the RangeBandwidth parameter.
 *
 *
 * \ingroup ImageSegmentation
 *
 * \ingroup OTBConversion
 */
template <class TInputLabelImage,  class TInputSpectralImage>
class ITK_EXPORT LabelImageSmallRegionMergingFilter :
public PersistentFilterStreamingDecorator<PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> >
{
  
public:
  /** Standard Self typedef */
  typedef LabelImageSmallRegionMergingFilter                                                 Self;
  typedef PersistentFilterStreamingDecorator
    <PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> >   Superclass;
  typedef itk::SmartPointer<Self>                                                            Pointer;
  typedef itk::SmartPointer<const Self>                                                      ConstPointer;

  /** Type macro */
  itkNewMacro(Self);

  /** Creation through object factory macro */
  itkTypeMacro(LabelImageSmallRegionMergingFilter, PersistentFilterStreamingDecorator);

  typedef PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentFilterType;
  typedef typename PersistentFilterType::InputLabelImageType InputLabelImageType;
  typedef typename PersistentFilterType::InputSpectralImageType InputSpectralImageType;
  typedef typename PersistentFilterType::LabelPopulationMapType LabelPopulationMapType;
  typedef typename PersistentFilterType::LabelStatisticMapType LabelStatisticMapType;
224
  typedef typename PersistentFilterType::CorrespondanceMapType CorrespondanceMapType;
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
286
  
  /** Sets the input image where the value of a pixel is the region id */
  void SetInputLabelImage( const InputLabelImageType * labelImage)
  {
    this->GetFilter()->SetInputLabelImage( labelImage );
  }
  
  
  /** Sets the input image representing spectral values */
  void SetInputSpectralImage( const InputSpectralImageType * spectralImage)
  {
    this->GetFilter()->SetInputSpectralImage( spectralImage );
  }
  
  /** Returns input label image */
  InputLabelImageType * GetInputLabelImage()
  {
    return this->GetFilter()->GetInputLabelImage();
  }
  
  /** Returns input spectral image */
  InputSpectralImageType * GetInputSpectralImage()
  {
    return this->GetFilter()->GetInputSpectralImage();
  }
  
  /** Set size of polygon to be merged */
  void SetSize(unsigned int size) 
  {
    this->GetFilter()->SetSize( size );
  }
  
  /** Get size of polygon to be merged */
  unsigned int GetSize()
  {
    return this->GetFilter()->GetSize();
  }
  
  /** Set the Label population map */
  void SetLabelPopulation( LabelPopulationMapType const & labelPopulation )
  {
    this->GetFilter()->SetLabelPopulation( labelPopulation );
  }
  
  /** Get the Label population map */
  LabelPopulationMapType const & GetLabelPopulation(  ) const
  {
    return this->GetFilter()->GetLabelPopulation();
  }
  
  /** Set the Label statistic map */
  void SetLabelStatistic( LabelStatisticMapType const & labelStatistic )
  {
    this->GetFilter()->SetLabelStatistic( labelStatistic );
  }
  
  /** Get the Label statistic map */
  LabelStatisticMapType const & GetLabelStatistic( ) const
  {
    return this->GetFilter()->GetLabelStatistic();
  }
  
287
288
289
290
291
292
  CorrespondanceMapType const & GetCorrespondanceMap() const
  {
    return this->GetFilter()->GetCorrespondanceMap();
  }
  
  
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
protected:
  /** Constructor */
  LabelImageSmallRegionMergingFilter() {}
  /** Destructor */
  ~LabelImageSmallRegionMergingFilter() override {}

private:
  LabelImageSmallRegionMergingFilter(const Self &) = delete;
  void operator =(const Self&) = delete;

};

} // end namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
#include "otbLabelImageSmallRegionMergingFilter.hxx"
#endif

#endif