[vtkusers] VTK FFT convolution different to Matlab convolution

Markus Neuner neuner.markus at gmx.net
Thu Aug 19 13:45:26 EDT 2010


Looks like a casting error because of your data type "UnsignedShort".

<<< snip >>>
vtkImageCast cast
cast SetInputConnection [real GetOutputPort]
cast SetOutputScalarTypeToUnsignedShort
cast Update
<<< snap >>

What is the result when you change the type to float/double?


On 19.08.2010 17:54, John Fraser wrote:
> Hello,
>  
> I'm trying to implement convolutional coding using FFT (based upon
> TestFFTCorrelation.tcl).  The problem is I'm getting off by 1 "errors"
> in the results.  I'm not sure if this is expected or not, I thought
> that VTK FFT based convolution should return exactly the same results
> as Matlab.  I've tried padding the image with zeros as well but I
> can't get the results to be the same.  Any ideas why they are different?
>  
> To test this my kernel is 3 X 3 filled with 1s and my image is 10 X 10
> filled with 1s.  The result from VTK is:
>  
>      8     9     8     8     8     8     9     8     8     8
>      8     9     8     8     8     8     9     8     8     8
>      9     9     9     8     8     9     9     9     8     8
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     8     8     9     9     9     8     8
>      8     9     8     8     8     8     9     8     8     8
>      8     9     8     8     8     8     9     8     8     8
>      9     9     9     8     8     9     9     9     8     8
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     8     8     9     9     9     8     8
>  
> whereas Matlab produces all 9s using ifft2( fft2( d ).*fft2( k ) )
>  
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>      9     9     9     9     9     9     9     9     9     9
>  
>  
> Code (using VTK TCL binary, based upon TestFFTCorrelation.tcl):
>
> set dims 2
> set imgdim 9
> set kerdim 2
>  
> vtkImageReader image1
> # sample image is all 1s 10 X 10 = 100 bytes
> image1 SetFileName "SampleImg.bin"
> image1 SetHeaderSize 0
> image1 SetDataByteOrderToLittleEndian
> image1 SetFileDimensionality $dims
> image1 SetDataOrigin 0 0 0
> image1 SetDataSpacing 1 1 1
> image1 SetNumberOfScalarComponents 1
> image1 SetDataExtent 0 $imgdim 0 $imgdim 0 0
> image1 SetDataScalarTypeToUnsignedChar
>  
> vtkImageReader kernel
> # kernel image is all 1s 3 X 3 = 9 bytes
> kernel SetFileName "Kernel.bin"
> kernel SetHeaderSize 0
> kernel SetDataByteOrderToLittleEndian
> kernel SetFileDimensionality $dims
> kernel SetDataOrigin 0 0 0
> kernel SetDataSpacing 1 1 1
> kernel SetNumberOfScalarComponents 1
> kernel SetDataExtent 0 $kerdim 0 $kerdim 0 0
> kernel SetDataScalarTypeToUnsignedChar
>  
> vtkImageFFT fft1
> fft1 SetDimensionality $dims
> fft1 SetInputConnection [image1 GetOutputPort]
>  
> vtkImageConstantPad pad2
> pad2 SetInputConnection [kernel GetOutputPort]
> pad2 SetOutputWholeExtent 0 $imgdim 0 $imgdim 0 0
>  
> vtkImageFFT fft2
> fft2 SetDimensionality $dims
> fft2 SetInputConnection [pad2 GetOutputPort]
>  
> vtkImageMathematics mult
> mult SetOperationToComplexMultiply
> mult SetInput1 [fft1 GetOutput]
> mult SetInput2 [fft2 GetOutput]
>  
> vtkImageRFFT rfft
> rfft SetDimensionality $dims
> rfft SetInputConnection [mult GetOutputPort]
>  
> vtkImageExtractComponents real
> real SetInputConnection [rfft GetOutputPort]
> real SetComponents 0
>  
> vtkImageCast cast
> cast SetInputConnection [real GetOutputPort]
> cast SetOutputScalarTypeToUnsignedShort
> cast Update
>  
> vtkImageWriter writer
> writer SetInputConnection [cast GetOutputPort]
> writer SetFileName "VTK.img"
> writer SetFileDimensionality $dims
> writer Write
>  
> exit
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100819/1fc3c818/attachment.htm>


More information about the vtkusers mailing list