/Users/lyon/j4p/src/j2d/edge/gabor/GaborCanvas.java
|
1 package j2d.edge.gabor;
2
3 import math.Mat2;
4
5 import java.awt.*;
6 import java.awt.image.MemoryImageSource;
7
8 class GaborCanvas extends FilterCanvas {
9 private Image image;
10 private int[] spatialGaborIntArray = null;
11 private int[] frequencyGaborIntArray = null;
12 private int lat = 64;
13 private int sz = lat;
14
15 public GaborCanvas() {
16
17 }
18
19 public float[] getKernelf() {
20 float k[] = new float[spatialGaborIntArray.length];
21 for (int i = 0; i < k.length; i++)
22 k[i] = spatialGaborIntArray[i] / 255.0f;
23 return k;
24 }
25
26 public float[][] getKernel() {
27 short s[][] = j2d.ImageUtils.getGreenFromImage(image, this);
28 float f[][] = Mat2.normalize(s);
29 Mat2.scale(f, 2.0);
30 return f;
31 }
32
33 public void drawImage(String string) {
34 if (string.equals("space"))
35 image = getImage(spatialGaborIntArray);
36 if (string.equals("fourier"))
37 image = getImage(frequencyGaborIntArray);
38 this.repaint();
39 }
40
41 public Image getImage() {
42 return image;
43 }
44
45 public void setImage(Image img) {
46 image = img;
47 }
48
49 public Image getImage(int ia[]) {
50 return this.createImage(new MemoryImageSource(sz, sz,
51 ia,
52 0, sz));
53 }
54
55 public void paint(Graphics graphics) {
56 if (image != null)
57 graphics.drawImage(image, 0, 0, getWidth(), getHeight(), null);
58 }
59
60 // DrawGabor.ComputeImageArrays(size, lambda_val, theta_val, phi_val,
61 // gamma_val, sigma_val);
62 public void ComputeImageArrays
63 (int kernelSize, double lambda,
64 double theta, double phi, double gamma, double sigma) {
65 theta = theta * Math.PI / 180.0;
66 spatialGaborIntArray = new int[kernelSize * kernelSize];
67 frequencyGaborIntArray = new int[kernelSize * kernelSize];
68 sz = kernelSize;
69 double sigmaSquared = sigma * sigma;
70 // - 2 PI **2 = -19.7...
71 double sigmaSquaredOnSizeSquared = -19.739208802178716 * sigmaSquared /
72 (double) (kernelSize * kernelSize);
73 double sizeOnLambda = (double) kernelSize / lambda;
74 double sineTheta = Math.sin(theta);
75 double cosTheta = Math.cos(theta);
76 double twoPi = 2 * Math.PI;
77 double gammaSquared = gamma * gamma;
78 //I am guessing that these are IEEE TIP V 8 No 10 variables, but I am not sure,
79 // - DL
80 for (int xMinusU = -kernelSize / 2; xMinusU < kernelSize / 2; xMinusU++) {
81 for (int yMinusV = -kernelSize / 2; yMinusV < kernelSize / 2; yMinusV++) {
82 double U = (double) xMinusU * cosTheta - (double) yMinusV * sineTheta;
83 double V = (double) xMinusU * sineTheta + (double) yMinusV * cosTheta;
84 double vSquared = V * V;
85 double uSquared = U * U;
86 // This is the g of eq 2...? DL.
87 computeGabor(uSquared, gammaSquared, vSquared, sigmaSquared,
88 twoPi, U, lambda, phi,
89 yMinusV, kernelSize, xMinusU);
90 //computeFourier(sigmaSquaredOnSizeSquared, U,
91 // sizeOnLambda, vSquared,
92 // gammaSquared, yMinusV, kernelSize, xMinusU);
93 }
94 }
95 }
96
97 private void computeGabor(double uSquared,
98 double gammaSquared,
99 double vSquared,
100 double sigmaSquared,
101 double twoPi,
102 double U,
103 double lambda,
104 double phi,
105 int yMinusV,
106 int size,
107 int xMinusU) {
108 double g
109 = Math.exp(-(uSquared + gammaSquared * vSquared)
110 / (2.0 * sigmaSquared)) *
111 Math.cos(twoPi * U / lambda + phi);
112 int gScaled = (int) Math.round((1.0 + g) / 2.0 * 255.0);
113 spatialGaborIntArray[getArrayIndex(yMinusV, size, xMinusU)]
114 = packPixel(gScaled);
115 }
116
117 private void computeFourier(double sigmaSquaredOnSizeSquared,
118 double U, double sizeOnLambda,
119 double Vsquared, double gammaSquared,
120 int yMinusV, int size, int xMinusU) {
121 double g;
122 int gScaled;
123 g = (Math.exp(sigmaSquaredOnSizeSquared * ((U - sizeOnLambda)
124 * (U - sizeOnLambda)
125 + Vsquared / gammaSquared))
126 + Math.exp(sigmaSquaredOnSizeSquared * ((U + sizeOnLambda)
127 * (U + sizeOnLambda)
128 + Vsquared / gammaSquared)));
129 gScaled = (int) Math.round(g * 255.0);
130 if (g > 1.0)
131 gScaled = 255;
132 frequencyGaborIntArray[getArrayIndex(yMinusV, size, xMinusU)]
133 = packPixel(gScaled);
134 }
135
136 private static int packPixel(int gScaled) {
137 return gScaled + (gScaled << 8) + (gScaled << 16) + -16777216;
138 }
139
140 private static int getArrayIndex(int yMinusEta, int size, int xMinusPsi) {
141 return (yMinusEta + size / 2) * size + xMinusPsi + size / 2;
142 }
143 }
144