WMScalarSegmentation.h 9.81 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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WMSCALARSEGMENTATION_H
#define WMSCALARSEGMENTATION_H

#include <string>
#include <vector>

#include "../../common/WItemSelection.h"
#include "../../common/WItemSelector.h"

34 35
#include "../../dataHandler/WITKImageConversion.h"

36 37 38 39 40
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../kernel/WModuleOutputData.h"
#include "../../dataHandler/WDataSetScalar.h"

41 42 43 44 45 46 47 48 49 50 51 52 53
#ifdef OW_USE_ITK

#include "itkWatershedImageFilter.h"
#include "itkOtsuThresholdImageFilter.h"

// smoothing filters
#include "itkGradientAnisotropicDiffusionImageFilter.h"
// other
#include "itkCastImageFilter.h"
#include "itkGradientMagnitudeImageFilter.h"

#endif  // OW_USE_ITK

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
/**
 * First version of a module that implements 3D-image segmentation algorithms.
 *
 * \ingroup modules
 */
class WMScalarSegmentation: public WModule
{
public:

    /**
     * Default constructor.
     */
    WMScalarSegmentation();

    /**
     * Destructor.
     */
    virtual ~WMScalarSegmentation();

    /**
     * Return the name of this module.
     *
     * \return the module's name.
     */
    virtual const std::string getName() const;

    /**
     * Return a description of this module.
     *
     * \return description to module.
     */
    virtual const std::string getDescription() const;

    /**
     * Return a new instance of this module.
     *
     * \return the prototype used to create every module in OpenWalnut.
     */
    virtual boost::shared_ptr< WModule > factory() const;

    /**
     * Get the icon for this module in XPM format.
     *
     * \return the icon.
     */
    virtual const char** getXPMIcon() const;

protected:

    /**
     * Entry point after loading the module. Runs in separate thread.
     */
    virtual void moduleMain();

    /**
     * Initialize the connectors this module is using.
     */
    virtual void connectors();

    /**
     * Initialize the properties for this module.
     */
    virtual void properties();

    /**
     * Callback for m_active. Overwrite this in your modules to handle m_active changes separately.
     */
    virtual void activate();

private:

    /**
     * Do a segmentation depending on the current module property values.
     *
     * \return the resulting segmented dataset.
     */
130
    void doSegmentation();
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

    /**
     * An input connector used to get datasets from other modules.
     */
    boost::shared_ptr< WModuleInputData< WDataSetScalar > > m_input;

    /**
     * The output connector used to provide the calculated data to other modules.
     */
    boost::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output;

    /**
     * This is a pointer to the dataset the module is currently working on.
     */
    boost::shared_ptr< WDataSetScalar > m_dataSet;

    /**
     * This is a pointer to the segmented dataset.
     */
    boost::shared_ptr< WDataSetScalar > m_result;

    /**
     * A condition used to notify about changes in several properties.
     */
    boost::shared_ptr< WCondition > m_propCondition;

    /**
     * The number of the currently selected segmentation method.
     */
    std::size_t m_algoIndex;

    /**
     * A list of possible segmentation algorithms.
     */
    boost::shared_ptr< WItemSelection > m_algos;

    /**
     * A selection box for segmentation algorithms.
     */
    WPropSelection m_algoType;

    /**
     * The threshold property for the simple threshold segmentation.
     */
175 176
    WPropDouble m_threshold;

177 178 179
    //! The watershed level.
    WPropDouble m_level;

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
    //! A function object that implements a simple threshold segmentation.
    class SimpleSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
    {
    public:
        /**
         * Constructor.
         * \param t The threshold for segmentation.
         */
        SimpleSegmentation( double t ) // NOLINT
            : m_threshold( t )
        {
        }

        /**
         * Do the segmentation.
         *
         * \tparam T The integral data type.
         * \param valueset The valueset to segment.
         * \return A pointer to a new valueset.
         */
        template< typename T >
        boost::shared_ptr< WValueSetBase > operator() ( WValueSet< T > const* valueset ) const;

    private:
        //! the threshold
        double m_threshold;
    };
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

#ifdef OW_USE_ITK
    //!
    class WatershedSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
    {
    public:
        /**
         * Constructor.
         * \param t The threshold for segmentation.
         */
        WatershedSegmentation( double level, double t, boost::shared_ptr< WDataSetScalar > ds ) // NOLINT
            : m_level( level ),
              m_threshold( t ),
              m_dataset( ds )
        {
        }

