]> Creatis software - cpPlugins.git/blob - plugins/ImageFilters/RegionOfInterestImageFilter.cxx
...
[cpPlugins.git] / plugins / ImageFilters / RegionOfInterestImageFilter.cxx
1 #include <plugins/ImageFilters/RegionOfInterestImageFilter.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_Instances_ExtractImageFilters.h>
4
5 // -------------------------------------------------------------------------
6 cpPluginsImageFilters::RegionOfInterestImageFilter::
7 RegionOfInterestImageFilter( )
8   : Superclass( )
9 {
10   this->_AddInput( "Input" );
11   this->_AddOutput< cpPlugins::Image >( "Output" );
12   this->m_Parameters.ConfigureAsBool( "AutomaticRegion" );
13   this->m_Parameters.ConfigureAsReal( "BackgroundValue" );
14   this->m_Parameters.SetBool( "AutomaticRegion", true );
15   this->m_Parameters.SetReal( "BackgroundValue", 0 );
16 }
17
18 // -------------------------------------------------------------------------
19 cpPluginsImageFilters::RegionOfInterestImageFilter::
20 ~RegionOfInterestImageFilter( )
21 {
22 }
23
24 // -------------------------------------------------------------------------
25 void cpPluginsImageFilters::RegionOfInterestImageFilter::
26 _GenerateData( )
27 {
28   auto image = this->GetInputData< itk::DataObject >( "Input" );
29   cpPlugins_Image_Demangle_Pixel_AllScalars     ( _GD0, image, 2 );
30   else cpPlugins_Image_Demangle_Pixel_AllScalars( _GD0, image, 3 );
31   else this->_Error( "No valid input image." );
32 }
33
34 // -------------------------------------------------------------------------
35 template< class _TInputImage >
36 void cpPluginsImageFilters::RegionOfInterestImageFilter::
37 _GD0( _TInputImage* image )
38 {
39   typedef
40     itk::RegionOfInterestImageFilter< _TInputImage, _TInputImage >
41     _TFilter;
42   typedef
43     cpExtensions::Algorithms::RegionOfInterestImageCalculator< _TInputImage >
44     _TCalculator;
45
46   if( this->m_Parameters.GetBool( "AutomaticRegion" ) )
47   {
48     typename _TCalculator::Pointer calculator = _TCalculator::New( );
49     calculator->SetImage( image );
50     calculator->Compute( );
51
52     auto minidx = calculator->GetMinimum( );
53     auto maxidx = calculator->GetMaximum( );
54     typename _TInputImage::SizeType size;
55     for( unsigned int d = 0; d < _TInputImage::ImageDimension; ++d )
56       size[ d ] = maxidx[ d ] - minidx[ d ] + 1;
57
58     typename _TInputImage::RegionType roi;
59     roi.SetIndex( minidx );
60     roi.SetSize( size );
61     
62     auto filter = this->_CreateITK< _TFilter >( );
63     filter->SetInput( image );
64     filter->SetRegionOfInterest( roi );
65     filter->Update( );
66     this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
67   }
68   else
69     this->_Error( "Manual region not yet implemented." );
70 }
71
72 // eof - $RCSfile$