[Insight-users] using ImageRegionIterator will change the image value?

Dongfeng Han handongfeng at gmail.com
Fri Jan 23 00:43:23 EST 2009


hello Luis
thank you for your reply.
I have checked the image values, they are same.
But when using ImageJ to visusize, them are different by eye.
If ImageJ has some bugs? or may be i have somthings wrong with it.


here I post my code, hope it is useful.
the function conncect 3 scans to one scan. begin with scan3
(filename3)and then scan2 (filename2)and then scan1(filename1).
I hope it will be useful.
It works though there may be some error:) I did not make comments now
.sorry for that.

int   ConnectScans( char* filename1, char* filename2, char* filename3,
char* outputfilename, void * pimg)// pimg will return the result image
pointer
{

	// Software Guide : BeginCodeSnippet
	typedef signed short        InputPixelType;
	typedef signed short        OutputPixelType;
	const   unsigned int        Dimension = 3;

	typedef itk::Image< InputPixelType,  Dimension >    InputImageType;
	typedef itk::Image< OutputPixelType, Dimension >    OutputImageType;
	// Software Guide : EndCodeSnippet

   // Software Guide : BeginCodeSnippet
	typedef itk::ImageFileReader< InputImageType  >  ReaderType;
	typedef itk::ImageFileWriter< OutputImageType >  WriterType;

	ReaderType::Pointer reader1 = ReaderType::New();
	ReaderType::Pointer reader2 = ReaderType::New();
	ReaderType::Pointer reader3 = ReaderType::New();

	WriterType::Pointer writer = WriterType::New();
	// Software Guide : EndCodeSnippet
	//
	// Here we recover the file names from the command line arguments
	//
	//const char * inputFilename  = argv[1];
	const char * outputFilename = outputfilename;


	reader1->SetFileName( filename1  );
	reader2->SetFileName( filename2  );
	reader3->SetFileName( filename3);

	reader1->Update();
	reader2->Update();
	reader3->Update();

	writer->SetFileName( outputFilename );

	// OutputImageType::IndexType start;
	// start[0] = 0;
	// start[1] = 0;
	// start[2] = 0;
	OutputImageType::IndexType start1;
	OutputImageType::IndexType start2;
	OutputImageType::IndexType start3;

	OutputImageType::SizeType size;
	OutputImageType::SizeType size1;
	OutputImageType::SizeType size2;
	OutputImageType::SizeType size3;

	size1 =  reader1->GetOutput()->GetLargestPossibleRegion().GetSize();
	size2 =  reader2->GetOutput()->GetLargestPossibleRegion().GetSize();
	size3 =  reader3->GetOutput()->GetLargestPossibleRegion().GetSize();
	
	size[0] =size1[0];
	size[1] =size1[1];
	size[2] =size1[2]+size2[2]+size3[2];
	std::cout<<size<<std::endl;
	std::cout<<size1<<std::endl;
	std::cout<<size2<<std::endl;
	std::cout<<size3<<std::endl;


	start1[0] = 0;
	start1[1] = 0;
	start1[2] = size3[2]+size2[2];

	printf("start 1 is %d\n", start1[2]);

	start2[0] = 0;
	start2[1] = 0;
	start2[2] = size3[2];
	printf("start 2 is %d\n", start2[2]);

	start3[0] = 0;
	start3[1] = 0;
	start3[2] = 0;
	printf("start 3 is %d\n", start3[2]);


   typedef itk::RegionOfInterestImageFilter< InputImageType,
		OutputImageType > FilterType;

	FilterType::Pointer filter = FilterType::New();

	OutputImageType::RegionType desiredRegion3;
	desiredRegion3.SetSize(  size3  );
	desiredRegion3.SetIndex( start3 );
	//filter->SetRegionOfInterest( desiredRegion3 );
	//filter->SetInput( reader3->GetOutput() );
	//writer->SetInput( filter->GetOutput() );

	printf("111111\n");
	OutputImageType::RegionType desiredRegion2;
	desiredRegion2.SetSize(  size2  );
	desiredRegion2.SetIndex( start3 );
	//filter->SetRegionOfInterest( desiredRegion2 );
	//filter->SetInput( reader2->GetOutput() );
   // writer->SetInput( filter->GetOutput() );
	
	OutputImageType::RegionType desiredRegion1;
	desiredRegion1.SetSize(  size1  );
	desiredRegion1.SetIndex( start3 );
	//filter->SetRegionOfInterest( desiredRegion1 );
	//filter->SetInput( reader1->GetOutput() );
    //writer->SetInput( filter->GetOutput() );

	//  Finally we execute the pipeline by invoking Update() on the writer. The
	//  call is placed in a \code{try/catch} block in case exceptions are
	//  thrown.
	//
	//  Software Guide : EndLatex

	// Software Guide : BeginCodeSnippet

	/*************************************************************/
	typedef itk::ImageRegionConstIterator< OutputImageType > ConstIteratorType;
	typedef itk::ImageRegionIterator< OutputImageType>       IteratorType;
	OutputImageType::RegionType desiredRegion;
	desiredRegion.SetSize(  size  );
	desiredRegion.SetIndex( start3 );

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

   OutputImageType::Pointer *kimg = (OutputImageType::Pointer *)pimg;
     *kimg = outputImage;

    outputImage->SetRegions( desiredRegion );
	const OutputImageType::SpacingType& spacing =
reader3->GetOutput()->GetSpacing();
	const OutputImageType::PointType& inputOrigin =
reader3->GetOutput()->GetOrigin();
	double   outputOrigin[ Dimension ];

	for(unsigned int i=0; i< Dimension; i++)
	{
		outputOrigin[i] =inputOrigin[i] + spacing[i] * start3[i];
	}

	std::cout<<"outputOrigin "<<outputOrigin<<std::endl;
	outputImage->SetSpacing( spacing );
	outputImage->SetOrigin(  outputOrigin );
	outputImage->Allocate();


	ConstIteratorType inputIt3(   reader3->GetOutput(), desiredRegion3  );
	IteratorType      outputIt3(  outputImage,         desiredRegion );

	for ( inputIt3.GoToBegin(), outputIt3.GoToBegin(); !inputIt3.IsAtEnd();
		++inputIt3, ++outputIt3)
	{
		outputIt3.Set(  inputIt3.Get()  );
	}

    desiredRegion.SetIndex( start2 );
     ConstIteratorType inputIt2(   reader2->GetOutput(), desiredRegion2  );
	IteratorType      outputIt2(  outputImage,         desiredRegion );
	
	

	for ( inputIt2.GoToBegin(), outputIt2.GoToBegin(); !inputIt2.IsAtEnd();
		++inputIt2, ++outputIt2)
	{
		outputIt2.Set(  inputIt2.Get()  );
	}



	 desiredRegion.SetIndex( start1 );
	 ConstIteratorType inputIt1(   reader1->GetOutput(), desiredRegion1  );
	 IteratorType      outputIt1(  outputImage,         desiredRegion );
	
	

	for ( inputIt1.GoToBegin(), outputIt1.GoToBegin(); !inputIt1.IsAtEnd();
		++inputIt1, ++outputIt1)
	{
		outputIt1.Set(  inputIt1.Get()  );
	}
	/************************************************************/

    writer->SetInput( outputImage );





	try
	{
		writer->Update();
	}
	catch( itk::ExceptionObject & err )
	{
		std::cerr << "ExceptionObject caught333 !" << std::endl;
		std::cerr << err << std::endl;
		return EXIT_FAILURE;
	
	}
	// Software Guide : EndCodeSnippet

	
	return EXIT_FAILURE;
}


More information about the Insight-users mailing list