[Insight-users] Question regarding use of iterator
Jian XIE
jxie at cs.mu.OZ.AU
Tue Jul 6 01:28:00 EDT 2004
Dear ITK user, I try to write a small testing program such that this
program iterate through a 2D image(jpg file), through the use of itk
iterator, it then copy the values pixel by pixel into a new 2D image(jpg
file). here is my code fragment
//////////////////
// usual codes
typedef unsigned char PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
const char * inputFilename = argv[1];
const char * outputFilename = argv[2];
reader->SetFileName( inputFilename );
// read in the image into memory
reader->Update();
// use iterator here
typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
typedef itk::ImageRegionIterator< ImageType> IteratorType;
ImageType::Pointer outputImage = ImageType::New();
ImageType::Pointer image = ImageType::New();
image = reader->GetOutput();
// create 2 iterator, inputIt to read in pixels while outputIt
// write pixel that read in. May be the concept here is wrong???
ConstIteratorType inputIt( reader->GetOutput(), reader->GetOutput()->GetRequestedRegion() );
IteratorType outputIt( outputImage, outputImage->GetRequestedRegion() );
for ( inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt) {
// get a bus error when execute this line
outputIt.Set( inputIt.Get() );
}
writer->SetFileName( outputFilename );
writer->SetInput( outputImage );
writer->Update();
I always get bus error when execute the for loop to iterate through the
image. The tutorial on iterator didn't actually cover copying values pixel by pixel
through iterator. Is there any good resource on ITK other than the
ITKSoftwareGuide?
Thanks in advance for any help!!
Jian
BCS (Honours)
CSSE University Of Melbourne
More information about the Insight-users
mailing list