ImageJ 2/3D FFT in Imglib2 not what I expect

Hi all,

I’m having difficulties getting a 2 or 3D FFT using Imglib2, the example scripts’ functions have been deprecated and so I’m a little confused on how to approach the problem. The code I have so far produces an imagej which is neither the same size or half the size of the original image. It looks to be an FFT but with the top and bottom half switched and the left half of the FFT missing.

Code so far please advise!

		    File file = new File( "Segment.tif" );
            
        	InputStream is = getClass().getResourceAsStream("Single.tif");
            Opener opener = new Opener();
            ImagePlus input = opener.openTiff(is, "Input");
            //input.show();


           	
            
            Img< UnsignedByteType > image = ImagePlusAdapter.wrap(input);        
            ImgFactory< ComplexFloatType > imgFactory = new ArrayImgFactory< ComplexFloatType >();        
            Img< ComplexFloatType > fft = imgFactory.create( new long[]{ 1024, 1024, 3 }, new ComplexFloatType(0,0) );
            Img< ComplexFloatType > output = imgFactory.create( new long[]{ 1024, 1024, 3 }, new ComplexFloatType(0,0) );
            
            //final RandomAccess< UnsignedByteType > r = image.randomAccess();
            //final RandomAccess< UnsignedByteType > r = image.randomAccess();
            
            output = FFT.realToComplex(image,output.factory());
            FFT.complexToComplexInverse((RandomAccessibleInterval<ComplexFloatType>) output.randomAccess(),1);
            //output = FFT.realToComplex(image.randomAccess(),output.randomAccess());
            
            ImageJFunctions.show( output );

Thanks

Craig

Hi Craig

I would recommend opening up the Fiji Script editor, going to template->tutorials and looking at the Find_Template tutorial. This tutorial is similar to the imglib2 FFT example but uses imagej-ops (the behavior should be similar to what you would get using FFTMethods as ops just wraps FFTMethods).

Also here is a link to a similar question regarding FFT

http://imagej.net/pipermail/imagej-devel/2014-December/002373.html

I believe in that case the differences were because the extension strategy and border sizes are different between the old and new imglib2 FFTs. Keep in mind the input to the FFT may be padded to a “fast” size and that changes the output size.

Good day Craig,

you may search for the ImageJ-Plugin “Parallel FFTJ_1-4”.

It does either a DFT or a FFT, depending on the image support size, and it provides what you would expect from a 2D or 3D DFT.

HTH

Herbie

I’ve used Parallel FFTJ before and I agree it is good, I just expected the inbuilt FFT in imglib2 to be the preferred method primarily because it should put FFT values into a complex structure automatically. I’m not averse I’m more asking the question is FFTJ a better alternative to imglib2 FFT?

Craig,

if you are not satisfied by the imglib2 FFT approach, why not use an approach that satisfies you.

Somehow puzzled

Herbie

My main concern is that repackaging the imageplus images out of FFTJ input a complex imglib2 data structure would be type and ram wasted.

Hi Craig

If you are writing large amounts of code that use imglib2 and/or imagej-ops the imglib2 FFT would be the preferred method. It is highly optimized to work with imglib2 structures (for example you can “virtually” extend images using views).

If you do not have a need for imglib2, than maybe another FFT method would be easier to work with.

Yes. It should return a complex image, with the first dimensions slightly larger than 1/2 the original dimension, and the other dimensions slightly larger then the original dimensions. (first dimensions 1/2 size because of symmetry for FFT of real values, the slightly larger is because padding is done).

0 frequency should be at 0,0. Is this what you are seeing? Or something different??

For example if I perform FFT on a 1024 by 1024 image it returns a complex array of size 561 by 1040 (btw does anyone know why power 2 sizes get padded?? @StephanPreibisch??).

When you display with ImageJFunctions.show(), I believe internally it defaults to show the power spectrum, you can pass a ‘converter’ to show different views.

# display fft phase spectrum
ImageJFunctions.show( templateFFT,ComplexPhaseFloatConverter() ).setTitle( "fft phase spectrum" )

# display fft real values
ImageJFunctions.show( templateFFT,ComplexRealFloatConverter() ).setTitle( "fft real values" )
        
# display fft imaginary values
ImageJFunctions.show( templateFFT, ComplexImaginaryFloatConverter() ).setTitle( "fft imaginary values" )