        /**
         * Do the segmentation.
         *
         * \tparam T The integral data type.
         * \param valueset The valueset to segment.
         * \return A pointer to a new valueset.
         */
        template< typename T >
        boost::shared_ptr< WValueSetBase > operator() ( WValueSet< T > const* ) const;

    private:
        //! the level
        double m_level;
        //! the threshold
        double m_threshold;
        //! the dataset
        boost::shared_ptr< WDataSetScalar > m_dataset;
    };

    //!
    class OtsuSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
    {
    public:
        /**
         * Constructor.
         * \param t The threshold for segmentation.
         */
        OtsuSegmentation( boost::shared_ptr< WDataSetScalar > ds ) // NOLINT
            : m_dataset( ds )
        {
        }

        /**
         * Do the segmentation.
         *
         * \tparam T The integral data type.
         * \param valueset The valueset to segment.
         * \return A pointer to a new valueset.
         */
        template< typename T >
        boost::shared_ptr< WValueSetBase > operator() ( WValueSet< T > const* v ) const;

    private:
        //! the dataset
        boost::shared_ptr< WDataSetScalar > m_dataset;
    };
#endif  // OW_USE_ITK
271 272
};

273 274 275 276 277 278 279 280
template< typename T >
boost::shared_ptr< WValueSetBase > WMScalarSegmentation::SimpleSegmentation::operator() ( WValueSet< T > const* valueset ) const
{
    std::vector< T > values( valueset->size() );
    for( std::size_t k = 0; k < valueset->size(); ++k )
    {
        values[ k ] = ( static_cast< double >( valueset->getScalar( k ) ) <
                        m_threshold * ( valueset->getMaximumValue() - valueset->getMinimumValue() ) / 100.0 + valueset->getMinimumValue()
281
                        ? static_cast< T >( 0 ) : static_cast< T >( 100 ) );
282 283 284
    }
    return boost::shared_ptr< WValueSet< T > >( new WValueSet< T >( 0, 1, values, DataType< T >::type ) );
}
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
#ifdef OW_USE_ITK

template< typename T >
boost::shared_ptr< WValueSetBase > WMScalarSegmentation::WatershedSegmentation::operator() ( WValueSet< T > const* ) const
{
    typedef itk::Image< T, 3 > ImgType;
    typedef itk::Image< double, 3 > RealType;
    typedef itk::Image< uint64_t, 3 > LabelType; // this might be a problem on 32-bit systems
    typedef itk::Image< float, 3 > FinalType;

    typedef itk::GradientAnisotropicDiffusionImageFilter< ImgType, RealType > SmoothingType;
    typedef itk::CastImageFilter< LabelType, FinalType > CastFilter;
    typedef itk::GradientMagnitudeImageFilter< RealType, RealType > GradFilter;
    typedef itk::WatershedImageFilter< RealType > WaterFilter;

    typename ImgType::Pointer img = makeImageFromDataSet< T >( m_dataset );
    typename SmoothingType::Pointer sm = SmoothingType::New();
    typename CastFilter::Pointer cf = CastFilter::New();
    typename GradFilter::Pointer gf = GradFilter::New();
    typename WaterFilter::Pointer ws = WaterFilter::New();

    sm->SetNumberOfIterations( 10 );
    sm->SetTimeStep( 0.0625 );
    sm->SetConductanceParameter( 2.0 );
    sm->SetInput( img );
    gf->SetInput( sm->GetOutput() );
    ws->SetInput( gf->GetOutput() );
    ws->SetLevel( m_level / 100.0 );
    ws->SetThreshold( m_threshold / 100.0 );
    cf->SetInput( ws->GetOutput() );
    cf->Update();
    boost::shared_ptr< WDataSetScalar > res = makeDataSetFromImage< float >( cf->GetOutput() );
    return res->getValueSet();
}

template< typename T >
boost::shared_ptr< WValueSetBase > WMScalarSegmentation::OtsuSegmentation::operator() ( WValueSet< T > const* v ) const
{
    typedef itk::Image< T, 3 > ImgType;
    typedef itk::OtsuThresholdImageFilter< ImgType, ImgType > Otsu;

    typename ImgType::Pointer img = makeImageFromDataSet< T >( m_dataset );
    typename Otsu::Pointer o = Otsu::New();
    o->SetInput( img );
    o->SetOutsideValue( v->getMaximumValue() );
    o->SetInsideValue( v->getMinimumValue() );
    o->Update();
    boost::shared_ptr< WDataSetScalar > res = makeDataSetFromImage< T >( o->GetOutput() );
    return res->getValueSet();
}

#endif  // OW_USE_ITK

339
#endif  // WMSCALARSEGMENTATION_H