<DIV>
<DIV>Hi Luis/all,</DIV>
<DIV> </DIV>
<DIV>Here is the updated median filter routine that receives an input buffer and applies median filter (ITK) and imports the filtered image to VTK. The problem I had with the earlier routine was with ShallowCopy (so obvious); the imageToVTKFilter output (which is vtkImageData *) is destroyed after returning from the routine, while ShallowCopy maintains the earlier reference; so I replaced with DeepCopy and it worked.</DIV>
<DIV> </DIV>
<DIV>Thanks for your help Luis/all.</DIV>
<DIV> </DIV>
<DIV>Madhu.</DIV>
<DIV><BR>New routine:</DIV>
<DIV>//Apply median filter to the input buffer<BR>void medianOnBuffer(unsigned char *userBuffer, int imgWidth, int imgHeight, vtkImageData* medianImage){</DIV>
<DIV> int *dimensions;<BR> dimensions = new int[2];<BR> dimensions[0] = imgWidth;<BR> dimensions[1] = imgHeight;</DIV>
<DIV> typedef unsigned char pixelType;<BR> typedef itk::Image<pixelType, 2> imageType;</DIV>
<DIV> </DIV>
<DIV> //////////////////////////////////<BR> //Import the input buffer into ITK<BR> typedef itk::ImportImageFilter<pixelType, 2> importFilterType;<BR> importFilterType::Pointer importFilter = importFilterType::New();</DIV>
<DIV> importFilterType::SizeType size;<BR> size[0] = imgWidth; //x-axis<BR> size[1] = imgHeight; //y-axis</DIV>
<DIV> importFilterType::IndexType start;<BR> start.Fill(0);</DIV>
<DIV> importFilterType::RegionType region;<BR> region.SetIndex(start);<BR> region.SetSize(size);</DIV>
<DIV> importFilter->SetRegion(region);</DIV>
<DIV> double origin[2];<BR> origin[0] = 0.0;<BR> origin[1] = 0.0;</DIV>
<DIV> importFilter->SetOrigin(origin);</DIV>
<DIV> double spacing[2];<BR> spacing[0] = 1.0;<BR> spacing[1] = 1.0;</DIV>
<DIV> importFilter->SetSpacing(spacing);</DIV>
<DIV> const bool importImageFilterWillOwnTheBuffer = false;<BR> importFilter->SetImportPointer(userBuffer, imgWidth*imgHeight, importImageFilterWillOwnTheBuffer);<BR> importFilter->Update();</DIV>
<DIV> </DIV>
<DIV> //////////////////////////////////<BR> //Apply median filter to the image<BR> typedef itk::Image<pixelType, 2> InputImageType;<BR> typedef itk::Image<pixelType, 2> OutputImageType;<BR> typedef itk::MedianImageFilter<InputImageType, OutputImageType> filterType;</DIV>
<DIV> filterType::Pointer filter = filterType::New();</DIV>
<DIV> //Define filter boundaries<BR> InputImageType::SizeType indexRadius;</DIV>
<DIV> indexRadius[0] = 1;<BR> indexRadius[1] = 1;</DIV>
<DIV> filter->SetRadius(indexRadius);<BR> filter->SetInput(importFilter->GetOutput());<BR> filter->Update();</DIV>
<DIV> </DIV>
<DIV> /////////////////////////////////////////////<BR> //Import the filtered image to VTK (from ITK)<BR> typedef itk::ImageToVTKImageFilter<OutputImageType> connectorType;<BR> connectorType::Pointer connector = connectorType::New();</DIV>
<DIV> connector->SetInput(filter->GetOutput());<BR> connector->Update();</DIV>
<DIV> //Copy the output<BR> medianImage->DeepCopy(connector->GetImporter()->GetOutput());<BR> medianImage->Update();<BR>}<BR><BR><B><I>Luis Ibanez <luis.ibanez@kitware.com></I></B> wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #1010ff 2px solid"><BR>Hi Madhusudhanan,<BR><BR>From your email,<BR>It seems that the code is now working fine for you...<BR>is that right ?<BR><BR><BR>Could you please describe the problems that you found<BR>when you were:<BR><BR>"initializing some values earlier in the pipeline."<BR><BR><BR><BR>Thanks<BR><BR><BR>Luis<BR><BR><BR><BR>------------------------------------<BR>Madhusudhanan Balasubramanian wrote:<BR><BR>> Hi Luis / all,<BR>> <BR>> The routine I posted for interfacing the ITK output (output from median <BR>> filter) with VTK works just fine. I had problem in initializing some <BR>> values earlier in the pipeline. Thought I'll let you know.<BR>> <BR>> Madhu.<BR>> <BR>> */Luis Ibanez <LUIS.IBANEZ@KITWARE.COM>/* wrote:<BR>> <BR>> <BR>> Hi Madhusudhanan,<BR>> <BR>> Please write to a file the output of the input filter<BR>> in order to verify if the
ITK image created form your<BR>> buffer is correct. Just connect an ImageFileWriter at<BR>> the output of the Importer.<BR>> <BR>> That will help to identify if the problem is related<BR>> to your local buffer or to the convertion between ITK<BR>> and VTK images.<BR>> <BR>> Please let us know what you find.<BR>> <BR>> <BR>> Thanks<BR>> <BR>> <BR>> Luis<BR>> <BR>> <BR>> ----------------------------------------<BR>> Madhusudhanan Balasubramanian wrote:<BR>> <BR>> > Hi all,<BR>> ><BR>> > I wrote a small routine that applied median filter (ITK) on an input<BR>> > buffer (unsigned char *) and imports the filter output to VTK.<BR>> > Initially, when I returned the vtkImageData * output from<BR>> > 'ImageToVTKImageFilter', I had problem accessing the data (memory<BR>> > exception). So I decided to ! copy the data into a separate<BR>> buffer before<BR>> > returning from the routine.
However the result is just some noisy<BR>> image<BR>> > (nowhere close to the input image). I am herewith attaching the<BR>> routine<BR>> > that I wrote. I appreciate if anyone has any inputs on this.<BR>> ><BR>> > Thanks,<BR>> > Madhu.<BR>> ><BR>> > //Apply median filter to the input buffer<BR>> > void medianOnBuffer(unsigned char *userBuffer, int imgWidth, int<BR>> > imgHeight, vtkImageData* medianImage, unsigned char *outputImageUSC){<BR>> ><BR>> > int *dimensions;<BR>> > dimensions = new int[2];<BR>> > dimensions[0] = imgWidth;<BR>> > dimensions[1] = imgHeight;<BR>> ><BR>> > typedef unsigned char pixelType;<BR>> > typedef itk::Image imageType;<BR>> > typedef itk::ImportImageFilter importFilterType;<BR>> > importFilterType::Pointer importFilter = importFilterType::New();<BR>> > importFilterType::SizeType size;<BR>> > size[0] = imgWidth;
//x-axis<BR>> > s! ize[1] = imgHeight; //y-axis<BR>> > importFilterType::IndexType start;<BR>> > start.Fill(0);<BR>> > importFilterType::RegionType region;<BR>> > region.SetIndex(start);<BR>> > region.SetSize(size);<BR>> > importFilter->SetRegion(region);<BR>> > double origin[2];<BR>> > origin[0] = 0.0;<BR>> > origin[1] = 0.0;<BR>> > importFilter->SetOrigin(origin);<BR>> > double spacing[2];<BR>> > spacing[0] = 1.0;<BR>> > spacing[1] = 1.0;<BR>> > importFilter->SetSpacing(spacing);<BR>> > //<BR>> > const bool importImageFilterWillOwnTheBuffer = false;<BR>> > importFilter->SetImportPointer(userBuffer, imgWidth*imgHeight,<BR>> > importImageFilterWillOwnTheBuffer);<BR>> > importFilter->Update();<BR>> > //Apply median filter to the image<BR>> > typedef itk::Image InputImageType;<BR>> > typedef itk::Image OutputImageType;<BR>> >
typedef itk::MedianImageFilter filterType;<BR>> > filterType::Pointer filter = filterType::New();<BR>> > //Define filter boundaries<BR>> > InputImageType::SizeType indexRadius;<BR>> > indexRadius[0] = 1;<BR>> > indexRadius[1] = 1;<BR>> > filter->SetRadius(indexRadius);<BR>> > filter->SetInput(importFilter->GetOutput());<BR>> > filter->Update();<BR>> > //Import the filtered image to VTK<BR>> > typedef itk::ImageToVTKImageFilter connectorType;<BR>> > connectorType::Pointer connector = connectorType::New();<BR>> > connector->SetInput(filter->GetOutput());<BR>> > connector->Update();<BR>> > //Copy the output<BR>> > medianImage->ShallowCopy(connector->GetOutput());<BR>> > medianImage->Update();<BR>> > //Validate the imported image<BR>> > unsigned char *testBuffer = (unsigned char<BR>> > *)medianImage->GetScalarPointer();<BR>> ><BR>>
> //copy result to outputImageUSC<BR>> > int i, j;<BR>> > for (i = 0; i < dimensions[1]; i++)<BR>> > for (j = 0; j < dimensions[0]; j++)<BR>> > outputImageUSC[i * dimensions[0] + j] = testBuffer[i * dimensions[0]<BR>> > + j];<BR>> > }<BR>> ><BR>> <BR>> <BR>> <BR>> <BR>> <BR>> __________________________________________________<BR>> Do You Yahoo!?<BR>> Tired of spam? Yahoo! Mail has the best spam protection around<BR>> http://mail.yahoo.com<BR>> <BR><BR><BR><BR><BR></BLOCKQUOTE></DIV><p>__________________________________________________<br>Do You Yahoo!?<br>Tired of spam? Yahoo! Mail has the best spam protection around <br>http://mail.yahoo.com