void operator()( const Parameter ¶m ) { OSLM_DEBUG( "itk::ImageSeriesWriter with PIXELTYPE "<< fwTools::DynamicType::string<PIXELTYPE>() ); ::fwData::Image::sptr image = param.m_dataImage; // VAG attention : ImageFileReader ne notifie AUCUNE progressEvent mais son ImageIO oui!!!! mais ImageFileReader ne permet pas de l'atteindre // car soit mis a la mano ou alors construit lors de l'Update donc trop tard // Il faut dont creer une ImageIO a la mano (*1*): affecter l'observation sur IO (*2*) et mettre le IO dans le reader (voir *3*) // Reader IO (*1*) typename itk::ImageIOBase::Pointer imageIOWrite = itk::ImageIOFactory::CreateImageIO( "image.jpg", itk::ImageIOFactory::WriteMode); assert( imageIOWrite.IsNotNull() ); // create writer typedef itk::Image< PIXELTYPE, 3> itkImageType; typedef itk::Image< unsigned char, 2 > Image2DType; typedef typename itk::ImageSeriesWriter< itkImageType, Image2DType > WriterType; typename WriterType::Pointer writer = WriterType::New(); // set observation (*2*) itk::LightProcessObject::Pointer castHelper= (itk::LightProcessObject *)(imageIOWrite.GetPointer()); assert( castHelper.IsNotNull() ); Progressor progress(castHelper, param.m_fwWriter, param.m_filename); // create itk Image typename itkImageType::Pointer itkImage = ::fwItkIO::itkImageFactory<itkImageType>( image ); typedef ::itk::IntensityWindowingImageFilter< itkImageType, itkImageType > RescaleFilterType; typename RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New(); double min, max; ::fwData::Composite::sptr poolTF; poolTF = image->getField< ::fwData::Composite>( ::fwComEd::Dictionary::m_transferFunctionCompositeId ); if(poolTF) { ::fwData::Composite::iterator iter = poolTF->find(::fwData::TransferFunction::s_DEFAULT_TF_NAME); if(iter != poolTF->end()) { ::fwData::TransferFunction::sptr tf; tf = ::fwData::TransferFunction::dynamicCast(iter->second); min = tf->getWLMinMax().first; max = tf->getWLMinMax().second; } } else { ::fwComEd::fieldHelper::MedicalImageHelpers::getMinMax(image, min, max); } rescaleFilter->SetWindowMinimum( min ); rescaleFilter->SetWindowMaximum( max ); rescaleFilter->SetOutputMinimum( 0 ); rescaleFilter->SetOutputMaximum( 255 ); rescaleFilter->InPlaceOff(); rescaleFilter->SetInput( itkImage ); rescaleFilter->Update(); writer->SetInput( rescaleFilter->GetOutput() ); typedef itk::NumericSeriesFileNames NameGeneratorType; NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New(); std::string format = param.m_filename; format += "/%04d.jpg"; nameGenerator->SetSeriesFormat( format.c_str() ); nameGenerator->SetStartIndex( 1 ); nameGenerator->SetEndIndex( image->getSize()[2] ); nameGenerator->SetIncrementIndex( 1 ); writer->SetFileNames( nameGenerator->GetFileNames() ); writer->SetImageIO( imageIOWrite ); // save image; writer->Update(); }
/** * draws the tree from the TreeDrawer into a volumetric 3D * image as a series of 2D png slices */ void drawImage(TreeDrawer * td, const char* rootName){ typedef unsigned char PixelType; const unsigned int Dimension = 3; typedef itk::Image< PixelType, Dimension > ImageType; ImageType::Pointer image = ImageType::New(); ImageType::SizeType size; size[0] = td->dim[0]; // size along X size[1] = td->dim[1]; // size along Y size[2] = td->dim[2]; // size along Z ImageType::IndexType start; start[0] = 0; // first index on X start[1] = 0; // first index on Y start[2] = 0; // first index on Z ImageType::RegionType region; region.SetSize( size ); region.SetIndex( start ); image->SetRegions( region ); image->Allocate(); ImageType::IndexType pixelIndex; pixelIndex[0] = 0; // x position pixelIndex[1] = 0; // y position pixelIndex[2] = 0; // z position for(int i = 0; i < td->dim[0]; i++){ for(int j = 0; j < td->dim[1]; j++){ for(int k = 0 ; k < td->dim[2]; k++){ pixelIndex[0] = i; pixelIndex[1] = j; pixelIndex[2] = k; image->SetPixel(pixelIndex, td->imageAt(i, j, k)); } } } typedef itk::Image< unsigned char, 2 > Image2DType; typedef itk::ImageSeriesWriter< ImageType, Image2DType > WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetInput( image); typedef itk::NumericSeriesFileNames NameGeneratorType; NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New(); std::string format = rootName; format += "%03d"; format += ".jpg"; nameGenerator->SetSeriesFormat( format.c_str() ); const unsigned int firstSlice = start[2]; const unsigned int lastSlice = start[2] + size[2] - 1; nameGenerator->SetStartIndex( firstSlice ); nameGenerator->SetEndIndex( lastSlice ); nameGenerator->SetIncrementIndex( 1 ); writer->SetFileNames( nameGenerator->GetFileNames() ); try{ writer->Update(); }catch( itk::ExceptionObject & excp ){ throw "Exception thrown while reading the image"; } return; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // new for itk 4.8 itk::JPEGImageIOFactory::RegisterOneFactory(); // init m_pInvertIntensityFilter = InvertIntensityFilterType::New(); m_pConnector = FilterType::New(); ////////////////////////// // vtk objects // QT/VTK interact m_pRenderer = vtkRenderer::New(); // ui.vtkWidget->GetRenderWindow()->AddRenderer(m_pRenderer); m_pRenderWindow = vtkRenderWindow::New(); m_pRenderWindow->AddRenderer(m_pRenderer); m_pRenderWindowInteractor = vtkRenderWindowInteractor::New(); m_pRenderWindowInteractor->SetRenderWindow(m_pRenderWindow); m_pTissueExtractor = vtkMarchingCubes::New(); //vtkContourFilter::New(); m_pTissueNormals = vtkPolyDataNormals::New(); m_pTissueMapper = vtkPolyDataMapper::New(); m_pTissue = vtkActor::New(); m_pTextActor = vtkActor2D::New(); m_pTextMapper = vtkTextMapper::New(); // read a volume // QString strVolumeIndex; //strVolumeIndex.sprintf("../", nZoomOut, // iWidth, iLength, iHeight); QString strSeriesFormat; strSeriesFormat = QString("K:/Projects/TestVol3/%04d.jpg"); //qDebug() << strSeriesFormat; NameGeneratorType::Pointer pNameGenerator = NameGeneratorType::New(); pNameGenerator->SetSeriesFormat(strSeriesFormat.toStdString()); unsigned int nStartIndex = 1; pNameGenerator->SetStartIndex(nStartIndex); //0 ); pNameGenerator->SetEndIndex(256); pNameGenerator->SetIncrementIndex( 2 ); /////////// // optional //pReader->SetImageIO(itk::JPEGImageIO::New()); ReaderType::Pointer pReader = ReaderType::New(); pReader->SetFileNames(pNameGenerator->GetFileNames()); try { pReader->Update(); } catch (itk::ExceptionObject & err ) { std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl; return EXIT_FAILURE; } m_pImageData = pReader->GetOutput(); // m_pImageData->DisconnectPipeline(); // execute pipeline m_pInvertIntensityFilter->SetInput(m_pImageData); m_pConnector->SetInput(m_pInvertIntensityFilter->GetOutput()); //m_pReader->GetOutput()); #if VTK_MAJOR_VERSION <= 5 m_pTissueExtractor->SetInput(m_pConnector->GetOutput()); #else m_pConnector->Update(); m_pTissueExtractor->SetInputData(m_pConnector->GetOutput()); #endif //int nIsoSurface = ui.lineEditValue->text().toInt(); //if(nIsoSurface == 0) int nIsoSurface = 155; m_pTissueExtractor->SetValue(0, nIsoSurface); #if VTK_MAJOR_VERSION <= 5 m_pTissueNormals->SetInput(m_pTissueExtractor->GetOutput()); #else m_pTissueNormals->SetInputConnection(m_pTissueExtractor->GetOutputPort()); #endif //m_pTissueNormals->SetFeatureAngle(60.0); #if VTK_MAJOR_VERSION <= 5 m_pTissueMapper->SetInput(m_pTissueNormals->GetOutput()); #else m_pTissueMapper->SetInputConnection(m_pTissueNormals->GetOutputPort()); #endif m_pTissueMapper->ScalarVisibilityOff(); m_pTissue->SetMapper(m_pTissueMapper); //if(!m_pVolumeMapper) //{ // m_pVolumeMapper = vtkFixedPointVolumeRayCastMapper::New(); // m_pVolumeMapper->SetInput(m_pConnector->GetOutput()); // m_pVolume = vtkVolume::New(); // m_pVolume->SetMapper(m_pVolumeMapper); // m_pVolume->Update(); // //m_pRenderer->AddActor(m_pVolume); m_pRenderer->AddActor(m_pTissue); m_pRenderer->SetBackground(0.0,0.0,0.4); m_pRenderer->ResetCamera(); m_pRenderer->GetRenderWindow()->Render(); m_pRenderWindowInteractor->Start(); // save to vtk vtkPolyDataWriter* pWriter = vtkPolyDataWriter::New(); #if VTK_MAJOR_VERSION <= 5 pWriter->SetInput(m_pTissueNormals->GetOutput()); #else pWriter->SetInputConnection(m_pTissueNormals->GetOutputPort()); #endif QString strVTKFileName; strVTKFileName.sprintf("test2.vtk"); pWriter->SetFileName(strVTKFileName.toStdString().c_str()); pWriter->SetFileTypeToBinary(); pWriter->Write(); // save to stl vtkSTLWriter* pSTLWriter = vtkSTLWriter::New(); #if VTK_MAJOR_VERSION <= 5 pSTLWriter->SetInput(m_pTissueNormals->GetOutput()); #else pSTLWriter->SetInputConnection(m_pTissueNormals->GetOutputPort()); #endif QString strSTLFileName; strSTLFileName.sprintf("test2.stl"); pSTLWriter->SetFileName(strSTLFileName.toStdString().c_str()); pSTLWriter->SetFileTypeToBinary(); pSTLWriter->Write(); // finalize m_pRenderer->Delete(); m_pTissueExtractor->Delete(); m_pTissueNormals->Delete(); m_pTissueMapper->Delete(); m_pTissue->Delete(); m_pTextActor->Delete();; m_pTextMapper->Delete(); pReader->Delete(); pWriter->Delete(); pSTLWriter->Delete(); // TODO: there are missing pointers to be deleted. Add them. return a.exec(); }