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