[Insight-users] crop and flip images

Luis Ibanez luis . ibanez at kitware . com
Thu, 27 Nov 2003 20:14:02 -0500


Hi Stefan,

Your program fine works for me.

I tried using the image

   Insight/Data/Examples/BrainProtonDensitySlice.png

and setting the parameters of the region to

   const int xStart=60,yStart=80,xExtent=100,yExtent=100;

and uncommenting line 128

   flipAxesSet[1]=true;

The output image is as expected, the vertical flip
of the region starting at (60,80) and with size (100,100).
Your program is extracting the region and flipping it
as expected.


Can you describe in more detail what you find to be
incorrect in this output ?

Thanks


   Luis.



----------------------------------

Stefan Lindenau wrote:

> Hi all,
> 
> I have a little problem while processing my images.
> When I use RegionOfInterestImageFilter and FlipImageFilter one after the 
> other in the pipeline, the result is not the same image just flipped 
> along the specified axis.
> 
> Is this the right way? I have my sourcecode attached. So you can  see 
> how I set up the pipeline and the region of interest.
> 
> Thank you
> stefan
> 
> 
> ------------------------------------------------------------------------
> 
> 
> #include "itkRGBPixel.h"
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkMetaImageIO.h"
> #include "itkImageIOBase.h"
> #include "itkRegionOfInterestImageFilter.h"
> #include "itkFlipImageFilter.h"
> 
> 
> 
> 
> int main(int argc, char ** argv) {
>    
> 	//These parameters are hardcoded. This program is only for helping purposes.
> 	const int xStart=648,yStart=0,xExtent=464,yExtent=672;
> 
> 	std::cout << "This program resizes the input file with the following parameters"<<std::endl;
> 	std::cout << "x starting value: " << xStart<<std::endl;
> 	std::cout << "y starting value: " << yStart<<std::endl;
> 	std::cout << "x extension     : " << xExtent << std::endl;
> 	std::cout << "y extension     : " << yExtent << std::endl;
> 	std::cout << "==========================="<<std::endl<<std::endl;
> 
> 
> 	if(argc!=3) {
> 		std::cerr<<"Invalid command line options provided!"<<std::endl;
> 		std::cerr<<"Pls use the program in the following manner:"<<std::endl<<std::endl;
> 		std::cerr<<argv[0]<<" inputFile outputFile"<<std::endl;
> 		return -1;
> 	}
>     
> 	/*
> 	Typedefs to use the ITK in a fashionable manner
> 	
> 	*/
> 	typedef itk::RGBPixel<unsigned char>    PixelType;
>     typedef itk::Image<PixelType, 2>        ImageType;
> 
>     typedef itk::ImageFileReader< ImageType> ReaderType;
>     typedef itk::ImageFileWriter< ImageType> WriterType;
> 
>     typedef itk::RegionOfInterestImageFilter<ImageType,ImageType> 
> 											 CropFilterType;
> 	typedef itk::FlipImageFilter< ImageType> FlipType;
>     
> 
>     /*
> 	Declaration of Variables and Pointers
> 
> 	*/
> 	ReaderType::Pointer reader = ReaderType::New();
>     WriterType::Pointer writer = WriterType::New();
>    
> 	CropFilterType::Pointer crop = CropFilterType::New();
> 
> 	ImageType::RegionType region;
> 	ImageType::SizeType		size;
> 	ImageType::IndexType    index;
> 
> 	FlipType::Pointer flip =FlipType::New();
> 	FlipType::FlipAxesArrayType flipAxesSet;
> 	
> 	/*
> 	Setting of the filenames of the input and output files
> 	currently command line options are used to determine them
> 	*/
>     std::string input  = argv[1];
>     std::string output = argv[2];
> 
>     
> 	/*
> 	Setting up and performing InputIO
> 
> 	*/
> 	reader->SetFileName( input.c_str() );
> 	reader->ReleaseDataFlagOn();
> 	try 
> 		{
> 		reader->Update();
> 		} 
> 	catch (itk::ExceptionObject &err) 
>         {
>         std::cout<< "Exception caught:" << std::endl;
>         std::cout<< err <<std::endl;
> 		
>         return -1;
>         }
>     
> 	/*
> 	Getting the region we want to extract. The Image is only cropped
> 	in XY Dimensions the Z Dimension keeps unchanged
> 
> 	*/
> 	region=reader->GetOutput()->GetLargestPossibleRegion();
> 	index=region.GetIndex();
> 	size=region.GetSize();
> 
> 	index.SetElement(0,xStart);
> 	index.SetElement(1,yStart);
> 	
> 	size.SetElement(0,xExtent);
> 	size.SetElement(1,yExtent);
> 
> 	
> 
> 	region.SetSize(size);
> 	region.SetIndex(index);
> 	
> 
> 	/*
> 	Setup of crop ImageFilter
> 
> 	*/
> 	crop->SetInput(reader->GetOutput());
> 	crop->SetRegionOfInterest(region);
> 	crop->ReleaseDataFlagOn();
> 
> 	/*
> 	We have to flip the Image in Y Direction.
> 
> 	*/
> 	flip->SetInput(crop->GetOutput());
> 	flip->ReleaseDataFlagOn();
> 	
> 	flipAxesSet=flip->GetFlipAxes();
> 	//flipAxesSet[1]=true;
> 	flip->SetFlipAxes(flipAxesSet);
> 	
> 
> 	/*
> 	Of course we want to write the image to the HDD
> 
> 	*/
> 	writer->SetFileName( output.c_str() );
>     writer->SetInput( flip->GetOutput() );
> 
>     
> 	/*
> 	The invocation of the pipeline should cause the execution of the crop and flip
> 	filters. The image should be read already and therefor resist in the main memory.
> 	*/
> 	try {
>         writer->Update();
>         } 
>     catch (itk::ExceptionObject &err) 
>         {
>         std::cout<< "Exception caught:" << std::endl;
>         std::cout<< err <<std::endl;
> 		
>         return -1;
>         }
> 
>     return 0;
> }