File: otbHooverInstanceFilter.h

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (284 lines) | stat: -rw-r--r-- 9,900 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
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef otbHooverInstanceFilter_h
#define otbHooverInstanceFilter_h

#include <set>
#include "itkInPlaceLabelMapFilter.h"
#include "itkVariableSizeMatrix.h"
#include "itkVariableLengthVector.h"

namespace otb
{
/** \class HooverInstanceFilter
 *
 * \brief This class computes the Hoover instances.
 *
 * The Hoover instances are computed from two segmentations : ground truth (GT) and machine segmentation (MS); and
 * a Hoover confusion matrix. It is intended to work with AttributesMapLabelObject, where the Hoover scores will be stored.
 * The different instances are :
 *    - correct detection (a GT region paired with a MS region)
 *    - under segmentation (a MS region paired with a set of GT regions)
 *    - over segmentation (a GT region paired with a set of MS regions)
 *    - missed (a GT region that doesn't belong to any of the previous instances)
 *    - noise (a MS region that doesn't belong to any of the previous instances)
 *
 * The corresponding scores are :
 *    - RC : for correct detection
 *    - RF : for over segmentation
 *    - RA : for under segmentation
 *    - RM : for missed regions (only for GT)
 *    - RN : for noise regions (only for MS)
 *
 * These Hoover scores that are stored in the label maps are computed between 0 (if the region doesn't belong to this kind of instance) and 1.
 *
 * If the user wants to which region labels have been paired, he can set the flag UseExtendedAttributes. The extended attributes contain the
 * labels that have been paired in Hoover instances:
 *    - ATTRIBUTE_CD (correct detection) : label of the corresponding region in the other segmentation
 *    - ATTRIBUTE_OS[_x] (over segmentation) : label of the opposite over segmented region in GT / labels of the corresponding regions of MS
 *    - ATTRIBUTE_US[_x] (under segmentation) : labels of the corresponding regions of GT / label of the under segmentated region of MS
 *    - ATTRIBUTE_M (missed) : missed region own label (in GT)
 *    - ATTRIBUTE_N (noise) : noise region own label (in MS)
 *
 * These attributes are handled in a different way than the Hoover scores. The simple presence of an extended attribute in a given region has a
 * meaning, regardless of its value. It is assumed that its value always corresponds to an existing region label. This is why these extended
 * attributes are not reset but removed before computing Hoover instances.
 * (see Hoover et al., "An experimental comparison of range image segmentation algorithms", IEEE PAMI vol. 18, no. 7, July 1996)
 *
 * \sa HooverMatrixFilter
 *
 * \ingroup OTBMetrics
 */

template< class TLabelMap >
class ITK_EXPORT HooverInstanceFilter :
    public itk::InPlaceLabelMapFilter< TLabelMap >
{
public:
  /** Standard class typedefs. */
  typedef HooverInstanceFilter   Self;
  typedef itk::InPlaceLabelMapFilter< TLabelMap >      Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;

  /** Standard New method. */
  itkNewMacro(Self);
  /** Run-time type information (and related methods). */
  itkTypeMacro(HooverInstanceFilter, InPlaceLabelMapFilter);

  /** Some convenient typedefs. */
  typedef TLabelMap                                     LabelMapType;
  typedef typename LabelMapType::Pointer                LabelMapPointer;
  typedef typename LabelMapType::ConstIterator          ConstIteratorType;
  typedef typename LabelMapType::Iterator               IteratorType;
  typedef typename LabelMapType::LabelObjectType        LabelObjectType;
  typedef typename LabelObjectType::AttributeType       AttributeType;
  typedef typename LabelObjectType::AttributesValueType AttributesValueType;
  typedef typename LabelMapType::LabelVectorType        LabelVectorType;
  typedef typename LabelMapType::RegionType             ImageRegionType;

  //typedef typename LabelObjectType::LineContainerType   LineContainerType;
  typedef typename LabelObjectType::IndexType           IndexType;
  typedef typename LabelObjectType::LabelType           LabelType;

  typedef unsigned long                                 CoefficientType;
  typedef itk::VariableSizeMatrix<CoefficientType>      MatrixType;

  typedef itk::VariableLengthVector<CoefficientType>    CardinalVector;
  typedef std::set<CoefficientType>                     RegionSetType;
  typedef std::vector<LabelObjectType*>                 ObjectVectorType;

  void SetGroundTruthLabelMap(const LabelMapType *gt);
  void SetMachineSegmentationLabelMap(const LabelMapType *ms);

  const LabelMapType* GetGroundTruthLabelMap();
  LabelMapType* GetMachineSegmentationLabelMap();

  LabelMapType* GetOutputGroundTruthLabelMap();
  LabelMapType* GetOutputMachineSegmentationLabelMap();

  itkSetMacro(HooverMatrix, MatrixType);
  itkGetMacro(HooverMatrix, MatrixType);

  itkSetMacro(Threshold, double);
  itkGetMacro(Threshold, double);

  itkSetMacro(UseExtendedAttributes, bool);
  itkGetMacro(UseExtendedAttributes, bool);

  /** Get the average scores (after filter update) */
  itkGetMacro(MeanRC, AttributesValueType);
  itkGetMacro(MeanRF, AttributesValueType);
  itkGetMacro(MeanRA, AttributesValueType);
  itkGetMacro(MeanRM, AttributesValueType);
  itkGetMacro(MeanRN, AttributesValueType);

  enum AttributeTypes {
    ATTRIBUTE_CD=100,
    ATTRIBUTE_OS,
    ATTRIBUTE_US,
    ATTRIBUTE_M,
    ATTRIBUTE_N,
    ATTRIBUTE_RC,
    ATTRIBUTE_RF,
    ATTRIBUTE_RA,
    ATTRIBUTE_RM,
    ATTRIBUTE_RN
  };

  static std::string GetNameFromAttribute( const AttributeType & a )
    {
    std::string name;
    switch( a )
      {
      case ATTRIBUTE_CD: name = "HooverInstance_Ext_CD"; break;
      case ATTRIBUTE_OS: name = "HooverInstance_Ext_OS"; break;
      case ATTRIBUTE_US: name = "HooverInstance_Ext_US"; break;
      case ATTRIBUTE_M:  name = "HooverInstance_Ext_M";  break;
      case ATTRIBUTE_N:  name = "HooverInstance_Ext_N";  break;
      case ATTRIBUTE_RC: name = "HooverInstance_RC";     break;
      case ATTRIBUTE_RF: name = "HooverInstance_RF";     break;
      case ATTRIBUTE_RA: name = "HooverInstance_RA";     break;
      case ATTRIBUTE_RM: name = "HooverInstance_RM";     break;
      case ATTRIBUTE_RN: name = "HooverInstance_RN";     break;
      }
    return name;
    }

  static AttributeType GetAttributeFromName( const std::string & name )
    {
    if ( name == "HooverInstance_Ext_CD" )
      {
      return ATTRIBUTE_CD;
      }
    else if ( name == "HooverInstance_Ext_OS" )
      {
      return ATTRIBUTE_OS;
      }
    else if ( name == "HooverInstance_Ext_US" )
      {
      return ATTRIBUTE_US;
      }
    else if ( name == "HooverInstance_Ext_M" )
      {
      return ATTRIBUTE_M;
      }
    else if ( name == "HooverInstance_Ext_N" )
      {
      return ATTRIBUTE_N;
      }
    else if ( name == "HooverInstance_RC" )
      {
      return ATTRIBUTE_RC;
      }
    else if ( name == "HooverInstance_RF" )
      {
      return ATTRIBUTE_RF;
      }
    else if ( name == "HooverInstance_RA" )
      {
      return ATTRIBUTE_RA;
      }
    else if ( name == "HooverInstance_RM" )
      {
      return ATTRIBUTE_RM;
      }
    else if ( name == "HooverInstance_RN" )
      {
      return ATTRIBUTE_RN;
      }
    }

protected:
  HooverInstanceFilter();
  ~HooverInstanceFilter() ITK_OVERRIDE {};

  /** Re implement the allocate output method to handle the second output correctly */
  void AllocateOutputs() ITK_OVERRIDE;

  /** Re implement the release input method to handle the second input correctly */
  void ReleaseInputs() ITK_OVERRIDE;

  /** Actions :
   *    - Fill cardinalities of GT regions
   */
  void ThreadedProcessLabelObject( LabelObjectType * labelObject ) ITK_OVERRIDE;

  /** Actions:
   *    - Check matrix size
   *    - Init cardinalities lists
   *    - Fill cardinalities list for MS (GT is done by ThreadedProcessLabelObject)
   */
  void BeforeThreadedGenerateData() ITK_OVERRIDE;

  /** Actions :
   *    - Compute Hoover instances
   */
  void AfterThreadedGenerateData() ITK_OVERRIDE;

private:

  /** number of regions (label objects) found in ground truth (GT) */
  unsigned long     m_NumberOfRegionsGT;

  /** number of regions (label objects) found in machine segmentation (MS) */
  unsigned long     m_NumberOfRegionsMS;

  /** List of labels in GT segmentation */
  LabelVectorType   m_LabelsGT;

  /** Hoover confusion matrix computed between GT and MS*/
  MatrixType        m_HooverMatrix;

  /** List of region sizes in GT */
  CardinalVector    m_CardRegGT;

  /** List of region sizes in MS */
  CardinalVector    m_CardRegMS;

  /** Threshold used to compute Hoover instances */
  double            m_Threshold;

  /** Flag to output additional information in label maps */
  bool m_UseExtendedAttributes;

  /** Average score for correct detection on the whole segmentation */
  AttributesValueType m_MeanRC;

  /** Average score for over segmentation on the whole segmentation */
  AttributesValueType m_MeanRF;

  /** Average score for under segmentation on the whole segmentation */
  AttributesValueType m_MeanRA;

  /** Average score for missed detection on the whole segmentation */
  AttributesValueType m_MeanRM;

  /** Average score for noise on the whole segmentation */
  AttributesValueType m_MeanRN;

};

}

#ifndef OTB_MANUAL_INSTANTIATION
#include "otbHooverInstanceFilter.txx"
#endif

#endif