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

Madhusudhanan Balasubramanian madhu_lsu at yahoo.com
Wed Nov 3 10:59:34 EST 2004


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];
}


			
---------------------------------
Do you Yahoo!?
 Check out the new Yahoo! Front Page.  www.yahoo.com/a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20041103/78e77e38/attachment.html


More information about the Insight-users mailing list