[Insight-users] crop and flip images

Stefan Lindenau stefan . lindenau at gmx . de
Thu, 27 Nov 2003 11:40:52 -0500


This is a multi-part message in MIME format.
--------------080707070202030508010703
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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


--------------080707070202030508010703
Content-Type: text/plain;
 name="VHDResizing.cxx"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="VHDResizing.cxx"


#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;
}
--------------080707070202030508010703--