~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/modules/scalarSegmentation/WSegmentationAlgoRegionGrowingConfidenceConnected.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H
 
26
#define WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H
 
27
 
 
28
#ifdef OW_USE_ITK
 
29
 
 
30
#include <string>
 
31
#include <vector>
 
32
 
 
33
#include "itkImage.h"
 
34
#include "itkConfidenceConnectedImageFilter.h"
 
35
#include "itkGradientAnisotropicDiffusionImageFilter.h"
 
36
#include "itkCastImageFilter.h"
 
37
 
 
38
#include "core/kernel/WKernel.h"
 
39
#include "core/kernel/WROIManager.h"
 
40
 
 
41
#include "core/graphicsEngine/WROIBox.h"
 
42
 
 
43
#include "core/dataHandler/WITKImageConversion.h"
 
44
 
 
45
#include "WSegmentationAlgo.h"
 
46
 
 
47
/**
 
48
 * Confidence connected region growing segmentation.
 
49
 * \class WSegmentationAlgoRegionGrowingConfidenceConnected
 
50
 */
 
51
class WSegmentationAlgoRegionGrowingConfidenceConnected : public WSegmentationAlgo
 
52
{
 
53
public:
 
54
    /**
 
55
     * Standard constructor.
 
56
     */
 
57
    WSegmentationAlgoRegionGrowingConfidenceConnected();
 
58
 
 
59
    /**
 
60
     * Destructor.
 
61
     */
 
62
    virtual ~WSegmentationAlgoRegionGrowingConfidenceConnected();
 
63
 
 
64
    /**
 
65
     * Return the name of this algorithm.
 
66
     * \return The name.
 
67
     */
 
68
    virtual std::string getName();
 
69
 
 
70
    /**
 
71
     * Return a description of this algorithm.
 
72
     * \return A description.
 
73
     */
 
74
    virtual std::string getDescription();
 
75
 
 
76
    /**
 
77
     * Checks if any properties were changed.
 
78
     * \return True, iff any properties were changed.
 
79
     */
 
80
    virtual bool propChanged();
 
81
 
 
82
    /**
 
83
     * Implements the operation.
 
84
     *
 
85
     * \tparam The type of the values in the dataset's valueset.
 
86
     * \param valueset The dataset's valueset.
 
87
     * \return The resulting dataset.
 
88
     */
 
89
    template< typename T >
 
90
    DataSetPtr operator() ( WValueSet< T > const* valueset ) const;
 
91
 
 
92
private:
 
93
    /**
 
94
     * Initializes the algorithm's properties.
 
95
     */
 
96
    virtual void properties();
 
97
 
 
98
    /**
 
99
     * A virtual function that calls the correct segmentation operation.
 
100
     * \return The resulting dataset.
 
101
     */
 
102
    virtual DataSetPtr applyOperation();
 
103
 
 
104
    //! The number of iteration in the smoothing filter.
 
105
    WPropInt m_smoothingIter;
 
106
 
 
107
    //! The conductance parameter for the anisotropic smoothing.
 
108
    WPropDouble m_conductance;
 
109
 
 
110
    //! The number of iteration in region growing.
 
111
    WPropInt m_regionGrowingIterations;
 
112
 
 
113
    //! The initial neighborhood radius.
 
114
    WPropInt m_neighborhoodRadius;
 
115
 
 
116
    //! The multiplier for the variance.
 
117
    WPropDouble m_multiplier;
 
118
};
 
119
 
 
120
template< typename T >
 
121
WSegmentationAlgo::DataSetPtr WSegmentationAlgoRegionGrowingConfidenceConnected::operator() ( WValueSet< T > const* ) const
 
122
{
 
123
    typedef itk::Image< T, 3 > ImgType;
 
124
    typedef itk::Image< double, 3 > RealType;
 
125
    typedef itk::Image< uint64_t, 3 > LabelType; // this might be a problem on 32-bit systems
 
126
    typedef itk::Image< float, 3 > FinalType;
 
127
 
 
128
    typedef itk::GradientAnisotropicDiffusionImageFilter< ImgType, RealType > SmoothingType;
 
129
    typedef itk::CastImageFilter< LabelType, FinalType > CastFilter;
 
130
    typedef itk::ConfidenceConnectedImageFilter< RealType, LabelType > RegionGrowingFilter;
 
131
 
 
132
    typename ImgType::Pointer image = makeImageFromDataSet< T >( m_dataSet );
 
133
    typename SmoothingType::Pointer smoothing = SmoothingType::New();
 
134
    typename CastFilter::Pointer cast = CastFilter::New();
 
135
    typename RegionGrowingFilter::Pointer regionGrowing = RegionGrowingFilter::New();
 
136
 
 
137
    std::vector< osg::ref_ptr< WROI > > rois = WKernel::getRunningKernel()->getRoiManager()->getRois();
 
138
    itk::Index< 3 > i;
 
139
 
 
140
    WROIBox* box = NULL;
 
141
 
 
142
    unsigned int k = 0;
 
143
    while( !box && k < rois.size() )
 
144
    {
 
145
        box = dynamic_cast< WROIBox* >( rois[ k ].get() );
 
146
        ++k;
 
147
    }
 
148
 
 
149
    if( box )
 
150
    {
 
151
        boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_dataSet->getGrid() );
 
152
        WVector3d v = 0.5 * ( box->getMinPos() + box->getMaxPos() );
 
153
        WValue< int > voxel = grid->getVoxelCoord( v );
 
154
 
 
155
        i[ 0 ] = static_cast< int32_t >( voxel[ 0 ] );
 
156
        i[ 1 ] = static_cast< int32_t >( voxel[ 1 ] );
 
157
        i[ 2 ] = static_cast< int32_t >( voxel[ 2 ] );
 
158
 
 
159
        smoothing->SetNumberOfIterations( m_smoothingIter->get( true ) );
 
160
        smoothing->SetTimeStep( 0.0625 );
 
161
        smoothing->SetConductanceParameter( m_conductance->get( true ) );
 
162
        smoothing->SetInput( image );
 
163
        regionGrowing->SetInput( smoothing->GetOutput() );
 
164
        regionGrowing->SetMultiplier( m_multiplier->get( true ) );
 
165
        regionGrowing->SetNumberOfIterations( m_regionGrowingIterations->get( true ) );
 
166
        regionGrowing->SetReplaceValue( 255.0f );
 
167
        regionGrowing->SetInitialNeighborhoodRadius( m_neighborhoodRadius->get( true ) );
 
168
        regionGrowing->SetSeed( i );
 
169
        cast->SetInput( regionGrowing->GetOutput() );
 
170
        try
 
171
        {
 
172
            cast->Update();
 
173
        }
 
174
        catch( ... )
 
175
        {
 
176
            throw WException( "Problem in Region Growing Segmentation" );
 
177
        }
 
178
        return makeDataSetFromImage< float >( cast->GetOutput() );
 
179
    }
 
180
    else
 
181
    {
 
182
        return DataSetPtr();
 
183
    }
 
184
}
 
185
 
 
186
#endif  // OW_USE_ITK
 
187
 
 
188
#endif  // WSEGMENTATIONALGOREGIONGROWINGCONFIDENCECONNECTED_H