From 124b9349d1cc0a7316228bab71f96bc183bcbf6f Mon Sep 17 00:00:00 2001 From: Frederic Cervenansky Date: Wed, 4 May 2011 15:17:04 +0000 Subject: [PATCH] *** empty log message *** --- .../itkVTKImageToImageFilter.h | 107 +++++++++++++ .../itkVTKImageToImageFilter.txx | 144 ++++++++++++++++++ appli/TestWxSimpleView/main.cxx | 20 ++- src/creaImageIOWxISimpleDlg.hpp | 131 ++++++++++------ 4 files changed, 351 insertions(+), 51 deletions(-) create mode 100644 appli/TestWxSimpleView/itkVTKImageToImageFilter.h create mode 100644 appli/TestWxSimpleView/itkVTKImageToImageFilter.txx diff --git a/appli/TestWxSimpleView/itkVTKImageToImageFilter.h b/appli/TestWxSimpleView/itkVTKImageToImageFilter.h new file mode 100644 index 0000000..39e9b56 --- /dev/null +++ b/appli/TestWxSimpleView/itkVTKImageToImageFilter.h @@ -0,0 +1,107 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile: itkVTKImageToImageFilter.h,v $ + Language: C++ + Date: $Date: 2011/05/04 15:17:04 $ + Version: $Revision: 1.1 $ + + Copyright (c) 2002 Insight Consortium. All rights reserved. + See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __itkVTKImageToImageFilter_h +#define __itkVTKImageToImageFilter_h + +#include "itkVTKImageImport.h" +#include "vtkImageExport.h" +#include "vtkImageData.h" + +#ifndef vtkFloatingPointType +#define vtkFloatingPointType float +#endif + +namespace itk +{ + +/** \class VTKImageToImageFilter + * \brief Converts a VTK image into an ITK image and plugs a + * vtk data pipeline to an ITK datapipeline. + * + * This class puts together an itkVTKImageImporter and a vtkImageExporter. + * It takes care of the details related to the connection of ITK and VTK + * pipelines. The User will perceive this filter as an adaptor to which + * a vtkImage can be plugged as input and an itk::Image is produced as + * output. + * + * \ingroup ImageFilters + */ +template +class ITK_EXPORT VTKImageToImageFilter : public ProcessObject +{ +public: + /** Standard class typedefs. */ + typedef VTKImageToImageFilter Self; + typedef ProcessObject Superclass; + typedef SmartPointer Pointer; + typedef SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(VTKImageToImageFilter, ProcessObject); + + /** Some typedefs. */ + typedef TOutputImage OutputImageType; + typedef typename OutputImageType::ConstPointer OutputImagePointer; + typedef VTKImageImport< OutputImageType > ImporterFilterType; + typedef typename ImporterFilterType::Pointer ImporterFilterPointer; + + /** Get the output in the form of a vtkImage. + This call is delegated to the internal vtkImageImporter filter */ + const OutputImageType * GetOutput() const; + + /** Set the input in the form of a vtkImageData */ + void SetInput( vtkImageData * ); + + /** Return the internal VTK image exporter filter. + This is intended to facilitate users the access + to methods in the exporter */ + vtkImageExport * GetExporter() const; + + /** Return the internal ITK image importer filter. + This is intended to facilitate users the access + to methods in the importer */ + ImporterFilterType * GetImporter() const; + + /** This call delegate the update to the importer */ + void Update(); + +protected: + VTKImageToImageFilter(); + virtual ~VTKImageToImageFilter(); + +private: + VTKImageToImageFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + ImporterFilterPointer m_Importer; + vtkImageExport * m_Exporter; + +}; + +} // end namespace itk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "itkVTKImageToImageFilter.txx" +#endif + +#endif + + + diff --git a/appli/TestWxSimpleView/itkVTKImageToImageFilter.txx b/appli/TestWxSimpleView/itkVTKImageToImageFilter.txx new file mode 100644 index 0000000..d74ee83 --- /dev/null +++ b/appli/TestWxSimpleView/itkVTKImageToImageFilter.txx @@ -0,0 +1,144 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile: itkVTKImageToImageFilter.txx,v $ + Language: C++ + Date: $Date: 2011/05/04 15:17:04 $ + Version: $Revision: 1.1 $ + + Copyright (c) 2002 Insight Consortium. All rights reserved. + See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef _itkVTKImageToImageFilter_txx +#define _itkVTKImageToImageFilter_txx + +#include "itkVTKImageToImageFilter.h" + +namespace itk +{ + + + +/** + * Constructor + */ +template +VTKImageToImageFilter +::VTKImageToImageFilter() +{ + + m_Exporter = vtkImageExport::New(); + + m_Importer = ImporterFilterType::New(); + + m_Importer->SetUpdateInformationCallback( m_Exporter->GetUpdateInformationCallback()); + m_Importer->SetPipelineModifiedCallback( m_Exporter->GetPipelineModifiedCallback()); + m_Importer->SetWholeExtentCallback( m_Exporter->GetWholeExtentCallback()); + m_Importer->SetSpacingCallback( m_Exporter->GetSpacingCallback()); + m_Importer->SetOriginCallback( m_Exporter->GetOriginCallback()); + m_Importer->SetScalarTypeCallback( m_Exporter->GetScalarTypeCallback()); + m_Importer->SetNumberOfComponentsCallback( m_Exporter->GetNumberOfComponentsCallback()); + m_Importer->SetPropagateUpdateExtentCallback( m_Exporter->GetPropagateUpdateExtentCallback()); + m_Importer->SetUpdateDataCallback( m_Exporter->GetUpdateDataCallback()); + m_Importer->SetDataExtentCallback( m_Exporter->GetDataExtentCallback()); + m_Importer->SetBufferPointerCallback( m_Exporter->GetBufferPointerCallback()); + m_Importer->SetCallbackUserData( m_Exporter->GetCallbackUserData()); + +} + + + + +/** + * Destructor + */ +template +VTKImageToImageFilter +::~VTKImageToImageFilter() +{ + if( m_Exporter ) + { + m_Exporter->Delete(); + m_Exporter = 0; + } +} + + + +/** + * Set a vtkImageData as input + */ +template +void +VTKImageToImageFilter +::SetInput( vtkImageData * inputImage ) +{ + m_Exporter->SetInput( inputImage ); +} + + + +/** + * Get an itk::Image as output + */ +template +const typename VTKImageToImageFilter::OutputImageType * +VTKImageToImageFilter +::GetOutput() const +{ + return m_Importer->GetOutput(); +} + + + + +/** + * Get the exporter filter + */ +template +vtkImageExport * +VTKImageToImageFilter +::GetExporter() const +{ + return m_Exporter; +} + + + +/** + * Get the importer filter + */ +template +typename VTKImageToImageFilter::ImporterFilterType * +VTKImageToImageFilter +::GetImporter() const +{ + return m_Importer; +} + + + + +/** + * Delegate the Update to the importer + */ +template +void +VTKImageToImageFilter +::Update() +{ + m_Importer->Update(); +} + + + + +} // end namespace itk + +#endif + diff --git a/appli/TestWxSimpleView/main.cxx b/appli/TestWxSimpleView/main.cxx index d8d5c17..60898b9 100644 --- a/appli/TestWxSimpleView/main.cxx +++ b/appli/TestWxSimpleView/main.cxx @@ -29,6 +29,7 @@ bool myApp::OnInit( ) wxInitAllImageHandlers(); std::vector vtktest; std::vector exts; + vtkImageData* im = vtkImageData::New(); if(ITK_DIM_TO_TEST == 3) { @@ -42,8 +43,11 @@ bool myApp::OnInit( ) std::vector test = x.getImagesSelected(); // convert 3D itk to 3D vtk x.split3Din3Dvtk(); - // get vtkImageData* vector - vtktest = x.getVtkImagesSelected(); + vtktest = x.getVtkImagesSelected(); + if(vtktest.size() >0) + { + crea::VtkBasicSlicer(vtktest.front()); + } } else if (ITK_DIM_TO_TEST == 4) { @@ -53,17 +57,19 @@ bool myApp::OnInit( ) // get itkImage vector std::vector test = x.getImagesSelected(); // convert 4D itk to 3D vtk with a directory to create tempory vtk files. - x.split4Din3Dvtk("d:/temp"); + x.split4Din3Dvtk("d:/temp2"); // get vtkImageData* vector vtktest = x.getVtkImagesSelected(); + if(vtktest.size() >0) + { + crea::VtkBasicSlicer(vtktest.front()); + } } + else{} - if(vtktest.size() >0) - { - crea::VtkBasicSlicer(vtktest.front()); - } + return false; } diff --git a/src/creaImageIOWxISimpleDlg.hpp b/src/creaImageIOWxISimpleDlg.hpp index e310a07..b2d813d 100644 --- a/src/creaImageIOWxISimpleDlg.hpp +++ b/src/creaImageIOWxISimpleDlg.hpp @@ -3,14 +3,20 @@ #include "creaImageIOWxGimmickReaderDialog.h" #include #include +#include #include #include +#include +#include #include -#include "itkImageToVTKImageFilter.h" +#include +//#include "itkImageToVTKImageFilter.h" +#include #include #include #include #include +#include namespace creaImageIO { @@ -74,7 +80,7 @@ namespace creaImageIO Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadGimmick ); /// \TODO Button to select Bruker directory - + m_dicom = true; Layout(); } @@ -178,24 +184,21 @@ namespace creaImageIO dlg.ShowModal(); if (dlg.GetReturnCode() == wxID_OK) { - // infoimage=_T("DICOM: ???_EED_???"); -//EED 07JUIN2010 dlg.GetSelectedImages(m_results,3); - std::vector out; - std::vector attr; -// attr.push_back("D0028_0010"); -// attr.push_back("D0008_0023"); -// attr.push_back("D0008_1070"); - dlg.stopReading(); - dlg.getSelected(out, attr,true,""); -// crea::VtkBasicSlicer(out.front().img); - //m_results.clear(); - int size=out.size(); - int ii; - //for (ii=0;ii out; + dlg.stopReading(); + dlg.GetSelectedFiles(out); + if(m_dicom) + { + readDicomImg( out); + } + else + { + std::vector::iterator ii = out.begin(); + for (;ii != out.end();ii++) + { + readImg( (*ii).c_str() ); + } + } dlg.OnExit(); } SetReturnCode( dlg.GetReturnCode() ); @@ -256,26 +259,58 @@ namespace creaImageIO + template + void WxISimpleDlg::readDicomImg(const std::vector &i_names) + { + typedef itk::GDCMImageIO GDCMType; + typedef itk::DICOMSeriesFileNames dicnames; + GDCMType::Pointer gdcmIO = GDCMType::New(); + dicnames::Pointer generator = dicnames::New(); +// generator->SetInput(i_names); + typedef itk::ImageSeriesReader ReaderType; + typename ReaderType::Pointer reader = ReaderType::New(); + reader->SetImageIO(gdcmIO); + reader->SetFileNames(i_names); + // reader->SetFileName( i_name ); + try + { + reader->Update(); + } + catch( itk::ExceptionObject & err ) + { + // std::cout << "Caught an exception reading" << i_name << ": " << std::endl; + std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; + throw err; + } + catch(...) + { + //std::cout << "Error while reading image " << i_name << std::endl; + throw; + } + m_Iresults.push_back(reader->GetOutput()); + + } + + + + template void WxISimpleDlg::split3Din3Dvtk() { - - int ii; if ((m_Iresults).size() != 0) { - - typename TImage::Pointer image = m_Iresults[0]; - image->GetImageDimension(); - typedef itk::ImageToVTKImageFilter< TImage > ConnectorType; - ConnectorType::Pointer connector = ConnectorType::New(); - for(unsigned int i = 0 ;i < m_Iresults.size(); i++) - { - connector->SetInput(m_Iresults[i]); - connector->Update(); - vtkImageData *im = vtkImageData::New(); - im->ShallowCopy(connector->GetOutput()); - m_Vresults.push_back(im); - } + typedef itk::ImageToVTKImageFilter< TImage > ConnectorType; + ConnectorType::Pointer connector = ConnectorType::New(); + for(unsigned int i = 0 ;i < m_Iresults.size(); i++) + { + connector->SetInput(m_Iresults[i]); + connector->GetImporter()->SetDataScalarTypeToUnsignedChar(); + connector->Update(); + vtkImageData *im = vtkImageData::New(); + im->ShallowCopy(connector->GetOutput()); + im->Update(); + m_Vresults.push_back(im); + } } } @@ -289,15 +324,20 @@ namespace creaImageIO typename TImage::Pointer image = m_Iresults[0]; // Size and dims for Input Image - typedef itk::Size<4> iSize; - typedef const itk::Size<4>::SizeValueType iSizeVal; + typedef itk::Size<4> iSize; // TO REMOVE + typedef const itk::Size<4>::SizeValueType iSizeVal; // TO REMOVE iSize size = image->GetLargestPossibleRegion().GetSize(); iSizeVal *dims_size = size.GetSize(); // Output Type - typedef itk::Image ImageOutputType; + typedef itk::Image ImageOutputType; typedef itk::ImageSeriesWriter SeriesWriterType; + /* typedef itk::VectorImage VectorImageType; + VectorImageType::Pointer vect = VectorImageType::New(); + vect.*/ + + SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New(); seriesWriter->SetInput( image); @@ -305,22 +345,25 @@ namespace creaImageIO typedef itk::NumericSeriesFileNames NamesGeneratorType; NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New(); namesGenerator->SetStartIndex(0); - namesGenerator->SetEndIndex(dims_size[3]-1); + namesGenerator->SetEndIndex(dims_size[3]-1); // TO CHANGE namesGenerator->SetIncrementIndex(1); std::string format = i_dir; format += "/image%03d.vtk"; namesGenerator->SetSeriesFormat( format.c_str() ); - - seriesWriter->SetFileNames( namesGenerator->GetFileNames() ); + const std::vector names = namesGenerator->GetFileNames(); + seriesWriter->SetFileNames( names ); try { seriesWriter->Update(); - vtkImageReader2 *vReader =vtkImageReader2::New(); - for(unsigned int i = 0 ;i < dims_size[3]; i++) + vtkImageReader2 *vReader = vtkImageReader2::New(); + vReader->SetFilePrefix(".vtk"); + std::vector::const_iterator it = names.begin(); + for( ;it != names.end(); ++it) { - vReader->SetFileName(namesGenerator->GetFileNames()[i].c_str()); + vReader->SetFileName((*it).c_str()); vReader->Update(); + crea::VtkBasicSlicer(vReader->GetOutput()); m_Vresults.push_back(vReader->GetOutput()); } } -- 2.47.1