OpenWalnut  1.5.0dev
WSegmentationAlgoThreshold.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSEGMENTATIONALGOTHRESHOLD_H
26 #define WSEGMENTATIONALGOTHRESHOLD_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include "WSegmentationAlgo.h"
33 
34 /**
35  * A very simple threshold segmentation working in two modi: If in LOWER_THRESHOLD mode than voxels
36  * that have a value smaller than the threshold are set to 0, while the rest of the voxels are set
37  * to 1 where as in UPPER_THRESHOLD mode the opposite applies.
38  *
39  * \class WSegmentationAlgoThreshold
40  */
42 {
43 public:
44  /**
45  * Standard constructor.
46  */
48 
49  /**
50  * Destructor.
51  */
53 
54  /**
55  * Return the name of this algorithm.
56  * \return The name.
57  */
58  virtual std::string getName();
59 
60  /**
61  * Return a description of this algorithm.
62  * \return A description.
63  */
64  virtual std::string getDescription();
65 
66  /**
67  * Checks if any properties were changed.
68  * \return True, iff any properties were changed.
69  */
70  virtual bool propChanged();
71 
72  /**
73  * Implements the operation.
74  *
75  * \tparam The type of the values in the dataset's valueset.
76  * \param valueset The dataset's valueset.
77  * \return The resulting dataset.
78  */
79  template< typename T >
80  DataSetPtr operator() ( WValueSet< T > const* valueset ) const;
81 
82 private:
83  /**
84  * Initializes the algorithm's properties.
85  */
86  virtual void properties();
87 
88  /**
89  * A virtual function that calls the correct segmentation operation.
90  * \return The resulting dataset.
91  */
92  virtual DataSetPtr applyOperation();
93 
94  //! The lower threshold in %.
95  WPropDouble m_low_threshold;
96 
97  //! The upper threshold in %.
98  WPropDouble m_upp_threshold;
99 
100  /**
101  * Whether the values inside range of thresholds should be keept, or
102  * resulting images should contain only 0 or 1 values (binarized).
103  */
104  WPropBool m_binarize;
105 };
106 
107 /**
108  * This function call operator can be called by a visitor. It performs
109  * the actual segmentation as mentioned in the description of this class
110  * \param valueset operator will work on this set of values
111  * \return The resulting values after applying the segmentation criterion.
112  */
113 template< typename T >
115 {
116  std::shared_ptr< std::vector< T > > values( new std::vector< T >( valueset->size() ) );
117 
118  double low_threshold = valueset->getMinimumValue() + m_low_threshold->get( true ) * ( valueset->getMaximumValue() - valueset->getMinimumValue() );
119  double upp_threshold = valueset->getMinimumValue() + m_upp_threshold->get( true ) * ( valueset->getMaximumValue() - valueset->getMinimumValue() );
120 
121  double val = 0.0;
122  if( m_binarize->get( true ) )
123  {
124  for( std::size_t k = 0; k < valueset->size(); ++k )
125  {
126  val = static_cast< double >( valueset->getScalar( k ) );
127  ( *values )[k] = static_cast< T >( val < low_threshold || val > upp_threshold ? 0 : 1 );
128  }
129  }
130  else
131  {
132  for( std::size_t k = 0; k < valueset->size(); ++k )
133  {
134  val = static_cast< double >( valueset->getScalar( k ) );
135  ( *values )[k] = static_cast< T >( val < low_threshold || val > upp_threshold ? 0 : val );
136  }
137  }
138  std::shared_ptr< WValueSet< T > > vs( new WValueSet< T >( 0, 1, values, DataType< T >::type ) );
139  return DataSetPtr( new WDataSetScalar( vs, m_dataSet->getGrid() ) );
140 }
141 
142 #endif // WSEGMENTATIONALGOTHRESHOLD_H
This data set type contains scalars as values.
A very simple threshold segmentation working in two modi: If in LOWER_THRESHOLD mode than voxels that...
WPropDouble m_upp_threshold
The upper threshold in %.
WSegmentationAlgoThreshold()
Standard constructor.
virtual bool propChanged()
Checks if any properties were changed.
virtual void properties()
Initializes the algorithm's properties.
DataSetPtr operator()(WValueSet< T > const *valueset) const
Implements the operation.
virtual std::string getDescription()
Return a description of this algorithm.
virtual std::string getName()
Return the name of this algorithm.
WPropBool m_binarize
Whether the values inside range of thresholds should be keept, or resulting images should contain onl...
virtual ~WSegmentationAlgoThreshold()
Destructor.
WPropDouble m_low_threshold
The lower threshold in %.
virtual DataSetPtr applyOperation()
A virtual function that calls the correct segmentation operation.
A base class for segmentation alorithms.
std::shared_ptr< WDataSetScalar > DataSetPtr
A conveniant typedef.
DataSetPtr m_dataSet
A pointer to the currently processed dataset.
Base Class for all value set types.
Definition: WValueSet.h:47
virtual size_t size() const
Definition: WValueSet.h:162
virtual T getScalar(size_t i) const
Definition: WValueSet.h:193
virtual double getMaximumValue() const
This method returns the largest value in the valueset.
Definition: WValueSet.h:289
virtual double getMinimumValue() const
This method returns the smallest value in the valueset.
Definition: WValueSet.h:278
An object that knows an appropriate dataType flag for the typename T.