/
FourierTransform.java
421 lines (357 loc) · 15.2 KB
/
FourierTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2023 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package net.imglib2.algorithm.fft;
import edu.mines.jtk.dsp.FftComplex;
import edu.mines.jtk.dsp.FftReal;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.Benchmark;
import net.imglib2.algorithm.MultiThreaded;
import net.imglib2.algorithm.OutputAlgorithm;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.outofbounds.OutOfBoundsConstantValueFactory;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.outofbounds.OutOfBoundsMirrorExpWindowingFactory;
import net.imglib2.outofbounds.OutOfBoundsMirrorFactory;
import net.imglib2.outofbounds.OutOfBoundsMirrorFactory.Boundary;
import net.imglib2.type.numeric.ComplexType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Util;
/**
* Computes the Fourier Transform of a given {@link RandomAccessibleInterval} or {@link Img}.
*
* @param <T> - the intput, {@link RealType}
* @param <S> - the ouput, {@link ComplexType}
* @author Stephan Preibisch
* @deprecated use {@link net.imglib2.algorithm.fft2.FFT} instead
*/
@Deprecated
public class FourierTransform<T extends RealType<T>, S extends ComplexType<S>> implements MultiThreaded, OutputAlgorithm<Img<S>>, Benchmark
{
public static enum PreProcessing { NONE, EXTEND_MIRROR, EXTEND_MIRROR_FADING, USE_GIVEN_OUTOFBOUNDSSTRATEGY }
public static enum Rearrangement { REARRANGE_QUADRANTS, UNCHANGED }
public static enum FFTOptimization { SPEED, MEMORY }
final RandomAccessibleInterval<T> input;
final Interval interval;
final int numDimensions;
final T inputType;
final ImgFactory<S> imgFactory;
Img<S> fftImage;
OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds;
PreProcessing preProcessing;
Rearrangement rearrangement;
FFTOptimization fftOptimization;
float relativeImageExtensionRatio;
int[] imageExtension;
float relativeFadeOutDistance;
int minExtension;
int[] originalSize, originalOffset, extendedSize, extendedZeroPaddedSize;
// if you want the image to be extended more use that
int[] inputSize = null, inputSizeOffset = null;
final S complexType;
String errorMessage = "";
int numThreads;
long processingTime;
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType,
final PreProcessing preProcessing, final Rearrangement rearrangement,
final FFTOptimization fftOptimization, final float relativeImageExtension, final float relativeFadeOutDistance,
final int minExtension )
{
this.input = input;
this.imgFactory = imgFactory;
this.interval = input;
this.complexType = complexType;
this.numDimensions = input.numDimensions();
this.extendedSize = new int[ numDimensions ];
this.extendedZeroPaddedSize = new int[ numDimensions ];
this.imageExtension = new int[ numDimensions ];
this.inputType = Util.getTypeFromInterval( input );
setPreProcessing( preProcessing );
setRearrangement( rearrangement );
setFFTOptimization( fftOptimization );
setRelativeFadeOutDistance( relativeFadeOutDistance );
setRelativeImageExtension( relativeImageExtension );
setMinExtension( minExtension );
this.originalSize = new int[ numDimensions ];
this.originalOffset = new int[ numDimensions ];
for ( int d = 0; d < numDimensions; ++d )
{
if ( interval.dimension( d ) > Integer.MAX_VALUE - 1 )
throw new RuntimeException( "FFT only supports a maximum size in each dimensions of " + (Integer.MAX_VALUE - 1) + ", but in dimension " + d + " it is " + interval.dimension( d ) );
originalSize[ d ] = (int)interval.dimension( d );
}
this.processingTime = -1;
setNumThreads();
}
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType )
{
this ( input, imgFactory, complexType, PreProcessing.EXTEND_MIRROR_FADING, Rearrangement.REARRANGE_QUADRANTS,
FFTOptimization.SPEED, 0.25f, 0.25f, 12 );
}
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType, final Rearrangement rearrangement )
{
this ( input, imgFactory, complexType );
setRearrangement( rearrangement );
}
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType, final FFTOptimization fftOptimization )
{
this ( input, imgFactory, complexType );
setFFTOptimization( fftOptimization );
}
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType, final PreProcessing preProcessing )
{
this ( input, imgFactory, complexType );
setPreProcessing( preProcessing );
}
public FourierTransform( final Img<T> input, final S complexType ) throws IncompatibleTypeException
{
this ( input, input.factory().imgFactory( complexType ), complexType, PreProcessing.EXTEND_MIRROR_FADING, Rearrangement.REARRANGE_QUADRANTS,
FFTOptimization.SPEED, 0.25f, 0.25f, 12 );
}
public FourierTransform( final Img<T> input, final S complexType, final Rearrangement rearrangement ) throws IncompatibleTypeException
{
this ( input, input.factory().imgFactory( complexType ), complexType );
setRearrangement( rearrangement );
}
public FourierTransform( final Img<T> input, final S complexType, final PreProcessing preProcessing ) throws IncompatibleTypeException
{
this ( input, input.factory().imgFactory( complexType ), complexType );
setPreProcessing( preProcessing );
}
public FourierTransform( final Img<T> input, final S complexType, final FFTOptimization fftOptimization ) throws IncompatibleTypeException
{
this ( input, input.factory().imgFactory( complexType ), complexType );
setFFTOptimization( fftOptimization );
}
public FourierTransform( final Img<T> input, final S complexType, final OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds ) throws IncompatibleTypeException
{
this ( input, input.factory().imgFactory( complexType ), complexType );
this.outOfBounds = outOfBounds;
setPreProcessing( PreProcessing.USE_GIVEN_OUTOFBOUNDSSTRATEGY );
}
public FourierTransform( final RandomAccessibleInterval<T> input, final ImgFactory<S> imgFactory, final S complexType, final OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds )
{
this ( input, imgFactory, complexType );
this.outOfBounds = outOfBounds;
setPreProcessing( PreProcessing.USE_GIVEN_OUTOFBOUNDSSTRATEGY );
}
public void setPreProcessing( final PreProcessing preProcessing ) { this.preProcessing = preProcessing; }
public void setCustomOutOfBoundsStrategy( final OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds )
{
this.outOfBounds = outOfBounds;
setPreProcessing( PreProcessing.USE_GIVEN_OUTOFBOUNDSSTRATEGY );
}
public void setRearrangement( final Rearrangement rearrangement ) { this.rearrangement = rearrangement; }
public void setFFTOptimization( final FFTOptimization fftOptimization ) { this.fftOptimization = fftOptimization; }
public void setRelativeFadeOutDistance( final float relativeFadeOutDistance ) { this.relativeFadeOutDistance = relativeFadeOutDistance; }
public void setMinExtension( final int minExtension ) { this.minExtension = minExtension; }
public void setImageExtension( final int[] imageExtension ) { this.imageExtension = imageExtension.clone(); }
public boolean setExtendedOriginalImageSize( final int[] inputSize )
{
for ( int d = 0; d < numDimensions; ++d )
if ( inputSize[ d ] < originalSize[ d ])
{
errorMessage = "Cannot set extended original image size smaller than image size";
return false;
}
this.inputSize = inputSize.clone();
this.inputSizeOffset = new int[ numDimensions ];
setRelativeImageExtension( relativeImageExtensionRatio );
return true;
}
public void setRelativeImageExtension( final float extensionRatio )
{
this.relativeImageExtensionRatio = extensionRatio;
for ( int d = 0; d < interval.numDimensions(); ++d )
{
// how much do we want to extend
if ( inputSize == null )
imageExtension[ d ] = Util.round( interval.dimension( d ) * ( 1 + extensionRatio ) ) - (int)interval.dimension( d );
else
imageExtension[ d ] = Util.round( inputSize[ d ] * ( 1 + extensionRatio ) ) - (int)interval.dimension( d );
if ( imageExtension[ d ] < minExtension )
imageExtension[ d ] = minExtension;
// add an even number so that both sides extend equally
//if ( imageExtensionSum[ d ] % 2 != 0)
// ++imageExtension[ d ];
// the new size includes the current image size
extendedSize[ d ] = imageExtension[ d ] + (int)interval.dimension( d );
}
}
public T getImageType() { return inputType; }
public int[] getExtendedSize() { return extendedSize.clone(); }
public PreProcessing getPreProcessing() { return preProcessing; }
public Rearrangement getRearrangement() { return rearrangement; }
public FFTOptimization getFFOptimization() { return fftOptimization; }
public float getRelativeImageExtension() { return relativeImageExtensionRatio; }
public int[] getImageExtension() { return imageExtension.clone(); }
public float getRelativeFadeOutDistance() { return relativeFadeOutDistance; }
public OutOfBoundsFactory<T, RandomAccessibleInterval<T>> getCustomOutOfBoundsStrategy() { return outOfBounds; }
public int getMinExtension() { return minExtension; }
public int[] getOriginalSize() { return originalSize.clone(); }
public int[] getOriginalOffset() { return originalOffset.clone(); }
public int[] getFFTInputOffset( )
{
if ( inputSize == null )
return originalOffset;
return inputSizeOffset;
}
public int[] getFFTInputSize( )
{
if ( inputSize == null )
return originalSize.clone();
return inputSize.clone();
}
@Override
public boolean process()
{
final long startTime = System.currentTimeMillis();
//
// perform FFT on the temporary image
//
final OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBoundsFactory;
switch ( preProcessing )
{
case USE_GIVEN_OUTOFBOUNDSSTRATEGY:
{
if ( outOfBounds == null )
{
errorMessage = "Custom OutOfBoundsStrategyFactory is null, cannot use custom strategy";
return false;
}
extendedZeroPaddedSize = getZeroPaddingSize( getExtendedImageSize( input, imageExtension ), fftOptimization );
outOfBoundsFactory = outOfBounds;
break;
}
case EXTEND_MIRROR:
{
extendedZeroPaddedSize = getZeroPaddingSize( getExtendedImageSize( input, imageExtension ), fftOptimization );
outOfBoundsFactory = new OutOfBoundsMirrorFactory< T, RandomAccessibleInterval<T> >( Boundary.SINGLE );
break;
}
case EXTEND_MIRROR_FADING:
{
extendedZeroPaddedSize = getZeroPaddingSize( getExtendedImageSize( input, imageExtension ), fftOptimization );
outOfBoundsFactory = new OutOfBoundsMirrorExpWindowingFactory< T, RandomAccessibleInterval<T> >( relativeFadeOutDistance );
break;
}
default: // or NONE
{
if ( inputSize == null )
{
final int[] tmp = new int[ input.numDimensions() ];
for ( int d = 0; d < numDimensions; ++d )
tmp[ d ] = (int)input.dimension( d );
extendedZeroPaddedSize = getZeroPaddingSize( tmp, fftOptimization );
}
else
{
extendedZeroPaddedSize = getZeroPaddingSize( inputSize, fftOptimization );
}
outOfBoundsFactory = new OutOfBoundsConstantValueFactory<T, RandomAccessibleInterval<T>>( inputType.createVariable() );
break;
}
}
originalOffset = new int[ numDimensions ];
for ( int d = 0; d < numDimensions; ++d )
{
if ( inputSize != null )
inputSizeOffset[ d ] = ( extendedZeroPaddedSize[ d ] - inputSize[ d ] ) / 2;
originalOffset[ d ] = ( extendedZeroPaddedSize[ d ] - (int)input.dimension( d ) ) / 2;
}
fftImage = FFTFunctions.computeFFT( input, imgFactory, complexType, outOfBoundsFactory, originalOffset, extendedZeroPaddedSize, getNumThreads(), false );
if ( fftImage == null )
{
errorMessage = "Could not compute the FFT transformation, most likely out of memory";
return false;
}
// rearrange quadrants if wanted
if ( rearrangement == Rearrangement.REARRANGE_QUADRANTS )
FFTFunctions.rearrangeFFTQuadrants( fftImage, true, getNumThreads() );
processingTime = System.currentTimeMillis() - startTime;
return true;
}
protected int[] getExtendedImageSize( final RandomAccessibleInterval<?> input, final int[] imageExtension )
{
final int[] extendedSize = new int[ input.numDimensions() ];
for ( int d = 0; d < input.numDimensions(); ++d )
{
// the new size includes the current image size
extendedSize[ d ] = imageExtension[ d ] + (int)input.dimension( d );
}
return extendedSize;
}
protected int[] getZeroPaddingSize( final int[] imageSize, final FFTOptimization fftOptimization )
{
final int[] fftSize = new int[ imageSize.length ];
// the first dimension is real to complex
if ( fftOptimization == FFTOptimization.SPEED )
fftSize[ 0 ] = FftReal.nfftFast( imageSize[ 0 ] );
else
fftSize[ 0 ] = FftReal.nfftSmall( imageSize[ 0 ] );
// all the other dimensions complex to complex
for ( int d = 1; d < fftSize.length; ++d )
{
if ( fftOptimization == FFTOptimization.SPEED )
fftSize[ d ] = FftComplex.nfftFast( imageSize[ d ] );
else
fftSize[ d ] = FftComplex.nfftSmall( imageSize[ d ] );
}
return fftSize;
}
@Override
public long getProcessingTime() { return processingTime; }
@Override
public void setNumThreads() { this.numThreads = Runtime.getRuntime().availableProcessors(); }
@Override
public void setNumThreads( final int numThreads ) { this.numThreads = numThreads; }
@Override
public int getNumThreads() { return numThreads; }
@Override
public Img<S> getResult() { return fftImage; }
@Override
public boolean checkInput()
{
if ( errorMessage.length() > 0 )
{
return false;
}
else if ( input == null )
{
errorMessage = "Input image is null";
return false;
}
else
{
return true;
}
}
@Override
public String getErrorMessage() { return errorMessage; }
}