[Insight-users] Re: image file location : Reading PNG series
Luis Ibanez
luis.ibanez at kitware.com
Fri, 23 Jan 2004 10:09:38 -0500
This is a multi-part message in MIME format.
--------------030505090407090409010506
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Hi Sharad,
You could have saved a lot of time by just
using the IO example in
Insight/Examples/IO/
ImageSeriesReadWrite.cxx
which does exactly what you need:
Read a series of PNG files and save
them in another ITK supported format.
----
The problems in your code are related
to the inapropriate construction of the
format tha the ImageSeriesReader need.
This format is supposes to be a "printf"
style format. For your images:
myI001.png
myI002.png
myI003.png
...
The format should be
"myI%03d.png"
that can easily be constructed as:
std::string format = argv[3];
format = format + "%03d.png";
Also the images that you sent have
double extension .png, like
myI001.png.png
You probably are doing this in MS
windows as enabled the option of
not showing extentions. You may
want to disable this option in order
to preseve sanity for a longer time.
Attached is the working file.
----------
Some comments about your code:
1) Instead of
(int) atof( argv[2] );
you should use
atoi( argv[2] );
2) Use typedef instead of repeating the
instantiation of ITK templates.
like in the ImageFileWriter and
ImageSeriesReader declarations.
Regards,
Luis
-----------------
sharad at lynx.eng.fsu.edu wrote:
> hi luis
>
> can you help me with this
> i am trying to run this code for stacking 2D png images
> and everything is running fine except for when i compile it it does not
> take the png images it gives an error saying "debug error" and abnormal
> program termination- abort retry ignore
>
> i keep the png images in the debug directory and they are of the same size
> where exactly should the png images be present
> i am using c++ 6.0 on win xp and am passing command line arguments from
> projects/settings
> can you tell me what is the problem
> thanks a lot
> sharad
>
> the code is as follows and cmake file attached at end
> #################
>
> #include "itkNumericSeriesFileNames.h"
> #include "itkImageSeriesReader.h"
> #include "itkImageFileWriter.h"
> #include "itkImage.h"
> #include <vector>
> #include <string>
> #include <stdio.h>
>
> int main(int argc, char * argv[])
> {
> std::cout << argv[1] << std::endl;
> typedef itk::Image<unsigned char,3> Image3DType;
>
> // Verify the number of parameters in the command line
> if( argc < 4 )
> {
> std::cerr << "Usage: " << std::endl;
> std::cerr << argv[0] << "StartIndex EndIndex inputImageFileBaseName
> outputImageFile" << std::endl;
> std::cerr << " Example: StartIndex = 1 , EndIndex =
> 100,inputImageFileBaseName = myI00%d.png" << std::endl;
> std::cerr << " will read myI001.png, ..., myI100.png. 100 png files"
> << std::endl;
> std::cerr << " if outputImageFile = my.mha then it will output a file
> my.mha which is in meta format" << std::endl;
> std::cerr << " if outputImageFile = my.gipl then it will output a
> file my.gipl which is in gipl format" << std::endl;
> std::cerr << " ./readPngWrite3DGiplNew 0 1 myImages00%d.png outp.mha
> " << std::endl;
> std::cerr << " will read in myImages000.png and myImages001.png,
> output outp.mha in meta format " << std::endl;
> return -1;
> }
>
> const int StartIndex = (int) atof(argv[1]);
> const int EndIndex = (int) atof(argv[2]);
> const char * inImFileBase = argv[3];
> const char * ouImFileName = argv[4];
>
>
>
> //std::cout << argv[4] << std::endl;
>
> //
> // Here we generate filenames like
> // myImage000.png, myImage001.png .... to myImage200.png
> // The most important thing is that the serious must be
> // same size!!!!!!!!!!!!!!
> //
> itk::NumericSeriesFileNames::Pointer fileIter =
> itk::NumericSeriesFileNames::New();
>
> fileIter->SetStartIndex( StartIndex );
> fileIter->SetEndIndex( EndIndex );
> fileIter->SetIncrementIndex( 1 );
> //const char * inputFileNameBase = ( const char * ) strcat( (char *)
> //inImFileBase, (char *) "00%d.png");
> //std::cout << inputFileNameBase << std::endl;
> //std::cout << argv[3] << std::endl;
> //std::cout << argv[4] << std::endl;
> //fileIter->SetSeriesFormat("myImages00%d.png");
>
> fileIter->SetSeriesFormat(inImFileBase);
>
> itk::ImageSeriesReader<Image3DType>::Pointer reader =
> itk::ImageSeriesReader<Image3DType>::New();
> reader->SetFileNames( fileIter->GetFileNames() );
>
> reader->Update();
>
>
> //
> // Here we save the image in GIPL format
> // or any other forms which itk understand!
> // The user only need to give the correct
> // file extention !
> //
> itk::ImageFileWriter< Image3DType >::Pointer writer =
> itk::ImageFileWriter< Image3DType >::New();
>
> writer->SetInput( reader->GetOutput() );
> //writer->SetFileName("myImage.mha");
> //return 0;
>
> writer->SetFileName(ouImFileName);
> try
> {
> writer->Update();
> }
> catch( itk::ExceptionObject & err )
> {
> std::cout << "ExceptionObject caught !" << std::endl;
> std::cout << err << std::endl;
> return -1;
> }
>
>
> return 0;
> }
>
> ###########
>
> and cmake file is
> ########################
> # This project is designed to be built outside the Insight source tree.
> PROJECT(3Ddeconvolution)
>
> # Find ITK.
> FIND_PACKAGE(ITK)
> IF(ITK_FOUND)
> INCLUDE(${ITK_USE_FILE})
> ELSE(ITK_FOUND)
> MESSAGE(FATAL_ERROR
> "Cannot build without ITK. Please set ITK_DIR.")
> ENDIF(ITK_FOUND)
>
> ADD_EXECUTABLE(3Ddeconvolution 3Ddeconvolution.cxx )
>
>
> TARGET_LINK_LIBRARIES( 3Ddeconvolution
> ITKAlgorithms
> ITKBasicFilters
> ITKNumerics
> ITKIO
> ITKCommon
> )
>
>
>
>
--------------030505090407090409010506
Content-Type: text/plain;
name="readingFiles.cxx"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="readingFiles.cxx"
#include "itkNumericSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include <vector>
#include <string>
#include <stdio.h>
int main(int argc, char * argv[])
{
typedef itk::Image<unsigned char,3> Image3DType;
// Verify the number of parameters in the command line
if( argc < 4 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << "StartIndex EndIndex inputImageFileBaseName outputImageFile" << std::endl;
std::cerr << " Example: StartIndex = 1 , EndIndex = 100,inputImageFileBaseName = myI00%d.png" << std::endl;
std::cerr << " will read myI001.png, ..., myI100.png. 100 png files" << std::endl;
std::cerr << " if outputImageFile = my.mha then it will output a file my.mha which is in meta format" << std::endl;
std::cerr << " if outputImageFile = my.gipl then it will output a file my.gipl which is in gipl format" << std::endl;
std::cerr << " ./readPngWrite3DGiplNew 0 1 myImages00%d.png outp.mha " << std::endl;
std::cerr << " will read in myImages000.png and myImages001.png, output outp.mha in meta format " << std::endl;
return -1;
}
const int StartIndex = atoi( argv[1] );
const int EndIndex = atoi( argv[2] );
itk::NumericSeriesFileNames::Pointer fileIter =
itk::NumericSeriesFileNames::New();
fileIter->SetStartIndex( StartIndex );
fileIter->SetEndIndex( EndIndex );
fileIter->SetIncrementIndex( 1 );
std::string format = argv[3];
format = format + "%03d.png";
fileIter->SetSeriesFormat( format.c_str() );
typedef itk::ImageSeriesReader<Image3DType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileNames( fileIter->GetFileNames() );
reader->Update();
//
// Here we save the image in GIPL format
// or any other forms which itk understand!
// The user only need to give the correct
// file extention !
//
typedef itk::ImageFileWriter< Image3DType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetInput( reader->GetOutput() );
writer->SetFileName( argv[4] );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
return 0;
}
--------------030505090407090409010506--