32 #include <itkAnalyzeImageIO.h>
33 #include <itkImageFileReader.h>
34 #include <itkImageSeriesReader.h>
36 #include <itkImageSeriesWriter.h>
37 #include <itkGDCMImageIO.h>
38 #include <itkDICOMSeriesFileNames.h>
39 #include <itkNumericSeriesFileNames.h>
40 #include <itkVectorImage.h>
41 #include <itkMetaImageIO.h>
42 #include <vtkImageReader2.h>
43 #include <vtkMetaImageReader.h>
44 #include <boost/filesystem/path.hpp>
45 #include <boost/filesystem.hpp>
46 #include <boost/utility.hpp>
47 #include <creaVtkBasicSlicer.h>
56 const std::string i_namedescp ,
57 const std::string i_namedb)
58 : wxDialog(parent, -1,_T(
"DISPLAY IMAGES"), wxDefaultPosition, wxSize(230,150))
65 this->SetTitle(i_title);
68 wxButton *fileBut =
new wxButton(
this, -1,_T(
"Select a file to display"), wxPoint(10,7) );
72 wxButton *directoryBut =
new wxButton(
this, -1,_T(
"Select a directory to display"), wxPoint(10,40) );
76 wxButton *gimmickBut =
new wxButton(
this, -1,_T(
"Select Gimmick"), wxPoint(10,70) );
91 wxFileDialog* fileDlg =
new wxFileDialog( 0, _T(
"Select file"), _T(
""), _T(
""), crea::std2wx(
"*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
93 resultShowModal = fileDlg->ShowModal();
94 if ( resultShowModal==wxID_OK )
96 wxArrayString wxArray;
97 fileDlg->GetPaths(wxArray);
100 for(
int i = 0; i < wxArray.GetCount(); i++)
102 std::string name = crea::wx2std(wxArray[i]);
111 SetReturnCode( resultShowModal );
113 EndModal( resultShowModal );
128 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
129 wxDirDialog* dirDlg =
new wxDirDialog( 0, _T(
"Select the directory to display"), _T(
""), style);
131 resultShowModal = dirDlg->ShowModal();
132 if ( resultShowModal==wxID_OK )
134 std::string path = crea::wx2std(dirDlg->GetPath());
135 typedef boost::filesystem::directory_iterator dir_it;
140 for(;itr != end_itr; ++itr)
142 bvalid =
m_exts.size() == 0?
true :
false;
143 std::vector<std::string>::iterator it =
m_exts.begin();
144 std::string ext = itr->path().filename().string().substr(itr->path().filename().string().find_last_of(
"."));
145 for(; it !=
m_exts.end(); it++)
153 if (!boost::filesystem::is_directory(itr->status()) && bvalid)
155 readImg(itr->path().string().c_str());
159 SetReturnCode( resultShowModal );
161 EndModal( resultShowModal );
174 _T(
"Select image(s) - Gimmick! (c) CREATIS-LRMN 2008"),
182 if (dlg.GetReturnCode() == wxID_OK)
184 std::vector<std::string> out;
193 std::vector<std::string>::iterator ii = out.begin();
194 for (;ii != out.end();ii++)
201 SetReturnCode( dlg.GetReturnCode() );
203 EndModal( dlg.GetReturnCode() );
217 typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
218 itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode);
219 imageIO->SetFileName(i_name.c_str());
220 imageIO->ReadImageInformation();
221 return imageIO->GetComponentTypeInfo();
226 typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
227 itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode);
228 imageIO->SetFileName(i_name.c_str());
229 imageIO->ReadImageInformation();
230 return imageIO->GetNumberOfDimensions();
240 if (
getType(i_name) ==
typeid(
unsigned char))
242 typedef itk::Image<unsigned char, 3> TImage;
243 typedef itk::ImageFileReader<TImage> ReaderType;
244 ReaderType::Pointer reader = ReaderType::New();
245 reader->SetFileName( i_name );
247 reader->GetOutput()->Register();
250 else if (
getType(i_name) ==
typeid(
signed char))
252 typedef itk::Image<signed char, 3> TImage;
253 typedef itk::ImageFileReader<TImage> ReaderType;
254 ReaderType::Pointer reader = ReaderType::New();
255 reader->SetFileName( i_name );
257 reader->GetOutput()->Register();
260 else if (
getType(i_name) ==
typeid(short))
262 typedef itk::Image<short, 3> TImage;
263 typedef itk::ImageFileReader<TImage> ReaderType;
264 ReaderType::Pointer reader = ReaderType::New();
265 reader->SetFileName( i_name );
267 reader->GetOutput()->Register();
270 else if (
getType(i_name) ==
typeid(
unsigned short))
272 typedef itk::Image<unsigned short, 3> TImage;
273 typedef itk::ImageFileReader<TImage> ReaderType;
274 ReaderType::Pointer reader = ReaderType::New();
275 reader->SetFileName( i_name );
277 reader->GetOutput()->Register();
280 else if (
getType(i_name) ==
typeid(
unsigned int))
282 typedef itk::Image<unsigned int, 3> TImage;
283 typedef itk::ImageFileReader<TImage> ReaderType;
284 ReaderType::Pointer reader = ReaderType::New();
285 reader->SetFileName( i_name );
287 reader->GetOutput()->Register();
290 else if (
getType(i_name) ==
typeid(
signed int))
292 typedef itk::Image<signed int, 3> TImage;
293 typedef itk::ImageFileReader<TImage> ReaderType;
294 ReaderType::Pointer reader = ReaderType::New();
295 reader->SetFileName( i_name );
297 reader->GetOutput()->Register();
300 else if (
getType(i_name) ==
typeid(
unsigned long))
302 typedef itk::Image<unsigned long, 3> TImage;
303 typedef itk::ImageFileReader<TImage> ReaderType;
304 ReaderType::Pointer reader = ReaderType::New();
305 reader->SetFileName( i_name );
307 reader->GetOutput()->Register();
310 else if (
getType(i_name) ==
typeid(
signed long))
312 typedef itk::Image<signed long, 3> TImage;
313 typedef itk::ImageFileReader<TImage> ReaderType;
314 ReaderType::Pointer reader = ReaderType::New();
315 reader->SetFileName( i_name );
317 reader->GetOutput()->Register();
320 else if (
getType(i_name) ==
typeid(float))
322 typedef itk::Image<float, 3> TImage;
323 typedef itk::ImageFileReader<TImage> ReaderType;
324 ReaderType::Pointer reader = ReaderType::New();
325 reader->SetFileName( i_name );
327 reader->GetOutput()->Register();
330 else if (
getType(i_name) ==
typeid(double))
332 typedef itk::Image<double, 3> TImage;
333 typedef itk::ImageFileReader<TImage> ReaderType;
334 ReaderType::Pointer reader = ReaderType::New();
335 reader->SetFileName( i_name );
337 reader->GetOutput()->Register();
347 if (
getType(i_name) ==
typeid(
unsigned char))
349 typedef itk::Image<unsigned char, 4> TImage;
350 typedef itk::ImageFileReader<TImage> ReaderType;
351 ReaderType::Pointer reader = ReaderType::New();
352 reader->SetFileName( i_name );
354 reader->GetOutput()->Register();
356 split4Din3Dvtk<TImage>(reader->GetOutput());
358 else if (
getType(i_name) ==
typeid(
signed char))
360 typedef itk::Image<signed char, 4> TImage;
361 typedef itk::ImageFileReader<TImage> ReaderType;
362 ReaderType::Pointer reader = ReaderType::New();
363 reader->SetFileName( i_name );
365 reader->GetOutput()->Register();
367 split4Din3Dvtk<TImage>(reader->GetOutput());
369 else if (
getType(i_name) ==
typeid(
unsigned short))
371 typedef itk::Image<unsigned short, 4> TImage;
372 typedef itk::ImageFileReader<TImage> ReaderType;
373 ReaderType::Pointer reader = ReaderType::New();
374 reader->SetFileName( i_name );
376 reader->GetOutput()->Register();
378 split4Din3Dvtk<TImage>(reader->GetOutput());
380 else if(
getType(i_name) ==
typeid(short))
382 typedef itk::Image<short, 4> TImage;
383 typedef itk::ImageFileReader<TImage> ReaderType;
384 ReaderType::Pointer reader = ReaderType::New();
385 reader->SetFileName( i_name );
390 catch( itk::ExceptionObject & err )
392 std::cout <<
"Caught an exception reading" << i_name <<
": " << std::endl;
393 std::cout << err <<
" " << __FILE__ <<
" " << __LINE__ << std::endl;
398 std::cout <<
"Error while reading image " << i_name << std::endl;
401 reader->GetOutput()->Register();
403 split4Din3Dvtk<TImage>(reader->GetOutput());
405 else if (
getType(i_name) ==
typeid(
unsigned short))
407 typedef itk::Image<unsigned short, 4> TImage;
408 typedef itk::ImageFileReader<TImage> ReaderType;
409 ReaderType::Pointer reader = ReaderType::New();
410 reader->SetFileName( i_name );
412 reader->GetOutput()->Register();
414 split4Din3Dvtk<TImage>(reader->GetOutput());
416 else if (
getType(i_name) ==
typeid(
unsigned int))
418 typedef itk::Image<unsigned int, 4> TImage;
419 typedef itk::ImageFileReader<TImage> ReaderType;
420 ReaderType::Pointer reader = ReaderType::New();
421 reader->SetFileName( i_name );
423 reader->GetOutput()->Register();
425 split4Din3Dvtk<TImage>(reader->GetOutput());
427 else if (
getType(i_name) ==
typeid(
signed int))
429 typedef itk::Image<signed int, 4> TImage;
430 typedef itk::ImageFileReader<TImage> ReaderType;
431 ReaderType::Pointer reader = ReaderType::New();
432 reader->SetFileName( i_name );
434 reader->GetOutput()->Register();
436 split4Din3Dvtk<TImage>(reader->GetOutput());
438 else if (
getType(i_name) ==
typeid(
unsigned long))
440 typedef itk::Image<unsigned long, 4> TImage;
441 typedef itk::ImageFileReader<TImage> ReaderType;
442 ReaderType::Pointer reader = ReaderType::New();
443 reader->SetFileName( i_name );
445 reader->GetOutput()->Register();
447 split4Din3Dvtk<TImage>(reader->GetOutput());
449 else if (
getType(i_name) ==
typeid(
signed long))
451 typedef itk::Image<signed long, 4> TImage;
452 typedef itk::ImageFileReader<TImage> ReaderType;
453 ReaderType::Pointer reader = ReaderType::New();
454 reader->SetFileName( i_name );
456 reader->GetOutput()->Register();
458 split4Din3Dvtk<TImage>(reader->GetOutput());
460 else if (
getType(i_name) ==
typeid(float))
462 typedef itk::Image<float, 4> TImage;
463 typedef itk::ImageFileReader<TImage> ReaderType;
464 ReaderType::Pointer reader = ReaderType::New();
465 reader->SetFileName( i_name );
467 reader->GetOutput()->Register();
469 split4Din3Dvtk<TImage>(reader->GetOutput());
471 else if (
getType(i_name) ==
typeid(double))
473 typedef itk::Image<double, 4> TImage;
474 typedef itk::ImageFileReader<TImage> ReaderType;
475 ReaderType::Pointer reader = ReaderType::New();
476 reader->SetFileName( i_name );
478 reader->GetOutput()->Register();
480 split4Din3Dvtk<TImage>(reader->GetOutput());
514 typedef itk::Image<short,3> TImage;
515 typedef itk::GDCMImageIO GDCMType;
516 typedef itk::DICOMSeriesFileNames dicnames;
517 GDCMType::Pointer gdcmIO = GDCMType::New();
518 dicnames::Pointer generator = dicnames::New();
521 typedef itk::ImageSeriesReader<TImage> ReaderType;
522 ReaderType::Pointer reader = ReaderType::New();
523 reader->SetImageIO(gdcmIO);
524 reader->SetFileNames(i_names);
530 catch( itk::ExceptionObject & err )
533 std::cout << err <<
" " << __FILE__ <<
" " << __LINE__ << std::endl;
560 template <
typename TImage>
563 if(i_Img->GetImageDimension() == 4)
566 int dsize = i_Img->GetLargestPossibleRegion().GetSize(3);
568 typedef itk::Image<unsigned char,3> ImageOutputType;
569 typedef itk::ImageSeriesWriter<TImage, ImageOutputType > SeriesWriterType;
570 typedef itk::MetaImageIO MetaImageType;
571 MetaImageType::Pointer metaIO;
572 typename SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
575 typedef itk::NumericSeriesFileNames NamesGeneratorType;
576 NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
577 namesGenerator->SetStartIndex(0);
578 namesGenerator->SetEndIndex(dsize-1);
579 namesGenerator->SetIncrementIndex(1);
580 std::string format =
m_dir;
581 format +=
"/image%03d.mhd";
582 namesGenerator->SetSeriesFormat( format.c_str() );
584 const std::vector<std::string> names = namesGenerator->GetFileNames();
585 seriesWriter->SetFileNames( names );
586 seriesWriter->SetInput(i_Img);
587 seriesWriter->SetImageIO(metaIO);
590 seriesWriter->Update();
591 vtkMetaImageReader *vReader = vtkMetaImageReader::New();
592 std::vector<std::string>::const_iterator it = names.begin();
593 for( ;it != names.end(); ++it)
595 vReader->SetFileName((*it).c_str());
601 catch( itk::ExceptionObject & excp )
603 std::cerr <<
"Exception thrown while writing the series " << std::endl;
604 std::cerr << excp << std::endl;