From c43e85355af180dcad5859644826f4acf485b604 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leonardo=20Fl=C3=B3rez-Valencia?= Date: Thu, 29 Sep 2016 15:19:19 -0500 Subject: [PATCH] RescaleIntensity filter added. --- CMakeLists.txt | 2 +- lib/Instances/cpPlugins_Images.i | 2 +- lib/cpBaseQtApplication/MainWindow.cxx | 1 - lib/cpPlugins/Interface/Plugins.cxx | 107 +++++++++-------- plugins/CMakeLists.txt | 1 + .../RescaleIntensityImageFilter.cxx | 54 +++++++++ .../RescaleIntensityImageFilter.h | 29 +++++ plugins/ImageSources/RandomImageSource.cxx | 108 ++++++++++++++++++ plugins/ImageSources/RandomImageSource.h | 32 ++++++ 9 files changed, 286 insertions(+), 50 deletions(-) create mode 100644 plugins/ImageGenericFilters/RescaleIntensityImageFilter.cxx create mode 100644 plugins/ImageGenericFilters/RescaleIntensityImageFilter.h create mode 100644 plugins/ImageSources/RandomImageSource.cxx create mode 100644 plugins/ImageSources/RandomImageSource.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 85fe564..8fb53f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.5) +CMAKE_MINIMUM_REQUIRED(VERSION 3.0) ## ======================== ## == Project definition == diff --git a/lib/Instances/cpPlugins_Images.i b/lib/Instances/cpPlugins_Images.i index fd3781b..140d90c 100644 --- a/lib/Instances/cpPlugins_Images.i +++ b/lib/Instances/cpPlugins_Images.i @@ -31,7 +31,7 @@ c itk::ImportImageContainer< unsigned long, itk::FixedArray< #pixels#, #process_ c itk::ImportImageContainer< unsigned long, itk::Offset< #process_dims# > > c itk::Image$Base;Region$< #process_dims# > -c itk::Image< #pixels#, #process_dims# > +c itk::Image< $bool;#pixels#$, #process_dims# > c itk::Image< itk::#colors#< #pixels# >, #process_dims# > c itk::Image< std::complex< #reals# >, #process_dims# > c itk::Image< itk::#vectors#< #reals#, #process_dims# >, #process_dims# > diff --git a/lib/cpBaseQtApplication/MainWindow.cxx b/lib/cpBaseQtApplication/MainWindow.cxx index 67a3844..8e1abab 100644 --- a/lib/cpBaseQtApplication/MainWindow.cxx +++ b/lib/cpBaseQtApplication/MainWindow.cxx @@ -70,7 +70,6 @@ MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent ) // Get the plugins interface this->m_Plugins = TPlugins::New( ); - try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { } try { this->m_Plugins->OpenEnvironments( env.str( ) ); } catch( ... ) { } try { this->m_Plugins->SaveEnvironments( this->m_RunPath ); } catch( ... ) { } this->updateEnvironment( ); diff --git a/lib/cpPlugins/Interface/Plugins.cxx b/lib/cpPlugins/Interface/Plugins.cxx index 3e68371..b5f9269 100644 --- a/lib/cpPlugins/Interface/Plugins.cxx +++ b/lib/cpPlugins/Interface/Plugins.cxx @@ -91,17 +91,7 @@ AddEnvironments( const std::string& new_environment ) std::stringstream dir; dir << cpExtensions::CanonicalPath( *i ); if( dir.str( ) != "" ) - { - if( !cpExtensions::IsPathSeparator( dir.str( ).back( ) ) ) - dir << cpExtensions_PATH_SEPARATOR; - std::stringstream name; - name << dir.str( ) << cpPlugins_CONFIG; - std::ifstream check( name.str( ).c_str( ), std::ifstream::binary ); - if( check ) - this->m_Paths.insert( dir.str( ) ); - check.close( ); - - } // fi + this->m_Paths.insert( dir.str( ) ); } // rof } @@ -162,45 +152,59 @@ LoadEnvironments( ) void cpPlugins::Interface::Plugins:: SaveEnvironments( const std::string& dir ) const { - if( this->m_Paths.size( ) > 0 ) - { - std::stringstream buffer; - auto i = this->m_Paths.begin( ); - for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i ) - buffer << *i << std::endl; + std::stringstream buffer; + for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i ) + buffer << *i << std::endl; - std::stringstream fname; - fname << dir; - if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) - fname << cpExtensions_PATH_SEPARATOR; - fname << cpPlugins_PATHS; - if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) ) - throw std::runtime_error( "Error writing environment file." ); - } - else - throw std::runtime_error( "No paths to save." ); + std::stringstream fname; + fname << dir; + if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) + fname << cpExtensions_PATH_SEPARATOR; + fname << cpPlugins_PATHS; + if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) ) + throw std::runtime_error( "Error writing environment file." ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: OpenEnvironments( const std::string& dir ) { - std::stringstream fname; - fname << dir; - if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) - fname << cpExtensions_PATH_SEPARATOR; - fname << cpPlugins_PATHS; - std::string buffer; - if( cpExtensions::Read( buffer, fname.str( ) ) ) + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR ); + for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt ) { - std::istringstream input( buffer ); - std::stringstream paths; - for( std::string line; std::getline( input, line ); ) - paths << line << cpPlugins_ENV_SEPARATOR; - this->AddEnvironments( paths.str( ) ); - } - else - throw std::runtime_error( "Error opening environment file." ); + std::stringstream fname; + fname << *tIt; + if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) + fname << cpExtensions_PATH_SEPARATOR; + fname << cpPlugins_PATHS; + std::string buffer; + if( cpExtensions::Read( buffer, fname.str( ) ) ) + { + std::istringstream input( buffer ); + std::stringstream paths; + for( std::string line; std::getline( input, line ); ) + paths << line << cpPlugins_ENV_SEPARATOR; + this->AddEnvironments( paths.str( ) ); + } + else + { + bool success = true; + try + { + this->LoadDirectory( dir ); + } + catch( ... ) + { + success = false; + + } // yrt + if( success ) + this->AddEnvironments( dir ); + + } // fi + + } // rof } // ------------------------------------------------------------------------- @@ -425,13 +429,22 @@ Plugins( ) if( p != NULL ) str << p << cpPlugins_ENV_SEPARATOR; str << "."; - this->AddEnvironments( str.str( ) ); + this->OpenEnvironments( str.str( ) ); // Try to read locally defined paths - std::vector< std::string > tokens; - cpExtensions::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR ); - for( auto t = tokens.begin( ); t != tokens.end( ); ++t ) - this->OpenEnvironments( *t ); + /* TODO + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR ); + for( auto t = tokens.begin( ); t != tokens.end( ); ++t ) + { + try + { + this->OpenEnvironments( *t ); + } + catch( ... ) { } + + } // rof + */ } // ------------------------------------------------------------------------- diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index a985afe..cbf2528 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -11,6 +11,7 @@ SET( ImageGenericFilters ImageGradientFilters ImageMeshFilters + ImageSources ImageThresholdFilters IO MeshFilters diff --git a/plugins/ImageGenericFilters/RescaleIntensityImageFilter.cxx b/plugins/ImageGenericFilters/RescaleIntensityImageFilter.cxx new file mode 100644 index 0000000..01ccdbc --- /dev/null +++ b/plugins/ImageGenericFilters/RescaleIntensityImageFilter.cxx @@ -0,0 +1,54 @@ +#include +#include + +#include +#include +#include +#include + +// ------------------------------------------------------------------------- +cpPluginsImageGenericFilters::RescaleIntensityImageFilter:: +RescaleIntensityImageFilter( ) + : Superclass( ) +{ + this->_ConfigureInput< cpPlugins::DataObjects::Image >( "Input", true, false ); + this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" ); + + this->m_Parameters.ConfigureAsReal( "OutputMinimum" ); + this->m_Parameters.ConfigureAsReal( "OutputMaximum" ); + + this->m_Parameters.SetReal( "OutputMinimum", 0 ); + this->m_Parameters.SetReal( "OutputMaximum", 1 ); +} + +// ------------------------------------------------------------------------- +cpPluginsImageGenericFilters::RescaleIntensityImageFilter:: +~RescaleIntensityImageFilter( ) +{ +} + +// ------------------------------------------------------------------------- +void cpPluginsImageGenericFilters::RescaleIntensityImageFilter:: +_GenerateData( ) +{ + auto o = this->GetInputData( "Input" ); + cpPlugins_Demangle_ImageScalars_Dims( o, _GD0 ); + else this->_Error( "Invalid input image." ); +} + +// ------------------------------------------------------------------------- +template< class _TImage > +void cpPluginsImageGenericFilters::RescaleIntensityImageFilter:: +_GD0( _TImage* input ) +{ + typedef itk::RescaleIntensityImageFilter< _TImage > _TFilter; + + auto filter = this->_CreateITK< _TFilter >( ); + filter->SetInput( input ); + filter->SetOutputMinimum( this->m_Parameters.GetReal( "OutputMinimum" ) ); + filter->SetOutputMaximum( this->m_Parameters.GetReal( "OutputMaximum" ) ); + filter->Update( ); + this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) ); +} + +// eof - $RCSfile$ diff --git a/plugins/ImageGenericFilters/RescaleIntensityImageFilter.h b/plugins/ImageGenericFilters/RescaleIntensityImageFilter.h new file mode 100644 index 0000000..d04ee48 --- /dev/null +++ b/plugins/ImageGenericFilters/RescaleIntensityImageFilter.h @@ -0,0 +1,29 @@ +#ifndef __cpPluginsImageGenericFilters__RescaleIntensityImageFilter__h__ +#define __cpPluginsImageGenericFilters__RescaleIntensityImageFilter__h__ + +#include +#include + +namespace cpPluginsImageGenericFilters +{ + /** + */ + class cpPluginsImageGenericFilters_EXPORT RescaleIntensityImageFilter + : public cpPlugins::BaseObjects::ProcessObject + { + cpPluginsObject( + RescaleIntensityImageFilter, + cpPlugins::BaseObjects::ProcessObject, + ImageFilters + ); + + protected: + template< class _TImage > + inline void _GD0( _TImage* input ); + }; + +} // ecapseman + +#endif // __cpPluginsImageGenericFilters__RescaleIntensityImageFilter__h__ + +// eof - $RCSfile$ diff --git a/plugins/ImageSources/RandomImageSource.cxx b/plugins/ImageSources/RandomImageSource.cxx new file mode 100644 index 0000000..b0ef6c4 --- /dev/null +++ b/plugins/ImageSources/RandomImageSource.cxx @@ -0,0 +1,108 @@ +#include +#include + +#include +#include + +// ------------------------------------------------------------------------- +cpPluginsImageSources::RandomImageSource:: +RandomImageSource( ) + : Superclass( ) +{ + this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" ); + + this->m_Parameters.ConfigureAsUintList( "Size" ); + this->m_Parameters.ConfigureAsRealList( "Spacing" ); + + std::vector< std::string > pixels; + pixels.push_back( "char" ); + pixels.push_back( "short" ); + pixels.push_back( "int" ); + pixels.push_back( "long" ); + pixels.push_back( "uchar" ); + pixels.push_back( "ushort" ); + pixels.push_back( "uint" ); + pixels.push_back( "ulong" ); + pixels.push_back( "float" ); + pixels.push_back( "double" ); + this->m_Parameters.ConfigureAsChoices( "PixelType", pixels ); + this->m_Parameters.SetSelectedChoice( "PixelType", "uchar" ); + + std::vector< std::string > dims; + dims.push_back( "1" ); + dims.push_back( "2" ); + dims.push_back( "3" ); + dims.push_back( "4" ); + this->m_Parameters.ConfigureAsChoices( "Dimension", dims ); + this->m_Parameters.SetSelectedChoice( "Dimension", "2" ); +} + +// ------------------------------------------------------------------------- +cpPluginsImageSources::RandomImageSource:: +~RandomImageSource( ) +{ +} + +// ------------------------------------------------------------------------- +void cpPluginsImageSources::RandomImageSource:: +_GenerateData( ) +{ + auto pixel = this->m_Parameters.GetSelectedChoice( "PixelType" ); + if ( pixel == "char" ) this->_GD0< char >( ); + else if( pixel == "short" ) this->_GD0< short >( ); + else if( pixel == "int" ) this->_GD0< int >( ); + else if( pixel == "long" ) this->_GD0< long >( ); + else if( pixel == "uchar" ) this->_GD0< unsigned char >( ); + else if( pixel == "ushort" ) this->_GD0< unsigned short >( ); + else if( pixel == "uint" ) this->_GD0< unsigned int >( ); + else if( pixel == "ulong" ) this->_GD0< unsigned long >( ); + else if( pixel == "float" ) this->_GD0< float >( ); + else if( pixel == "double" ) this->_GD0< double >( ); + else this->_Error( "Invalid pixel type." ); +} + +// ------------------------------------------------------------------------- +template< class _TPixel > +void cpPluginsImageSources::RandomImageSource:: +_GD0( ) +{ + auto dim = this->m_Parameters.GetSelectedChoice( "Dimension" ); + if ( dim == "1" ) this->_GD1< _TPixel, 1 >( ); + else if( dim == "2" ) this->_GD1< _TPixel, 2 >( ); + else if( dim == "3" ) this->_GD1< _TPixel, 3 >( ); + else if( dim == "4" ) this->_GD1< _TPixel, 4 >( ); + else this->_Error( "Invalid dimension." ); +} + +// ------------------------------------------------------------------------- +template< class _TPixel, unsigned int _VDim > +void cpPluginsImageSources::RandomImageSource:: +_GD1( ) +{ + typedef itk::Image< _TPixel, _VDim > _TImage; + typedef itk::RandomImageSource< _TImage > _TFilter; + + auto size = this->m_Parameters.GetUintList( "Size" ); + auto spacing = this->m_Parameters.GetRealList( "Spacing" ); + if( size.size( ) < _VDim ) + this->_Error( "Invalid size." ); + if( spacing.size( ) < _VDim ) + this->_Error( "Invalid spacing." ); + + typename _TImage::SizeType out_size; + typename _TImage::SpacingType out_spac; + for( unsigned int i = 0; i < _VDim; ++i ) + { + out_size[ i ] = size[ i ]; + out_spac[ i ] = spacing[ i ]; + + } // rof + + _TFilter* filter = this->_CreateITK< _TFilter >( ); + filter->SetSize( out_size ); + filter->SetSpacing( out_spac ); + filter->Update( ); + this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) ); +} + +// eof - $RCSfile$ diff --git a/plugins/ImageSources/RandomImageSource.h b/plugins/ImageSources/RandomImageSource.h new file mode 100644 index 0000000..ad9b581 --- /dev/null +++ b/plugins/ImageSources/RandomImageSource.h @@ -0,0 +1,32 @@ +#ifndef __cpPluginsImageSources__RandomImageSource__h__ +#define __cpPluginsImageSources__RandomImageSource__h__ + +#include +#include + +namespace cpPluginsImageSources +{ + /** + */ + class cpPluginsImageSources_EXPORT RandomImageSource + : public cpPlugins::BaseObjects::ProcessObject + { + cpPluginsObject( + RandomImageSource, + cpPlugins::BaseObjects::ProcessObject, + ImageSources + ); + + protected: + template< class _TPixel > + inline void _GD0( ); + + template< class _TPixel, unsigned int _VDim > + inline void _GD1( ); + }; + +} // ecapseman + +#endif // __cpPluginsImageSources__RandomImageSource__h__ + +// eof - $RCSfile$ -- 2.45.1