[vtkusers] VTK FFT convolution different to Matlab convolution

John Fraser John.Fraser at icr.ac.uk
Tue Aug 24 06:16:18 EDT 2010


Markus you are right, the error is introduced because of imagecast which is actually implemented in terms of static_cast, this causes numbers like 8.9999 to be rounded down to 8.  Instead what I should actually have is a filter that first rounds the data to the nearest integer before casting it to the data type I need.

>>> Markus Neuner <neuner.markus at gmx.net> 19/08/2010 18:45 >>>

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
  

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addressee only.  If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer and network.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100824/5b3e3a40/attachment.htm>


More information about the vtkusers mailing list