[Insight-users] How to restrict a filter to a region
Elena Pavlovskaia
elena.pavlovskaia at otismed.com
Mon Apr 16 23:08:15 EDT 2007
Hello!
I want to apply a filter to a sub-region in my image.
Looking into comments in the header files I though I had to call
SetRequestedRegion before executing the filter.
I tried calling SetRequestedRegion with both input and output images
of the filter, but the filter seemed to be always applied to the whole
image.
I tried doing it with a) SignedDanielssonDistanceMapImageFilter;
b) DiscreteGaussianImageFilter.
Can somebody advise on how I could restrict a filter execution to a region?
I am mostly interested with restricting the DiscreteGaussianImageFilter.
I pasted below the code where I am trying to restrict the
SignedDanielssonDistanceMapImageFilter to a sub-region.
I run it with the following parameters:
"InitialImage.png" "SmoothedImage.png" 4 4
The first two parameters stand for the names of the output images:
before smoothing and after smoothing.
The last two parameters are the filter parameters.
I attached the output of my program execution. The SmoothedImage.png looks
like
if the smoothing filter were applied to the whole image.
Thank you!
Elena
------------- MY CODE --------------
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkImage.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageFileWriter.h"
#include "itkCastImageFilter.h"
#include "itkDiscreteGaussianImageFilter.h"
int main( int argc, char * argv[] )
{
if( argc < 5 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " InitialImageFile
SmoothedImageFile variance maxKernelWidth " << std::endl;
return EXIT_FAILURE;
}
typedef float InputPixelType;
typedef float OutputPixelType;
typedef itk::Image< InputPixelType, 2 > InputImageType;
typedef itk::Image< OutputPixelType, 2 > OutputImageType;
typedef unsigned char WritePixelType;
typedef itk::Image< WritePixelType, 2 > WriteImageType;
typedef itk::CastImageFilter<
OutputImageType, WriteImageType > CastFilterType;
CastFilterType::Pointer caster = CastFilterType::New();
typedef itk::ImageFileWriter< WriteImageType > WriterType;
WriterType::Pointer writer1 = WriterType::New();
writer1->SetFileName( argv[1] );
WriterType::Pointer writer2 = WriterType::New();
writer2->SetFileName( argv[2] );
// Create input image
InputImageType::Pointer image = InputImageType::New();
InputImageType::IndexType start;
start[0] = 0; // first index on X
start[1] = 0; // first index on Y
const unsigned int length = 50;
InputImageType::SizeType size;
size[0] = length; // size along X
size[1] = length; // size along Y
InputImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
// Fill in the top/left triangle with 0, and the right/bottom
triangle with 255:
typedef itk::ImageRegionIteratorWithIndex<InputImageType>
IteratorType;
IteratorType it( image, region );
InputPixelType subtract = length - 1;
it.GoToBegin();
while( !it.IsAtEnd() )
{
InputImageType::IndexType index = it.GetIndex();
InputPixelType value = index[0] + index[1] - subtract;
if( value > 0 )
{
value = 255.0;
}
else
{
value = 0.0;
}
it.Set( value );
++it;
}
// Write down our initial image:
caster->SetInput( image );
caster->Modified();
writer1->SetInput( caster->GetOutput() );
writer1->Update();
// Init a smoothing filter:
typedef itk::DiscreteGaussianImageFilter<
InputImageType, OutputImageType > FilterType;
FilterType::Pointer filter = FilterType::New();
filter->SetInput( image );
const double gaussianVariance = atof( argv[3] );
const unsigned int maxKernelWidth = atoi( argv[4] );
filter->SetVariance( gaussianVariance );
filter->SetMaximumKernelWidth( maxKernelWidth );
// Create a regionOut with the sizes = 1/2 of the whole image
// and set it as requested region for in and out images of the
smoothing filter:
OutputImageType::Pointer smoothImage = filter->GetOutput();
OutputImageType::RegionType regionOut = region;
OutputImageType::RegionType::SizeType sizeOut;
sizeOut[0] = size[0]/2;
sizeOut[1] = size[1]/2;
regionOut.SetSize( sizeOut );
smoothImage->SetRegions( region );
smoothImage->Allocate();
image->SetRequestedRegion( regionOut );
smoothImage->SetRequestedRegion( regionOut );
// Execute the filter:
filter->Update();
// Write down the result:
caster->SetInput( smoothImage );
caster->Modified();
writer2->SetInput( caster->GetOutput() );
writer2->Update();
return EXIT_SUCCESS;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: InitialImage.png
Type: image/png
Size: 268 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/insight-users/attachments/20070416/32da7147/InitialImage.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SmoothedImage.png
Type: image/png
Size: 344 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/insight-users/attachments/20070416/32da7147/SmoothedImage.png
More information about the Insight-users
mailing list