File: FFTMath.java

package info (click to toggle)
imagej 1.46a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: java: 89,778; sh: 311; xml: 51; makefile: 6
file content (137 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (2)
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
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.text.*;
import java.awt.*;
import java.io.*;

/** The class implements the Process/FFT/Math command. */
public class FFTMath implements PlugIn {

    private static final int CONJUGATE_MULTIPLY=0, MULTIPLY=1, DIVIDE=2;
    private static String[] ops = {"Correlate", "Convolve", "Deconvolve"};
    private static int index1;
    private static int index2;
    private static int operation = CONJUGATE_MULTIPLY;
    private static boolean doInverse = true;
    private static String title = "Result";
    private ImagePlus imp1, imp2;
            
    public void run(String arg) {
        if (showDialog())
            doMath(imp1, imp2);
    }
    
    public boolean showDialog() {
        int[] wList = WindowManager.getIDList();
        if (wList==null) {
            IJ.noImage();
            return false;
        }
        String[] titles = new String[wList.length];
        for (int i=0; i<wList.length; i++) {
            ImagePlus imp = WindowManager.getImage(wList[i]);
            if (imp!=null)
                titles[i] = imp.getTitle();
            else
                titles[i] = "";
        }
        if (index1>=titles.length)index1 = 0;
        if (index2>=titles.length)index2 = 0;
        GenericDialog gd = new GenericDialog("FFT Math");
        gd.addChoice("Image1: ", titles, titles[index1]);
        gd.addChoice("Operation:", ops, ops[operation]);
        gd.addChoice("Image2: ", titles, titles[index2]);
        gd.addStringField("Result:", title);
        gd.addCheckbox("Do inverse transform", doInverse);
        gd.addHelp(IJ.URL+"/docs/menus/process.html#fft-math");
        gd.showDialog();
        if (gd.wasCanceled())
            return false;
        index1 = gd.getNextChoiceIndex();
        operation = gd.getNextChoiceIndex();
        index2 = gd.getNextChoiceIndex();
        title = gd.getNextString();
        doInverse = gd.getNextBoolean();
        String title1 = titles[index1];
        String title2 = titles[index2];
        imp1 = WindowManager.getImage(wList[index1]);
        imp2 = WindowManager.getImage(wList[index2]);
        return true;
   }
    
    public void doMath(ImagePlus imp1, ImagePlus imp2) {
    	FHT h1, h2=null;
    	ImageProcessor fht1, fht2;
		fht1  = (ImageProcessor)imp1.getProperty("FHT");
		if (fht1!=null)
			h1 = new FHT(fht1);
		else {
			IJ.showStatus("Converting to float");
       		ImageProcessor ip1 = imp1.getProcessor();
       	 	h1 = new FHT(ip1);
       	}
		fht2  = (ImageProcessor)imp2.getProperty("FHT");
		if (fht2!=null)
			h2 = new FHT(fht2);
		else {
        	ImageProcessor ip2 = imp2.getProcessor();
        	if (imp2!=imp1)
       	 		h2 = new FHT(ip2);
       	}
        if (!h1.powerOf2Size()) {
        	IJ.error("FFT Math", "Images must be a power of 2 size (256x256, 512x512, etc.)");
        	return;
        }
        if (imp1.getWidth()!=imp2.getWidth()) {
        	IJ.error("FFT Math", "Images must be the same size");
        	return;
        }
		if (fht1==null) {
			IJ.showStatus("Transform image1");
			h1.transform();
		}
		if (fht2==null) {
			if (h2==null)
				h2 = new FHT(h1.duplicate());
				else {
					IJ.showStatus("Transform image2");
					h2.transform();
				}
		}
		FHT result=null;
		switch (operation) {
			case CONJUGATE_MULTIPLY: 
				IJ.showStatus("Complex conjugate multiply");
				result = h1.conjugateMultiply(h2); 
				break;
			case MULTIPLY: 
				IJ.showStatus("Fourier domain multiply");
				result = h1.multiply(h2); 
				break;
			case DIVIDE: 
				IJ.showStatus("Fourier domain divide");
				result = h1.divide(h2); 
				break;
		}
		if (doInverse) {
			IJ.showStatus("Inverse transform");
			result.inverseTransform();
			IJ.showStatus("Swap quadrants");
			result.swapQuadrants();
			IJ.showStatus("Display image");
			result.resetMinAndMax();
        	new ImagePlus(title, result).show();
		} else {
			IJ.showStatus("Power spectrum");
			ImageProcessor ps = result.getPowerSpectrum();
			ImagePlus imp3 = new ImagePlus(title, ps.convertToFloat());
			result.quadrantSwapNeeded = true;
			imp3.setProperty("FHT", result);
        	imp3.show();
		}
		IJ.showProgress(1.0);
    }
 
}