Re: ??fht.swapQuadrant

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: ??fht.swapQuadrant

Roger Bourne
Hi All

Some time ago I downloaded the "LowpassFilters"
(Ideal/Butterworth/Gaussian) plugin from the web.  More recently it
appears to have disappeared from the filter plugins page.

It seems to work OK except the display of the generated filter has the
quadrants switched - as if an fftshift (MatLab terminology) has been
applied.
Can I fix this by inserting fht.swapQuadrant?

The following is 2 excerpts from the original java code.

Thanks in advance,
Roger Bourne
Sydney University

--------------------------------------------------------------------------------
// shows the power spectrum and filters the image
    public void filtering(ImageProcessor ip, ImagePlus imp)
    {
        int maxN = Math.max(M, N);
        size = 2;
        while(size<maxN) size *= 2;
        IJ.runPlugIn("ij.plugin.FFT", "forward");
        h = Math.round((size-N)/2);
        w = Math.round((size-M)/2);
        ImageProcessor ip2 = ip.createProcessor(size, size);  //
processor of the padded image
        ip2.fill();
        ip2.insert(ip, w, h);
                if (ip instanceof ColorProcessor)
                {
            ImageProcessor bright = ((ColorProcessor)ip).getBrightness();
                        fht = new FHT(bright);
            fht.rgb = (ColorProcessor)ip.duplicate(); // get a
duplication of brightness in order to add it after filtering
        }
                else  fht = new FHT(ip2);

        fht.originalColorModel = ip.getColorModel();
        fht.originalBitDepth = imp.getBitDepth();
                fht.transform();    // calculates the Fourier transformation

                if (filter.equals("Ideal"))        ipFilter = Ideal();
                if (filter.equals("Butterworth"))  ipFilter =
Butterworth(order);
                if (filter.equals("Gaussian"))     ipFilter = Gaussian();

                fht.swapQuadrants(ipFilter);
                byte[] pixels_id = (byte[])ipFilter.getPixels();
                float[] pixels_fht = (float[])fht.getPixels();

                for (int i=0; i<size*size; i++)
        {
            pixels_fht[i] = (float)(pixels_fht[i]*(pixels_id[i]&255)/255.0);
        }

                mask = fht.getPowerSpectrum();
                ImagePlus imp2 = new ImagePlus("inverse FFT of
"+imp.getTitle(), mask);
                imp2.setProperty("FHT", fht);
                imp2.setCalibration(imp.getCalibration());
                doInverseTransform(fht);
    }
-----------------------------------------<snip>---------------------------------------

// creates a Butterworth lowpass filter
        public ByteProcessor Butterworth(int n)
        {
                ByteProcessor ip = new ByteProcessor(M,N);
                double value = 0;
                double distance = 0;
                int xcenter = (M/2)+1;
                int ycenter = (N/2)+1;

                for (int y = 0; y < N; y++)
                {
                        for (int x = 0; x < M; x++)
                        {
                                distance =
Math.abs(x-xcenter)*Math.abs(x-xcenter)+Math.abs(y-ycenter)*Math.abs(y-ycenter);
                                distance = Math.sqrt(distance);
                                double parz =
Math.pow(distance/threshold,2*n);
                                value = 255*(1/(1+parz));
                                ip.putPixelValue(x,y,value);
                  }
                }

                ByteProcessor ip2 = new ByteProcessor(size,size);
                ip2.fill();
                ip2.insert(ip, w, h);
                if (displayFilter) new ImagePlus("Butterworth filter",
ip2).show();
                return ip2;
        }