6 import ij.plugin.ContrastEnhancer;
23 private FHT transform;
24 private ImageProcessor filter;
25 private static boolean processStack;
26 private boolean padded;
27 private int originalWidth;
28 private int originalHeight;
29 private int stackSize = 1;
30 private int slice = 1;
32 public void run(String arg) {
34 if (arg.equals(
"redisplay"))
35 {redisplayPowerSpectrum();
return;}
40 if (fht==null && arg.equals(
"inverse")) {
48 if (imp.getRoi()!=null)
54 doInverseTransform(fht, ip);
56 doForewardTransform(fht, ip);
60 void doInverseTransform(FHT fht, ImageProcessor ip) {
63 showStatus(
"Inverse transform");
64 fht.inverseTransform();
65 if (fht.quadrantSwapNeeded)
68 ImageProcessor ip2 = fht;
69 if (fht.originalWidth>0) {
70 fht.setRoi(0, 0, fht.originalWidth, fht.originalHeight);
73 int bitDepth = fht.originalBitDepth>0?fht.originalBitDepth:imp.
getBitDepth();
75 case 8: ip2 = ip2.convertToByte(
false);
break;
76 case 16: ip2 = ip2.convertToShort(
false);
break;
78 showStatus(
"Setting brightness");
79 if (fht.rgb==null || ip2==null) {
83 ColorProcessor rgb = (ColorProcessor)fht.rgb.duplicate();
84 rgb.setBrightness((FloatProcessor)ip2);
90 if (bitDepth!=24 && fht.originalColorModel!=null)
91 ip2.setColorModel(fht.originalColorModel);
93 if (title.startsWith(
"FFT of "))
94 title = title.substring(7, title.length());
95 new ImagePlus(
"Inverse FFT of "+title, ip2).show();
98 public void doForewardTransform(FHT fht, ImageProcessor ip) {
99 showStatus(
"Foreward transform");
101 showStatus(
"Calculating power spectrum");
102 ImageProcessor ps = fht.getPowerSpectrum();
103 ImagePlus imp2 =
new ImagePlus(
"FFT of "+imp.
getTitle(), ps);
105 imp2.setProperty(
"FHT", fht);
108 FHT newFHT(ImageProcessor ip) {
110 if (ip instanceof ColorProcessor) {
111 showStatus(
"Extracting brightness");
112 ImageProcessor ip2 = ((ColorProcessor)ip).getBrightness();
113 fht =
new FHT(pad(ip2));
114 fht.rgb = (ColorProcessor)ip.duplicate();
116 fht =
new FHT(pad(ip));
118 fht.originalWidth = originalWidth;
119 fht.originalHeight = originalHeight;
122 fht.originalColorModel = ip.getColorModel();
126 ImageProcessor pad(ImageProcessor ip) {
127 originalWidth = ip.getWidth();
128 originalHeight = ip.getHeight();
129 int maxN = Math.max(originalWidth, originalHeight);
131 while(i<maxN) i *= 2;
132 if (i==maxN && originalWidth==originalHeight) {
137 showStatus(
"Padding to "+ maxN +
"x" + maxN);
138 ImageStatistics stats = ImageStatistics.getStatistics(ip, MEAN, null);
139 ImageProcessor ip2 = ip.createProcessor(maxN, maxN);
140 ip2.setValue(stats.mean);
142 ip2.insert(ip, 0, 0);
149 void showStatus(String msg) {
151 IJ.showStatus(
"FFT: " + slice+
"/"+stackSize);
156 void doMasking(FHT ip) {
159 float[] fht = (
float[])ip.getPixels();
161 mask = mask.convertToByte(
false);
162 ImageStatistics stats = ImageStatistics.getStatistics(mask, MIN_MAX, null);
163 if (stats.histogram[0]==0 && stats.histogram[255]==0)
165 boolean passMode = stats.histogram[255]!=0;
166 IJ.showStatus(
"Masking: "+(passMode?
"pass":
"filter"));
167 mask = mask.duplicate();
169 changeValues(mask, 0, 254, 0);
171 changeValues(mask, 1, 255, 255);
172 for (
int i=0; i<3; i++)
175 ip.swapQuadrants(mask);
177 byte[] maskPixels = (byte[])mask.getPixels();
178 for (
int i=0; i<fht.length; i++) {
179 fht[i] = (float)(fht[i]*(maskPixels[i]&255)/255.0);
185 void changeValues(ImageProcessor ip,
int v1,
int v2,
int v3) {
186 byte[] pixels = (byte[])ip.getPixels();
189 for (
int i=0; i<pixels.length; i++) {
192 pixels[i] = (byte)v3;
196 public void redisplayPowerSpectrum() {
199 {IJ.showMessage(
"FFT",
"Frequency domain image required");
return;}
200 ImageProcessor ps = fht.getPowerSpectrum();