Class AlgorithmFrequencyFilter

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.awt.event.WindowListener, java.lang.Runnable, java.util.EventListener

    public class AlgorithmFrequencyFilter
    extends AlgorithmBase
    AlgorithmFrequencyFilter.java.
    Author:
    William Gandler and Matthew J. McAuliffe Processing images by filtering in the frequency domain is a 3 step process: 1.) Performing a forward fast fourier transform to convert a spatial image into its frequency image. 2.) Enhancing some frequency components of the image and attenuating other frequency components of the image by mulitplying by a lowpass, highpass, bandpass, or bandstop filter. Frequency filters may be constructed with 1 of 3 methods - finite impulse response filters constructed with Hamming windows, Gaussian filters, Butterworth, and Chebyshev filters. However, for the Gaussian filters only lowpass and highpass filters are available. 3.) Performing an inverse fast fourier transform to convert from the frequency domain back into the spatial domain. This software module performs all 3 steps in one combined process. AlgorithmFFT is used to perform the steps one at a time and examine the Frequency space images.

    The module also creates a Gabor filter, which is essentially a tilted Gaussian with 2 unequal axes at an offset (freqU, freqV) from the origin. A Gabor filter only responds to a texture having both a particular frequency and a particular orientation. Note that a filter and its mirror image reflected across the u and v frequency axes produce identical frequency responses.

    This module also performs homomorphic fitering in which if any data is zero or negative a positive constant is added to all pixels to give a minimum value of 1. Then the log of the data is taken, the fast fourier transform is performed, the high frequencies are multiplied by a gain greater than 1 while the low frequencies are multiplied by a gain less than 1, the inverse fast fourier transform is performed, and the exponential of the the data is taken. Optionally, a specified percentage of the data can be clamped at the low end and/or at the high end. Finally, the data is rescaled so that the original minimum and maximum values are achieved. Only Butterworth filters are used with homomorphic filters in this module. References on homomorphic filters: 1.) Digital Image Processing Second Edition by Rafael C. Gonzalez and Richard E. Woods, Prentice-Hall, Inc., 2002, Chapter 4.5, pp. 191-194. 2.) "Butterworth equations for homomorphic filtering of images" by Holger. G. Adelmann, Computers in Medicine and Biology, Vol. 28, 1998, pp. 169-181.

    The core algorithm of this module, the fast fourier transform algorithm found in exec(), requires that all the dimensions of an N-dimensional dataset be powers of 2. To be able to use this algorithm on datasets with arbitrary dimensions, the data is zero padded to powers of 2 before applying the forward fast fourier transform and stripped down to the original dimensions after applying the inverse fast fourier transform. The powers of 2 are not kept identical because symmetrical Fourier pictures are not required.

    The typical full sequence is as follows: 1.) Data from a real spatial image is exported into a float array realData. 2.) An equally sized float array called imagData is created and filled with zeros. 3.) realData and imagData are enlarged to the powers of two. If finite impulse repsonse filters are constructed with Hamming windows and no cropping, the new dimension size is equal to the minimum power of two number that equals or exceeds the original dimension size + kDim - 1, where kDim is the diameter of a circular or spherical convolution kernel. If finite impulse response filters with Hamming windows and cropping or infinite impulse response Gaussian or Butterworth or Chebyshev filters are used, the new dimension size is equal to the minimum power of two number that equals or exceeds the original dimension size. The data is padded with zeros at the end of each dimension. 4.) exec() is invoked to run the fast fourier transform algorithm. 5.) If Butterworth or Gaussian or Gabor or Chebyshev filters are used, the center algorithm is invoked to reorder the data so that frequencies with the lowest magnitudes are at the center. If finite impulse response filters with windows are used: 6a.) An ideal filter kernel is constructed in the spatial domain. 7a.) A Hamming window kernel is constructed in the spatial domain. 8a.) The ideal kernel and the Hamming window kernel are multiplied together. 9a.) The kernel is zero padded up to the same dimensions that the image data was. 10a.) exec() is run to obtain the FFT of the kernel. If Gaussian or Butterworth or Chebyshev filters are used: For Gaussian and Butterworth and Chebyshev filters the transfer functions affect the real and imaginary parts of the FFT of the image in exactly the same manner. These filters are zero-phase- shift filters because they do not alter the phase of the transform. Since these filters are conjugate symmetric, the inverse FFTs of these filters are purely real. Since this filtering is equivalent to convolving to real 2D data sets, the result must be purely real. 6b.) The real part of the FFT is set equal to the appropriate filter magnitude and the imaginary part is set equal to zero. This filter has the same dimensions as the padded image data. 11.) The data FFT is set equal to the product of the data FFT and the filter FFT. 12.) The inverse FFT process is invoked. The complex data is exported into the 2 float arrays realData and imagData. There should be no need for zero padding at this point since the dimensions should already be all powers of 2 from before. 13.) If Butterworth or Gaussian or Chebyshev filters are used, the center() routine is invoked to restore the data to its original ordering. 14.) exec() is invoked to run the inverse fast fourier transform algorithm. 15.) The realData now holds the correct response if Gaussian or Butterworth or Chebyshev filtering was used. If FIR filtering with windows was used then realData holds a version of the correct response shifted by (kDim - 1)/2 toward the end of each dimension. The imagData should contain only roundoff error. 16.) For FIR filters shift the data back by (kDim - 1)/2 toward the start of each dimension. 17.) Stripping is performed to return the image to its original dimensions. 18.) The realData() is imported into the the new spatial image.

    Methods included calculate the magnitude and phase as shown below:

    magnitude = ( (realData)^2 + (imagData)^2 )^(1/2); phase = arctan(imagData/realData);

    • Field Detail

      • arrayLength

        private int arrayLength
        DOCUMENT ME!
      • filterOrder

        private int filterOrder
        DOCUMENT ME!
      • constructionMethod

        private int constructionMethod
        DOCUMENT ME!
      • createGabor

        private boolean createGabor
        DOCUMENT ME!
      • dimLengths

        private int[] dimLengths
        DOCUMENT ME!
      • doCrop

        private boolean doCrop
        DOCUMENT ME!
      • end

        private int[] end
        DOCUMENT ME!
      • f1

        private double f1
        DOCUMENT ME!
      • f2

        private double f2
        DOCUMENT ME!
      • filterType

        private int filterType
        DOCUMENT ME!
      • finalData

        private float[] finalData
        DOCUMENT ME!
      • freqU

        private float freqU
        Variables used in Gabor filter.
      • freqV

        private float freqV
        DOCUMENT ME!
      • highGain

        private float highGain
        DOCUMENT ME!
      • highTruncated

        private float highTruncated
        DOCUMENT ME!
      • iKernel

        private float[] iKernel
        DOCUMENT ME!
      • imagData

        private float[] imagData
        DOCUMENT ME!
      • image25D

        private boolean image25D
        DOCUMENT ME!
      • imageCrop

        private boolean imageCrop
        DOCUMENT ME!
      • imagKernelData

        private float[] imagKernelData
        DOCUMENT ME!
      • kDim

        private int kDim
        DOCUMENT ME!
      • lowGain

        private float lowGain
        DOCUMENT ME!
      • lowTruncated

        private float lowTruncated
        DOCUMENT ME!
      • magData

        private float[] magData
        DOCUMENT ME!
      • minimum

        private float minimum
        DOCUMENT ME!
      • maximum

        private float maximum
        DOCUMENT ME!
      • ndim

        private int ndim
        DOCUMENT ME!
      • newArrayLength

        private int newArrayLength
        DOCUMENT ME!
      • newDimLengths

        private int[] newDimLengths
        DOCUMENT ME!
      • newSliceSize

        private int newSliceSize
        DOCUMENT ME!
      • phaseData

        private float[] phaseData
        DOCUMENT ME!
      • realData

        private float[] realData
        DOCUMENT ME!
      • realKernelData

        private float[] realKernelData
        DOCUMENT ME!
      • sData

        private byte[] sData
        DOCUMENT ME!
      • sigmaU

        private float sigmaU
        DOCUMENT ME!
      • sigmaV

        private float sigmaV
        DOCUMENT ME!
      • start

        private int[] start
        DOCUMENT ME!
      • theta

        private float theta
        DOCUMENT ME!
      • transformDir

        private int transformDir
        DOCUMENT ME!
      • wKernel

        private float[] wKernel
        DOCUMENT ME!
      • zeroPad

        private boolean zeroPad
        DOCUMENT ME!
      • epsilon

        private double epsilon
      • rs

        private double rs
      • onlyFrequencyFilter

        private boolean onlyFrequencyFilter
    • Constructor Detail

      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage srcImg,
                                        boolean image25D,
                                        boolean imageCrop)
        Constructor used in Laplacian medialness option of live wire cost function.
        Parameters:
        srcImg - source image model
        image25D - if true processes each slice of a 3D image independently
        imageCrop - if true crop image if largest image dimension + kDim - 1 exceeds the smallest power of 2 number >= the largest dimension
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage srcImg,
                                        float freqU,
                                        float freqV,
                                        float sigmaU,
                                        float sigmaV,
                                        float theta,
                                        boolean createGabor)
        Constructor used for Gabor transform.
        Parameters:
        srcImg - Source image model
        freqU - Frequency along U axis before rotation by theta range -1 to 1
        freqV - Frequency along V axis before rotation by theta range -1 to 1
        sigmaU - Standard deviation along prerotated U axis
        sigmaV - Standard deviation along prerotated V axis
        theta - Rotation in radians
        createGabor - If true, produce an image of the Gabor filter
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage srcImg,
                                        boolean image25D,
                                        float freq1,
                                        int filterOrder,
                                        float lowGain,
                                        float highGain,
                                        float lowTruncated,
                                        float highTruncated)
        This constructor is only used for HOMOMORPHIC filters.
        Parameters:
        srcImg - source image model
        image25D - If true, processes each slice of a 3D image independently
        freq1 - cutoff frequency, transition frequency from low to high gain
        filterOrder - order of the Butterworth filter
        lowGain -
        highGain - > 1, gain at high frequencies
        lowTruncated - part of low histogram end truncated
        highTruncated - part of high histogram end truncated
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage destImg,
                                        ModelImage srcImg,
                                        float freqU,
                                        float freqV,
                                        float sigmaU,
                                        float sigmaV,
                                        float theta,
                                        boolean createGabor)
        Constructor used for Gabor transform.
        Parameters:
        destImg - Destination image model
        srcImg - Source image model
        freqU - Frequency along U axis before rotation by theta range -1 to 1
        freqV - Frequency along V axis before rotation by theta range -1 to 1
        sigmaU - Standard deviation along prerotated U axis
        sigmaV - Standard deviation along prerotated V axis
        theta - Rotation in radians
        createGabor - If true, produce an image of the Gabor filter
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage destImg,
                                        ModelImage srcImg,
                                        boolean image25D,
                                        float freq1,
                                        int filterOrder,
                                        float lowGain,
                                        float highGain,
                                        float lowTruncated,
                                        float highTruncated)
        This constructor is only used for HOMOMORPHIC filters.
        Parameters:
        destImg - image model where result image is to be stored
        srcImg - source image model
        image25D - If true, processes each slice of a 3D image independently
        freq1 - cutoff frequency, transition frequency from low to high gain
        filterOrder - order of the Butterworth filter
        lowGain -
        highGain - > 1, gain at high frequencies
        lowTruncated - part of low histogram end truncated
        highTruncated - part of high histogram end truncated
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage srcImg,
                                        boolean image25D,
                                        boolean imageCrop,
                                        int kernelDiameter,
                                        int filterType,
                                        double freq1,
                                        double freq2,
                                        int constructionMethod,
                                        int filterOrder,
                                        double epsilon,
                                        double rs,
                                        boolean onlyFrequencyFilter)
        Creates a new AlgorithmFrequencyFilter object.
        Parameters:
        srcImg - source image model
        image25D - if true processes each slice of a 3D image independently
        imageCrop - if true crop image if largest image dimension + kDim - 1 exceeds the smallest power of 2 number >= the largest dimension
        kernelDiameter - convolution kernel diameter - must be an odd integer >= 3
        filterType - LOWPASS, HIGHPASS, BANDPASS, or BANDSTOP
        freq1 - cutoff frequency in LOWPASS and HIGHPASS lower frequency in BANDPASS and BANDSTOP
        freq2 - higher frequency in BANDPASS and BANDSTOP User inputs f1 and f2 going from 0.0 to 1.0. Program multiplies these numbers by PI for FIR filters.
        constructionMethod - WINDOW for window finite impulse response, GAUSSIAN, or BUTTERWORTH
        filterOrder - order of a Butterworth or Chebyshev filter
        epsilon - Maximum Chebyshev filter ripple or Elliptic passband ripple in decibels
        rs - decibels stopband is down in Elliptic
        onlyFrequencyFilter -
      • AlgorithmFrequencyFilter

        public AlgorithmFrequencyFilter​(ModelImage destImg,
                                        ModelImage srcImg,
                                        boolean image25D,
                                        boolean imageCrop,
                                        int kernelDiameter,
                                        int filterType,
                                        double freq1,
                                        double freq2,
                                        int constructionMethod,
                                        int filterOrder,
                                        double epsilon,
                                        double rs,
                                        boolean onlyFrequencyFilter)
        Creates a new AlgorithmFrequencyFilter object.
        Parameters:
        destImg - image model where result image is to be stored
        srcImg - source image model
        image25D - if true processes each slice of a 3D image independently
        imageCrop - if true crop image if largest image dimension + kDim - 1 exceeds the smallest power of 2 number >= the largest dimension
        kernelDiameter - convolution kernel diameter - must be an odd integer >= 3
        filterType - LOWPASS, HIGHPASS, BANDPASS, or BANDSTOP
        freq1 - cutoff frequency in LOWPASS and HIGHPASS lower frequency in BANDPASS and BANDSTOP
        freq2 - higher frequency in BANDPASS and BANDSTOP User inputs f1 and f2 going from 0.0 to 1.0. Program multiplies these numbers by PI for FIR filters.
        constructionMethod - WINDOW for window finite impulse response, GAUSSIAN, or BUTTERWORTH
        filterOrder - order of the Butterworth or Chebyshev filter
        epsilon - Maximum Chebyshev filter ripple or Elliptic passband ripple in decibels
        rs - decibels stopband is down in Elliptic
        onlyFrequencyFilter -
    • Method Detail

      • finalize

        public void finalize()
        Prepares this class for destruction.
        Overrides:
        finalize in class AlgorithmBase
      • getImaginaryData

        public float[] getImaginaryData()
        Returns reference to imaginary data array.
        Returns:
        the reference the the imaginary datat array
      • getMagData

        public float[] getMagData()
        Returns reference to magnitude data array.
        Returns:
        the reference to the magnitude data array
      • getPhaseData

        public float[] getPhaseData()
        Returns reference to phase data array.
        Returns:
        the reference to the phase data array
      • getRealData

        public float[] getRealData()
        Returns reference to real data array.
        Returns:
        the reference the the real datat array
      • getSData

        public byte[] getSData()
        DOCUMENT ME!
        Returns:
        sData
      • getGabor

        public ModelImage getGabor()
        DOCUMENT ME!
        Returns:
        sData
      • calcInPlace

        private void calcInPlace()
        This function replaces the original image with a new image that is either the FFT, the filtered FFT, or the inverse FFT of the original or filtered image.
      • calcStoreInDest

        private void calcStoreInDest()
        This function produces a new image that is either the FFT, the filtered FFT, or the inverse FFT of the original or filtered image.
      • center

        private void center​(float[] rData,
                            float[] iData)
        Centers the FFT for display purposes.
        Parameters:
        rData - real data buffer
        iData - imaginary data buffer
      • edgeStrip

        private void edgeStrip​(float[] rData,
                               int z)
        This function edge strips the realData after the inverse FFT to return to the original dimensions that were present before the forward FFT was performed.
        Parameters:
        rData - DOCUMENT ME!
        z - DOCUMENT ME!
      • exec

        private void exec​(float[] rData,
                          float[] iData,
                          int z)
        This is the method that calculates the FFT Perform a data centering operation after the forward FFT Perform a data centering operation before the inverse FFT Note that a frequency filter operation performs a forward FFT.
        Parameters:
        rData - real data buffer
        iData - imaginary data buffer
        z - slice number in image25D processing, 0 otherwise
      • hammingKernel2D

        private void hammingKernel2D()
        Builds a hamming kernel in 2 dimensions.
      • hammingKernel3D

        private void hammingKernel3D()
        Builds a hamming kernel in 3 dimensions.
      • idealBPKernel2D

        private void idealBPKernel2D​(double FLow,
                                     double FHigh)
        Builds the idealized bandpass kernel in 2 dimensions.
        Parameters:
        FLow - defines the cutoff frequecy at low freqs.
        FHigh - defines the cutoff frequecy at high freqs.
      • idealBPKernel3D

        private void idealBPKernel3D​(double FLow,
                                     double FHigh)
        Builds the idealized bandpass kernel in 2 dimensions.
        Parameters:
        FLow - defines the cutoff frequecy at low freqs.
        FHigh - defines the cutoff frequecy at high freqs.
      • idealBSKernel2D

        private void idealBSKernel2D​(double FLow,
                                     double FHigh)
        Builds the idealized bandstop kernel in 2 dimensions.
        Parameters:
        FLow - defines the cutoff frequecy at low freqs.
        FHigh - defines the cutoff frequecy at high freqs.
      • idealBSKernel3D

        private void idealBSKernel3D​(double FLow,
                                     double FHigh)
        Builds the idealized bandstop kernel in 3 dimensions.
        Parameters:
        FLow - defines the cutoff frequecy at low freqs.
        FHigh - defines the cutoff frequecy at high freqs.
      • idealHPKernel2D

        private void idealHPKernel2D​(double cutoffFreq)
        Builds the idealized highpass kernel in 2 dimensions.
        Parameters:
        cutoffFreq - defines the cutoff frequecy
      • idealHPKernel3D

        private void idealHPKernel3D​(double cutoffFreq)
        Builds the idealized highpass kernel in 3 dimensions.
        Parameters:
        cutoffFreq - defines the cutoff frequecy
      • idealLPKernel2D

        private void idealLPKernel2D​(double cutoffFreq)
        Builds the idealized lowpass kernel in 2 dimensions.
        Parameters:
        cutoffFreq - defines the cutoff frequecy
      • idealLPKernel3D

        private void idealLPKernel3D​(double cutoffFreq)
        Builds the idealized lowpass kernel in 3 dimensions.
        Parameters:
        cutoffFreq - defines the cutoff frequecy
      • magnitude

        private void magnitude()
        Calculates magnitude from real and imaginary parts magnitude = ( (realData)^2 + (imagData)^2 )^(1/2);
      • makeEllipticFilter

        private void makeEllipticFilter​(double fr1,
                                        double fr2)
      • Chebyshev

        private double Chebyshev​(int order,
                                 double w)
      • makeChebyshevTypeIFilter

        private void makeChebyshevTypeIFilter​(double fr1,
                                              double fr2)
      • makeChebyshevTypeIIFilter

        private void makeChebyshevTypeIIFilter​(double fr1,
                                               double fr2)
      • makeButterworthFilter

        private void makeButterworthFilter​(double fr1,
                                           double fr2)
        DOCUMENT ME!
        Parameters:
        fr1 - DOCUMENT ME!
        fr2 - DOCUMENT ME!
      • makeComplexData

        private void makeComplexData()
        makeComplexData -
      • makeGaborFilter

        private void makeGaborFilter​(float freqU,
                                     float freqV,
                                     float sigmaU,
                                     float sigmaV,
                                     float theta,
                                     boolean createGabor)
        DOCUMENT ME!
        Parameters:
        freqU - DOCUMENT ME!
        freqV - DOCUMENT ME!
        sigmaU - DOCUMENT ME!
        sigmaV - DOCUMENT ME!
        theta - DOCUMENT ME!
        createGabor - DOCUMENT ME!
      • makeGaussianFilter

        private void makeGaussianFilter​(double rmsFreq)
        DOCUMENT ME!
        Parameters:
        rmsFreq - DOCUMENT ME!
      • makeHomomorphicFilter

        private void makeHomomorphicFilter​(double fr1)
        DOCUMENT ME!
        Parameters:
        fr1 - DOCUMENT ME!
      • makeKernelData

        private void makeKernelData()
        sets the kernel variables.
      • phase

        private void phase​(float[] phase)
        Calculates phase from real and imaginary parts.
        Parameters:
        phase - = arctan(imagData/realData);
      • restoreFinalData

        private void restoreFinalData()
        DOCUMENT ME!
      • shiftBack

        private void shiftBack​(float[] rData)
        Shifts the FFT data back from the center for processing purposes and inverse FFT.
        Parameters:
        rData - DOCUMENT ME!
      • zeroAround

        private void zeroAround()
        If a cropping operation was performed, shift the data back to the center and put zeros around the edges.