]> Creatis software - cpPlugins.git/blob - plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx
...
[cpPlugins.git] / plugins / ITKImageGenericFilters_1 / RegionOfInterestImageFilter.cxx
1 #include <ITKImageGenericFilters_1/RegionOfInterestImageFilter.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/BoundingBox.h>
4 #include <cpInstances/DataObjects/Image_Demanglers.h>
5
6 #include <itkRGBPixel.h>
7 #include <itkRGBAPixel.h>
8 #include <itkRegionOfInterestImageFilter.h>
9
10 // -------------------------------------------------------------------------
11 cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
12 RegionOfInterestImageFilter( )
13   : Superclass( )
14 {
15   typedef cpInstances::DataObjects::Image _TImage;
16   typedef cpInstances::DataObjects::BoundingBox _TBBox;
17   this->_ConfigureInput< _TImage >( "Input", true, false );
18   this->_ConfigureInput< _TBBox >( "BoundingBox", true, false );
19   this->_ConfigureOutput< _TImage >( "Output" );
20 }
21
22 // -------------------------------------------------------------------------
23 cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
24 ~RegionOfInterestImageFilter( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 void cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
30 _GenerateData( )
31 {
32   auto o = this->GetInputData( "Input" );
33   cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
34     this->_Error( "Invalid input image." );
35 }
36
37 // -------------------------------------------------------------------------
38 template< class _TImage >
39 void cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
40 _GD0( _TImage* input )
41 {
42   typedef cpInstances::DataObjects::BoundingBox _TBBox;
43   typedef itk::RegionOfInterestImageFilter< _TImage, _TImage > _TFilter;
44   typedef typename _TImage::IndexType  _TIndex;
45   typedef typename _TImage::PointType  _TPoint;
46   typedef typename _TImage::RegionType _TRegion;
47   typedef typename _TImage::SizeType   _TSize;
48
49   auto bb = this->GetInput< _TBBox >( "BoundingBox" );
50
51   _TRegion in_region = input->GetRequestedRegion( );
52   _TIndex in_i0 = in_region.GetIndex( );
53   _TIndex in_i1 = in_i0 + in_region.GetSize( );
54   _TIndex i0, i1;
55   input->TransformPhysicalPointToIndex( bb->GetMinimum< _TPoint >( ), i0 );
56   input->TransformPhysicalPointToIndex( bb->GetMaximum< _TPoint >( ), i1 );
57   for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
58   {
59     if( i0[ d ] < in_i0[ d ] ) i0[ d ] = in_i0[ d ];
60     if( i1[ d ] > in_i1[ d ] ) i1[ d ] = in_i1[ d ];
61
62   } // rof
63
64   _TSize size;
65   for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
66     size[ d ] = i1[ d ] - i0[ d ];
67
68   _TRegion region;
69   region.SetIndex( i0 );
70   region.SetSize( size );
71
72   auto filter = this->_CreateITK< _TFilter >( );
73   filter->SetInput( input );
74   filter->SetRegionOfInterest( region );
75   filter->Update( );
76   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
77 }
78
79 // eof - $RCSfile$