From: Frederic Cervenansky Date: Wed, 27 Apr 2011 19:12:20 +0000 (+0000) Subject: convert Analyze files to vtkImageData X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=a1c63fa4c0c98ac09cacfd5258e2b873ee15fb83;p=creaImageIO.git convert Analyze files to vtkImageData --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bce3f2b..ea8d332 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -74,7 +74,7 @@ FILE(GLOB SOURCES_CREAIMAGEIO_IMG_READER IF(USE_ITK) FILE(GLOB SOURCES_CREAIMAGEIO_ITK_DLG creaImageIOWxIsimpleDlg.h - creaImageIOWxIsimpleDlg.txx + creaImageIOWxIsimpleDlg.hpp ) ENDIF(USE_ITK) diff --git a/src/creaImageIOWxISimpleDlg.hpp b/src/creaImageIOWxISimpleDlg.hpp new file mode 100644 index 0000000..e310a07 --- /dev/null +++ b/src/creaImageIOWxISimpleDlg.hpp @@ -0,0 +1,339 @@ +//#include "creaImageIOWxISimpleDlg.h" + +#include "creaImageIOWxGimmickReaderDialog.h" +#include +#include +#include +#include +#include +#include "itkImageToVTKImageFilter.h" +#include +#include +#include +#include + +namespace creaImageIO +{ + template + typename TImage2::Pointer ReadImage( const std::string &fileName) + { + + + + typename TImage2::Pointer image; + + typedef itk::ImageFileReader ReaderType; + typename ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( fileName.c_str() ); + try + { + reader->Update(); + } + catch( itk::ExceptionObject & err ) + { + std::cout << "Caught an exception reading" << fileName << ": " << std::endl; + std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; + throw err; + } + catch(...) + { + std::cout << "Error while reading image " << fileName << std::endl; + throw; + } + image = reader->GetOutput(); + + return image; + } + + + ///Ctor + template + WxISimpleDlg::WxISimpleDlg(wxWindow *parent, + wxString i_title, + const std::string i_namedescp , + const std::string i_namedb) + : wxDialog(parent, -1,_T("DISPLAY IMAGES"), wxDefaultPosition, wxSize(230,150)) + { + namedescp = i_namedescp; + namedb = i_namedb; + + if(!i_title.empty()) + { + this->SetTitle(i_title); + } + // Button to select file(s) + wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) ); + Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadFile ); + + // Button to select directory + wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) ); + Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadDirectory ); + + // button to select creaImageIO + wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) ); + Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadGimmick ); + + /// \TODO Button to select Bruker directory + + Layout(); + + } +////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + template + void WxISimpleDlg::OnReadFile(wxCommandEvent& event) + { + int resultShowModal; + wxFileDialog* fileDlg = new wxFileDialog( 0, _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition); + + resultShowModal = fileDlg->ShowModal(); + if ( resultShowModal==wxID_OK ) + { + wxArrayString wxArray; + fileDlg->GetPaths(wxArray); + if(wxArray.size() >0) + { + for( int i = 0; i < wxArray.GetCount(); i++) + { + readImg( crea::wx2std(wxArray[i])); + } + } + else { + // TO DO WARNING MESSAGES + } + } + SetReturnCode( resultShowModal ); +// Close(); + EndModal( resultShowModal ); + } + + + template + void WxISimpleDlg::setExts(std::vector i_exts) + { + m_exts = i_exts; + } + ////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + template + void WxISimpleDlg::OnReadDirectory(wxCommandEvent &event) + { + int resultShowModal; + bool bvalid = false; + long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST; + wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style); + + resultShowModal = dirDlg->ShowModal(); + if ( resultShowModal==wxID_OK ) + { + std::string path = crea::wx2std(dirDlg->GetPath()); + typedef boost::filesystem::directory_iterator dir_it; + dir_it itr(path); + dir_it end_itr; + /*if (boost::filesystem::exists(path)) + {*/ + for(;itr != end_itr; ++itr) + { + bvalid = m_exts.size() == 0? true : false; + std::vector::iterator it = m_exts.begin(); + std::string ext = itr->filename().substr(itr->filename().find_last_of(".")); + for(; it != m_exts.end(); it++) + { + if(ext == (*it) ) + { + bvalid = true; + break; + } + } + if (!boost::filesystem::is_directory(itr->status()) && bvalid) + { + readImg(itr->string().c_str()); + } + } + } + SetReturnCode( resultShowModal ); +// Close(); + EndModal( resultShowModal ); + } + + ////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + template + void WxISimpleDlg::OnReadGimmick(wxCommandEvent &event) + { + // Run Gimmick + WxGimmickReaderDialog dlg(0,-1, + namedescp, + namedb, + _T("Select image(s) - Gimmick! (c) CREATIS-LRMN 2008"), + wxDefaultPosition, + wxSize(810,750), + GIMMICK_2D_IMAGE_SELECTION, + GIMMICK_3D_IMAGE_SELECTION, + _3D, + 1); + 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 + wxString WxISimpleDlg::getInfoImage() + { + return infoimage; + } + + + ////////////////////////////////////////////////////////////////////// + // Return the results vector // + ////////////////////////////////////////////////////////////////////// + template + std::vector WxISimpleDlg::getImagesSelected() + { + return m_Iresults; + } + + template + void WxISimpleDlg::readImg(const std::string &i_name) + { + // FCY : just to check but not needed, we hardly suppose that we load only same type and dim of images + // uncomment to test + /*typedef itk::ImageIOBase::IOComponentType ScalarPixelType; + itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode); + imageIO->SetFileName(i_name.c_str()); + imageIO->ReadImageInformation(); + ScalarPixelType pixelType = imageIO->GetComponentType(); + const size_t dims = imageIO->GetNumberOfDimensions();*/ + + typedef itk::ImageFileReader ReaderType; + typename ReaderType::Pointer reader = ReaderType::New(); + 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); + } + } + } + + + + template + void WxISimpleDlg::split4Din3Dvtk(const std::string &i_dir) + { + if ((m_Iresults).size() != 0) + { + 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; + iSize size = image->GetLargestPossibleRegion().GetSize(); + iSizeVal *dims_size = size.GetSize(); + + // Output Type + typedef itk::Image ImageOutputType; + typedef itk::ImageSeriesWriter SeriesWriterType; + + SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New(); + seriesWriter->SetInput( image); + + // from JPR file to generate output files + typedef itk::NumericSeriesFileNames NamesGeneratorType; + NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New(); + namesGenerator->SetStartIndex(0); + namesGenerator->SetEndIndex(dims_size[3]-1); + namesGenerator->SetIncrementIndex(1); + std::string format = i_dir; + format += "/image%03d.vtk"; + namesGenerator->SetSeriesFormat( format.c_str() ); + + seriesWriter->SetFileNames( namesGenerator->GetFileNames() ); + + try + { + seriesWriter->Update(); + vtkImageReader2 *vReader =vtkImageReader2::New(); + for(unsigned int i = 0 ;i < dims_size[3]; i++) + { + vReader->SetFileName(namesGenerator->GetFileNames()[i].c_str()); + vReader->Update(); + m_Vresults.push_back(vReader->GetOutput()); + } + } + catch( itk::ExceptionObject & excp ) + { + std::cerr << "Exception thrown while writing the series " << std::endl; + std::cerr << excp << std::endl; + //return EXIT_FAILURE; + } + + } + + } + + }// namespace end + diff --git a/src/creaImageIOWxIsimpleDlg.h b/src/creaImageIOWxIsimpleDlg.h new file mode 100644 index 0000000..6527b01 --- /dev/null +++ b/src/creaImageIOWxIsimpleDlg.h @@ -0,0 +1,64 @@ +#include "creaImageIOSimpleView.h" +#include +#include + +namespace creaImageIO +{ + /** + * \ingroup View + */ + + /// Simple Wxwidgets interface to select file(s) or directory or from creaImageIO database to display + template + class __declspec(dllexport) WxISimpleDlg : public wxDialog + { + public: + /// Ctor + WxISimpleDlg( wxWindow *parent, + wxString i_title =_T(""), + const std::string i_namedescp = "localdatabase_Descriptor.dscp", + const std::string i_namedb = "Local Database" + ); + + ~WxISimpleDlg(){}; + + void setExts(std::vector); + /// Callback to read file(s) + void OnReadFile(wxCommandEvent& event); + + /// Callback to read directory, no recursive + void OnReadDirectory(wxCommandEvent &event); + + /// Callback to select from creaImageIO database + void OnReadGimmick(wxCommandEvent &event); + + /// return a vtkImageData vector of selected images, if available + std::vector getVtkImagesSelected() {return m_Vresults;} + + /// return a itkImage vector of selected images, if available + std::vector getImagesSelected(); + + + wxString getInfoImage(); + + void split4Din3Dvtk(const std::string &i_dir); + void split3Din3Dvtk(); + private: + + std::vector m_exts; + std::string namedescp; + std::string namedb; + wxString infoimage; + + /// interface to read data + SimpleView m_view; + void readImg(const std::string &i_name); + + std::vector m_Iresults; + + std::vector m_Vresults; + + }; + + +} diff --git a/src/creaImageIOWxSimpleDlg.cpp b/src/creaImageIOWxSimpleDlg.cpp index 43e2ac1..afe1116 100644 --- a/src/creaImageIOWxSimpleDlg.cpp +++ b/src/creaImageIOWxSimpleDlg.cpp @@ -1,9 +1,13 @@ #include "creaImageIOWxSimpleDlg.h" #include "creaImageIOWxGimmickReaderDialog.h" +//#include +//#include +//#include namespace creaImageIO { + ///Ctor WxSimpleDlg::WxSimpleDlg(wxWindow *parent, wxString i_title, @@ -50,6 +54,9 @@ namespace creaImageIO fileDlg->GetPaths(wxArray); if(wxArray.size() >0) { + /* typedef itk::Image< float, 4 > ImageType; + ReadImage(crea::wx2std(wxArray[0]).c_str());*/ + infoimage=_T("FILE(s):")+wxArray[0]; std::vector files; for( int i = 0; i < wxArray.GetCount(); i++) diff --git a/src/itkImageToVTKImageFilter.h b/src/itkImageToVTKImageFilter.h new file mode 100644 index 0000000..145c840 --- /dev/null +++ b/src/itkImageToVTKImageFilter.h @@ -0,0 +1,134 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile: itkImageToVTKImageFilter.h,v $ + Language: C++ + Date: $Date: 2011/04/27 19:12:20 $ + 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 __itkImageToVTKImageFilter_h +#define __itkImageToVTKImageFilter_h + +#include "itkVTKImageExport.h" +#include "vtkImageImport.h" +#include "vtkImageData.h" +#include + +namespace itk +{ + +/** \class ImageToVTKImageFilter + * \brief Converts an ITK image into a VTK image and plugs a + * itk data pipeline to a VTK datapipeline. + * + * This class puts together an itkVTKImageExporter and a vtkImageImporter. + * 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 + * an itk::Image can be plugged as input and a vtkImage is produced as + * output. + * + * \ingroup ImageFilters + */ +template +class ITK_EXPORT ImageToVTKImageFilter : public ProcessObject +{ +public: + /** Standard class typedefs. */ + typedef ImageToVTKImageFilter 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(ImageToVTKImageFilter, ProcessObject); + + /** Some typedefs. */ + typedef TInputImage InputImageType; + typedef typename InputImageType::ConstPointer InputImagePointer; + typedef VTKImageExport< InputImageType> ExporterFilterType; + typedef typename ExporterFilterType::Pointer ExporterFilterPointer; + + /** Get the output in the form of a vtkImage. + This call is delegated to the internal vtkImageImporter filter */ + vtkImageData * GetOutput() const; + + /** Set the input in the form of an itk::Image */ + void SetInput( const InputImageType * ); + + /** Return the internal VTK image importer filter. + This is intended to facilitate users the access + to methods in the importer */ + vtkImageImport * GetImporter() const; + + /** Return the internal ITK image exporter filter. + This is intended to facilitate users the access + to methods in the exporter */ + ExporterFilterType * GetExporter() const; + + /** This call delegate the update to the importer */ + void Update(); + + const std::vector& getvtest() const + { + return m_vtest; + } + + int testsize() + { + return m_vtest.size(); + } + + std::vector addvector(const std::vector& v) { + for (unsigned int i=0; i& addtest(double toto) + { + m_vtest.push_back(toto); + return m_vtest; + } + std::vector tralala() + { + std::vector w; + for (double i=0; i<10; i++) + w.push_back(i); + return w; + + } + +protected: + ImageToVTKImageFilter(); + virtual ~ImageToVTKImageFilter(); + +private: + ImageToVTKImageFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + ExporterFilterPointer m_Exporter; + vtkImageImport * m_Importer; + std::vector m_vtest; +}; + +} // end namespace itk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "itkImageToVTKImageFilter.txx" +#endif + +#endif + + + diff --git a/src/itkImageToVTKImageFilter.txx b/src/itkImageToVTKImageFilter.txx new file mode 100644 index 0000000..c74c9cf --- /dev/null +++ b/src/itkImageToVTKImageFilter.txx @@ -0,0 +1,144 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile: itkImageToVTKImageFilter.txx,v $ + Language: C++ + Date: $Date: 2011/04/27 19:12:20 $ + 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 _itkImageToVTKImageFilter_txx +#define _itkImageToVTKImageFilter_txx + +#include "itkImageToVTKImageFilter.h" + +namespace itk +{ + + + +/** + * Constructor + */ +template +ImageToVTKImageFilter +::ImageToVTKImageFilter() +{ + + m_Importer = vtkImageImport::New(); + + m_Exporter = ExporterFilterType::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 +ImageToVTKImageFilter +::~ImageToVTKImageFilter() +{ + if( m_Importer ) + { + m_Importer->Delete(); + m_Importer = 0; + } +} + + + +/** + * Set an itk::Image as input + */ +template +void +ImageToVTKImageFilter +::SetInput( const InputImageType * inputImage ) +{ + m_Exporter->SetInput( inputImage ); +} + + + +/** + * Get a vtkImage as output + */ +template +vtkImageData * +ImageToVTKImageFilter +::GetOutput() const +{ + return m_Importer->GetOutput(); +} + + + + +/** + * Get the importer filter + */ +template +vtkImageImport * +ImageToVTKImageFilter +::GetImporter() const +{ + return m_Importer; +} + + + +/** + * Get the exporter filter + */ +template +typename ImageToVTKImageFilter::ExporterFilterType * +ImageToVTKImageFilter +::GetExporter() const +{ + return m_Exporter.GetPointer(); +} + + + +/** + * Delegate the Update to the importer + */ +template +void +ImageToVTKImageFilter +::Update() +{ + m_Importer->Update(); +} + + + + + +} // end namespace itk + +#endif +