[Insight-users] Problem when using seriesreader together with series writer
Wen Shi
wsa18 at sfu.ca
Mon Apr 13 22:13:54 EDT 2009
Hi John,
Thanks for your reply.
But your opinion is not correct since inputimage is an instance of class image, it's not a filter, so you can't use setinput to initialize it.
Actually What I am doing is using VED(Vesselness Enhancement Diffusion) filter to process data.
But VED can only deal with 3D data. So I use seriesreader to read a serie of 2D slice images to form a 3D data set, Then use VED to process the data set.
Finally I want to write these 3D data back to 2D slices for visualization.
That's why I mentioned I use seriesreader together with serieswriter. (I don't have the 3D image which is in just one file such as RAW image data).
The whole code is here:
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkMultiScaleHessianSmoothed3DToVesselnessMeasureImageFilterTest.cxx,v $
Language: C++
Date: $Date: 2007/04/01 21:19:46 $
Version: $Revision: 1.5 $
Copyright (c) Insight Software 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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkMultiScaleHessianSmoothed3DToVesselnessMeasureImageFilter.h"
//#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesWriter.h"
#include "itkImageSeriesReader.h"
#include "itkNumericSeriesFileNames.h"
#include "itkBMPImageIO.h"
#include "itkRescaleIntensityImageFilter.h"
int main(int argc, char* argv [] )
{
argc=4;
if ( argc < 3 )
{
std::cerr << "Missing Parameters: "
<< argv[0]
<< " Input_Image"
<< " Vessel_Enhanced_Output_Image [SigmaMin SigmaMax NumberOfScales]" << std::endl;
return EXIT_FAILURE;
}
// Define the dimension of the images
const unsigned int Dimension = 3;
typedef short InputPixelType;
typedef double OutputVesselnessPixelType;
// Declare the types of the images
typedef itk::Image< InputPixelType, Dimension> InputImageType;
typedef itk::Image<unsigned char, 3> OutputImageType;
typedef itk::Image<unsigned char, 2> ImageType;
typedef itk::Image< OutputVesselnessPixelType, Dimension> VesselnessOutputImageType;
typedef itk::ImageSeriesReader< InputImageType > ReaderType;
//typedef itk::ImageFileWriter< OutputImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
// WriterType::Pointer writer = WriterType::New();
const unsigned int first =001;
const unsigned int last =005;
typedef itk::NumericSeriesFileNames NameGeneratorType;
NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
nameGenerator->SetSeriesFormat( "vessel-%03d.bmp" );
nameGenerator->SetStartIndex( first );
nameGenerator->SetEndIndex( last );
nameGenerator->SetIncrementIndex( 1 );
reader->SetImageIO( itk::BMPImageIO::New() );
reader->SetFileNames(nameGenerator->GetFileNames() );
//reader->SetFileName ( "CT-Head.pvm" );
/*typedef itk::ImageFileReader< InputImageType > ImageReaderType;
ImageReaderType::Pointer reader = ImageReaderType::New();
reader->SetFileName( "vessel-001.bmp" );*/
std::cout << "Reading input image : " << "vessel-.bmp" << std::endl;
try
{
reader->Update();
}
catch ( itk::ExceptionObject &err )
{
std::cerr << "Exception thrown: " << err << std::endl;
return EXIT_FAILURE;
}
// Declare the type of multiscale vesselness filter
typedef itk::MultiScaleHessianSmoothed3DToVesselnessMeasureImageFilter<
InputImageType,
VesselnessOutputImageType>
MultiScaleVesselnessFilterType;
// Create a vesselness Filter
MultiScaleVesselnessFilterType::Pointer MultiScaleVesselnessFilter =
MultiScaleVesselnessFilterType::New();
MultiScaleVesselnessFilter->SetInput( reader->GetOutput() );
// if ( argc >= 4 )
// {
MultiScaleVesselnessFilter->SetSigmaMin( 0.5 );
// }
//if ( argc >= 5 )
// {
MultiScaleVesselnessFilter->SetSigmaMax( 1.0 );
// }
// if ( argc >= 6 )
// {
MultiScaleVesselnessFilter->SetNumberOfSigmaSteps( 5 );
// }
try
{
MultiScaleVesselnessFilter->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "Exception caught: " << err << std::endl;
return EXIT_FAILURE;
}
std::cout << "Writing out the enhanced image to " << "vessel(enhanced).png" << std::endl;
//Rescale the output of the vesslness image
typedef itk::RescaleIntensityImageFilter< VesselnessOutputImageType,
OutputImageType>
RescaleFilterType;
RescaleFilterType::Pointer rescale = RescaleFilterType::New();
rescale->SetInput( MultiScaleVesselnessFilter->GetOutput() );
rescale->SetOutputMinimum( 0 );
rescale->SetOutputMaximum( 255 );
try
{
rescale->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "Exception caught: " << err << std::endl;
return EXIT_FAILURE;
}
typedef itk::ImageSeriesWriter< OutputImageType, OutputImageType > ImageWriterType;
ImageWriterType::Pointer writer = ImageWriterType::New();
typedef itk::NumericSeriesFileNames NameGeneratorType;
NameGeneratorType::Pointer nameGenerator1 = NameGeneratorType::New();
std::string format = "output";
format += "%03d.";
format += "png"; // filename extension
nameGenerator1->SetSeriesFormat( format.c_str() );
// writer->SetFileName( "vessel(enhanced).png" );
// writer->SetInput ( rescale->GetOutput() );
InputImageType::ConstPointer inputImage = rescale->GetOutput();
/* InputImageType::Pointer inputImage = InputImageType::New();//modified based on John's opinion
inputImage->SetInput(rescale->GetOutput());*/
InputImageType::RegionType region = inputImage->GetLargestPossibleRegion();
InputImageType::IndexType start = region.GetIndex();
InputImageType::SizeType size = region.GetSize();
const unsigned int firstSlice = start[2];
const unsigned int lastSlice = start[2] + size[2] - 1;
nameGenerator1->SetStartIndex( firstSlice );
nameGenerator1->SetEndIndex( lastSlice );
nameGenerator1->SetIncrementIndex( 1 );
writer->SetFileNames( nameGenerator1->GetFileNames() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "Exception caught: " << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Thank you very much John!!!
Wen Shi
More information about the Insight-users
mailing list