Commit d2cff2e5 by reichenbach

[ADD] added a region growing algorithm to WMScalarSegmentation

parent 9a106a51
......@@ -91,6 +91,7 @@ void WMScalarSegmentation::properties()
m_algos->addItem( "Simple Threshold", "" );
m_algos->addItem( "Watershed", "" );
m_algos->addItem( "Otsu Threshold", "" );
m_algos->addItem( "Region Growing", "" );
m_algoType = m_properties->addProperty( "Seg Algo", "Choose a segmentation method.", m_algos->getSelectorFirst(), m_propCondition );
......@@ -175,6 +176,9 @@ void WMScalarSegmentation::doSegmentation()
case 2:
vs = m_dataSet->getValueSet()->applyFunction( OtsuSegmentation( m_dataSet ) );
break;
case 3:
vs = m_dataSet->getValueSet()->applyFunction( RegionGrowingSegmentation( m_dataSet ) );
break;
#endif // OW_USE_ITK
default:
errorLog() << "Invalid algorithm selected." << std::endl;
......
......@@ -32,6 +32,7 @@
#include "../../common/WItemSelector.h"
#include "../../dataHandler/WITKImageConversion.h"
#include "../../dataHandler/WDataHandler.h"
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
......@@ -42,6 +43,7 @@
#include "itkWatershedImageFilter.h"
#include "itkOtsuThresholdImageFilter.h"
#include "itkConfidenceConnectedImageFilter.h"
// smoothing filters
#include "itkGradientAnisotropicDiffusionImageFilter.h"
......@@ -206,13 +208,15 @@ private:
};
#ifdef OW_USE_ITK
//!
//! A class that implements a watershed segmentation algorithm.
class WatershedSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
{
public:
/**
* Constructor.
* \param level The water level.
* \param t The threshold for segmentation.
* \param ds The dataset.
*/
WatershedSegmentation( double level, double t, boost::shared_ptr< WDataSetScalar > ds ) // NOLINT
: m_level( level ),
......@@ -225,7 +229,6 @@ private:
* 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 >
......@@ -240,13 +243,13 @@ private:
boost::shared_ptr< WDataSetScalar > m_dataset;
};
//!
//! A segmentation using otsu's thresholding method.
class OtsuSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
{
public:
/**
* Constructor.
* \param t The threshold for segmentation.
* \param ds The dataset.
*/
OtsuSegmentation( boost::shared_ptr< WDataSetScalar > ds ) // NOLINT
: m_dataset( ds )
......@@ -267,6 +270,34 @@ private:
//! the dataset
boost::shared_ptr< WDataSetScalar > m_dataset;
};
//! Segmentation via confidence connected region growing.
class RegionGrowingSegmentation : public boost::static_visitor< boost::shared_ptr< WValueSetBase > >
{
public:
/**
* Constructor.
* \param ds The dataset.
*/
RegionGrowingSegmentation( 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* ) const;
private:
//! the dataset
boost::shared_ptr< WDataSetScalar > m_dataset;
};
#endif // OW_USE_ITK
};
......@@ -334,6 +365,69 @@ boost::shared_ptr< WValueSetBase > WMScalarSegmentation::OtsuSegmentation::opera
return res->getValueSet();
}
template< typename T >
boost::shared_ptr< WValueSetBase > WMScalarSegmentation::RegionGrowingSegmentation::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::ConfidenceConnectedImageFilter< RealType, LabelType > RegionGrowingFilter;
typename ImgType::Pointer img = makeImageFromDataSet< T >( m_dataset );
typename SmoothingType::Pointer sm = SmoothingType::New();
typename CastFilter::Pointer cf = CastFilter::New();
typename RegionGrowingFilter::Pointer rg = RegionGrowingFilter::New();
std::vector< osg::ref_ptr< WROI > > rois = WKernel::getRunningKernel()->getRoiManager()->getRois();
itk::Index< 3 > i;
WROIBox* box = NULL;
unsigned int k = 0;
while( !box && k < rois.size() )
{
box = dynamic_cast< WROIBox* >( rois[ 0 ].get() );
++k;
}
if( box )
{
i[ 0 ] = static_cast< int32_t >( box->getMinPos()[ 0 ] + box->getMaxPos()[ 0 ] ) / 2;
i[ 1 ] = static_cast< int32_t >( box->getMinPos()[ 1 ] + box->getMaxPos()[ 1 ] ) / 2;
i[ 2 ] = static_cast< int32_t >( box->getMinPos()[ 2 ] + box->getMaxPos()[ 2 ] ) / 2;
sm->SetNumberOfIterations( 5 );
sm->SetTimeStep( 0.0625 );
sm->SetConductanceParameter( 2.0 );
sm->SetInput( img );
rg->SetInput( sm->GetOutput() );
rg->SetMultiplier( 2.5 );
rg->SetNumberOfIterations( 5 );
rg->SetReplaceValue( 255.0f );
rg->SetInitialNeighborhoodRadius( 2 );
rg->SetSeed( i );
cf->SetInput( rg->GetOutput() );
try
{
cf->Update();
}
catch( ... )
{
throw WException( "Problem in Region Growing Segmentation" );
}
boost::shared_ptr< WDataSetScalar > res = makeDataSetFromImage< float >( cf->GetOutput() );
return res->getValueSet();
}
else
{
return m_dataset->getValueSet();
}
}
#endif // OW_USE_ITK
#endif // WMSCALARSEGMENTATION_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment