[Insight-users] Question regarding use of iterator

Miller, James V (Research) millerjv at crd.ge.com
Tue Jul 6 13:03:15 EDT 2004


Jian, 

You need to allocate the memory to use for the "output image".  You create
an output image with 

	ImageType::Pointer outputImage = ImageType::New();

but you have to allocate the "pixels".

output->SetRegions( image->GetBufferedRegion() );
output->Allocate();

prior to walking the image with the iterator.

Jim

-----Original Message-----
From: Jian XIE [mailto:jxie at cs.mu.OZ.AU]
Sent: Tuesday, July 06, 2004 1:28 AM
To: insight-users at itk.org
Subject: [Insight-users] Question regarding use of iterator


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
_______________________________________________
Insight-users mailing list
Insight-users at itk.org
http://www.itk.org/mailman/listinfo/insight-users


More information about the Insight-users mailing list