[Insight-users] image addition subtraction and the RescaleIntensityImageFilter

Seniha Esen Yuksel eseny99 at yahoo.com
Tue Nov 15 12:22:16 EST 2005


Skipped content of type multipart/alternative-------------- next part --------------
/*=========================================================================

  Program:	 Insight Segmentation & Registration Toolkit
  Module:	 $RCSfile: ImageSubtraction.cxx,v $
  Language:  C++
  Date: 	 $Date: 2005/12/31 01:16:11 $
  Version:	 $Revision: 1.23 $
  
	Copyright (c) Insight Software Consortium. All rights reserved.
	See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
	
	  This software is distributed WITHOUT ANY WARRANTY; without even 
	  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
	  PURPOSE.  See the above copyright notices for more information.
	  
=========================================================================*/



#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include <iostream.h>

#include "itkCastImageFilter.h"
#include "itkAddImageFilter.h"
#include "itkSubtractImageFilter.h"
#include "itkConnectedComponentImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"

#include <stdlib.h>
#include <string.h>
#include "string.h"


int main( int argc, char * argv[] )
{


const   unsigned int	   Dimension = 2;
	
	
typedef	 unsigned short	  InputPixelType;
typedef itk::Image< InputPixelType,  Dimension >	 InputImageType;

// Note that the output image type requires a larger
// dynamic range (unsigned short) in order to hold
// the values resulting from the addition.

typedef itk::AddImageFilter<
                      InputImageType,
                      InputImageType,
                      InputImageType  > AdderType;


typedef itk::Image< unsigned char, Dimension >   WriteImageType;

typedef itk::RescaleIntensityImageFilter<
                                   InputImageType,
                                   WriteImageType
                                          >   RescalerType;

typedef itk::ImageFileWriter< WriteImageType > WriterType;

// Note that the writer type is declared using 8 bits
// and the rescaler filter will renormalize the
// intensity levels of the sum in order to fit in 8 bits again.
// (after the addition the data was potentially in 9 bits.)

AdderType::Pointer adder = AdderType::New();

RescalerType::Pointer rescaler = RescalerType::New();

WriterType::Pointer writer = WriterType::New();


typedef itk::ImageFileReader< InputImageType >  ReaderType;
	ReaderType::Pointer reader1 = ReaderType::New();
	ReaderType::Pointer reader2 = ReaderType::New();
		
	reader1->SetFileName("im1.jpg");
	reader2->SetFileName("im2.jpg");
	
	

adder->SetInput1(reader1->GetOutput());
adder->SetInput2(reader2->GetOutput());

rescaler->SetInput( adder->GetOutput() );
rescaler->SetOutputMinimum(   0 );
rescaler->SetOutputMaximum( 255 );

writer->SetInput( rescaler->GetOutput() );

writer->SetFileName("sum1.png");

writer->Update();

// end of addition

//add the light kidneys
AdderType::Pointer adder2 = AdderType::New();

ReaderType::Pointer reader3 = ReaderType::New();
ReaderType::Pointer reader4 = ReaderType::New();

reader3->SetFileName("im15.jpg");
reader4->SetFileName("im26.jpg");

adder2->SetInput1(reader3->GetOutput());
adder2->SetInput2(reader4->GetOutput());
RescalerType::Pointer rescaler2 = RescalerType::New();

rescaler2->SetInput( adder2->GetOutput() );
rescaler2->SetOutputMinimum(   0 );
rescaler2->SetOutputMaximum( 255 );


writer->SetInput( rescaler2->GetOutput() );
writer->SetFileName("sum2.png");
writer->Update();

// end of addition


//subtracting the added images -- not the rescaled ones!


typedef itk::SubtractImageFilter<InputImageType,InputImageType,InputImageType> SubtractionImageFilterType;

SubtractionImageFilterType::Pointer subtractor=SubtractionImageFilterType::New();
subtractor->SetInput1(adder->GetOutput());
subtractor->SetInput2(adder2->GetOutput());

subtractor->Update();

RescalerType::Pointer rescaler3 = RescalerType::New();
rescaler3->SetInput( subtractor->GetOutput() );
rescaler3->SetOutputMinimum(   0 );
rescaler3->SetOutputMaximum( 255 );

writer->SetInput( rescaler3->GetOutput() );

writer->SetFileName("difference.png");

writer->Update();



///*******************
//connected component analysis:
//********************


typedef itk::CastImageFilter<WriteImageType, InputImageType > CastToRealFilterType2;
CastToRealFilterType2::Pointer toShort = CastToRealFilterType2::New();
toShort->SetInput(rescaler3->GetOutput() ); 
toShort->Update();



typedef itk::ConnectedComponentImageFilter <InputImageType,InputImageType > ConnectedComponentImageFilterType;
ConnectedComponentImageFilterType::Pointer labelFilter = ConnectedComponentImageFilterType::New ();
labelFilter->SetInput(toShort->GetOutput());
labelFilter->Update();


typedef itk:: RelabelComponentImageFilter<InputImageType,InputImageType > RelabelComponentImageFilterType;
RelabelComponentImageFilterType::Pointer relabelFilter = RelabelComponentImageFilterType::New ();

relabelFilter->SetInput(labelFilter->GetOutput());
relabelFilter->Update();



typedef itk::BinaryThresholdImageFilter<InputImageType,WriteImageType > BinaryThresholdImageFilterType;
BinaryThresholdImageFilterType::Pointer binaryThresholdFilter = BinaryThresholdImageFilterType::New ();

binaryThresholdFilter->SetInput( relabelFilter->GetOutput());
binaryThresholdFilter->SetOutsideValue(255);             
binaryThresholdFilter->SetInsideValue(0);            
binaryThresholdFilter->SetLowerThreshold( 1 );       
binaryThresholdFilter->SetUpperThreshold( 1);      
try
{

binaryThresholdFilter->Update(); 


}
catch( itk::ExceptionObject & err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}

writer->SetInput( binaryThresholdFilter->GetOutput() );

writer->SetFileName("Connecteddifference.png");

writer->Update();


return 0;
}


	



More information about the Insight-users mailing list