[Insight-users] Importing median filter output from ITK to VTK

Luis Ibanez luis.ibanez at kitware.com
Fri Nov 5 21:02:39 EST 2004


Hi Madhusudhanan,

Please write to a file the output of the input filter
in order to verify if the ITK image created form your
buffer is correct. Just connect an ImageFileWriter at
the output of the Importer.

That will help to identify if the problem is related
to your local buffer or to the convertion between ITK
and VTK images.

Please let us know what you find.


    Thanks


      Luis


----------------------------------------
Madhusudhanan Balasubramanian wrote:

> Hi all,
>  
> I wrote a small routine that applied median filter (ITK) on an input 
> buffer (unsigned char *) and imports the filter output to VTK.  
> Initially, when I returned the vtkImageData * output from 
> 'ImageToVTKImageFilter', I had problem accessing the data (memory 
> exception).  So I decided to copy the data into a separate buffer before 
> returning from the routine.  However the result is just some noisy image 
> (nowhere close to the input image).  I am herewith attaching the routine 
> that I wrote.  I appreciate if anyone has any inputs on this.
>  
> Thanks,
> Madhu.
>  
> //Apply median filter to the input buffer
> void medianOnBuffer(unsigned char *userBuffer, int imgWidth, int 
> imgHeight, vtkImageData* medianImage, unsigned char *outputImageUSC){
>  
>  int *dimensions;
>  dimensions = new int[2];
>  dimensions[0] = imgWidth;
>  dimensions[1] = imgHeight;
>  
>  typedef unsigned char pixelType;
>  typedef itk::Image<pixelType, 2> imageType;
>  typedef itk::ImportImageFilter<pixelType, 2> importFilterType;
>  importFilterType::Pointer importFilter = importFilterType::New();
>  importFilterType::SizeType size;
>  size[0] = imgWidth;  //x-axis
>  size[1] = imgHeight; //y-axis
>  importFilterType::IndexType start;
>  start.Fill(0);
>  importFilterType::RegionType region;
>  region.SetIndex(start);
>  region.SetSize(size);
>  importFilter->SetRegion(region);
>  double origin[2];
>  origin[0] = 0.0;
>  origin[1] = 0.0;
>  importFilter->SetOrigin(origin);
>  double spacing[2];
>  spacing[0] = 1.0;
>  spacing[1] = 1.0;
>  importFilter->SetSpacing(spacing);
>  //
>  const bool importImageFilterWillOwnTheBuffer = false;
>  importFilter->SetImportPointer(userBuffer, imgWidth*imgHeight, 
> importImageFilterWillOwnTheBuffer);
>  importFilter->Update();
>  //Apply median filter to the image
>  typedef itk::Image<pixelType, 2> InputImageType;
>  typedef itk::Image<pixelType, 2> OutputImageType;
>  typedef itk::MedianImageFilter<InputImageType, OutputImageType> filterType;
>  filterType::Pointer filter = filterType::New();
>  //Define filter boundaries
>  InputImageType::SizeType indexRadius;
>  indexRadius[0] = 1;
>  indexRadius[1] = 1;
>  filter->SetRadius(indexRadius);
>  filter->SetInput(importFilter->GetOutput());
>  filter->Update();
>  //Import the filtered image to VTK
>  typedef itk::ImageToVTKImageFilter<OutputImageType> connectorType;
>  connectorType::Pointer connector = connectorType::New();
>  connector->SetInput(filter->GetOutput());
>  connector->Update();
>  //Copy the output
>  medianImage->ShallowCopy(connector->GetOutput());
>  medianImage->Update();
>  //Validate the imported image
>  unsigned char *testBuffer = (unsigned char 
> *)medianImage->GetScalarPointer();
>  
>  //copy result to outputImageUSC
>  int i, j;
>  for (i = 0; i < dimensions[1]; i++)
>   for (j = 0; j < dimensions[0]; j++)
>    outputImageUSC[i * dimensions[0] + j] = testBuffer[i * dimensions[0] 
> + j];
> }
> 







More information about the Insight-users mailing list