/Users/lyon/j4p/src/ip/transforms/Gauss.java

1    package ip.transforms; 
2     
3    import math.Mat2; 
4     
5    /** 
6     * User: lyon 
7     * Date: Dec 22, 2002 
8     * Time: 1:53:40 AM 
9     */ 
10   public class Gauss { 
11    
12       public static final double gauss( 
13               double x, double y, 
14               double xc, double yc, double sigma) { 
15           double dx = x - xc; 
16           double dy = y - yc; 
17           double dx2 = dx * dx; 
18           double dy2 = dy * dy; 
19           double sigma2 = sigma * sigma; 
20           double oneOnSigma2 = 1 / sigma2; 
21           return 
22                   Math.exp(-(dx2 + dy2) * 
23                   oneOnSigma2 / 2) / Math.PI * oneOnSigma2 / 2; 
24       } 
25    
26       public static final double gauss( 
27               double x, 
28               double xc, double sigma) { 
29           double oneOnSigmaSquaredOn2 = 1 / (sigma * sigma) / 2; 
30           return 
31                   Math.exp(-((x - xc) * (x - xc)) * 
32                   oneOnSigmaSquaredOn2) / Math.PI * oneOnSigmaSquaredOn2; 
33       } 
34    
35       public static void printGaussKernel( 
36               int M, int N, 
37               double sigma, double centerMax) { 
38           short k[][] = new short[M][N]; 
39           int xc = M / 2; 
40           int yc = N / 2; 
41           double scale = centerMax * 2 * Math.PI * sigma * sigma; 
42           for (int x = 0; x < k.length; x++) 
43               for (int y = 0; y < k[0].length; y++) 
44                   k[x][y] = (short) 
45                           (scale * gauss(x, y, xc, yc, sigma)); 
46           //Mat.printKernel(k,"gauss"+k.length); 
47       } 
48    
49       public static void printGaussKernel( 
50               int M, int N, 
51               double sigma) { 
52           float k[][] = getGaussKernel(M, N, sigma); 
53           Mat2.printKernel(k, "gauss" + k.length); 
54       } 
55    
56       public static float[][] getGaussKernel( 
57               int M, int N, 
58               double sigma) { 
59           float k[][] = new float[M][N]; 
60           int xc = M / 2; 
61           int yc = N / 2; 
62           for (int x = 0; x < k.length; x++) 
63               for (int y = 0; y < k[0].length; y++) 
64                   k[x][y] = (float) gauss(x, y, xc, yc, sigma); 
65           Mat2.normalize(k); 
66           return k; 
67       } 
68    
69       public static double getMagnitudeOfTheDerivativeOfGauss( 
70               double x, double y, 
71               double xc, double yc, double sigma) { 
72           ConvolutionUtils.t2 = Math.pow(2.0 * x - 2.0 * xc, 2.0); 
73           ConvolutionUtils.t3 = sigma * sigma; 
74           ConvolutionUtils.t4 = ConvolutionUtils.t3 * ConvolutionUtils.t3; 
75           ConvolutionUtils.t5 = ConvolutionUtils.t4 * ConvolutionUtils.t4; 
76           ConvolutionUtils.t6 = 1 / ConvolutionUtils.t5; 
77           ConvolutionUtils.t9 = Math.pow(x - xc, 2.0); 
78           ConvolutionUtils.t11 = Math.pow(y - yc, 2.0); 
79           ConvolutionUtils.t16 = Math.pow(Math.exp(-(ConvolutionUtils.t9 + ConvolutionUtils.t11) / ConvolutionUtils.t3 / 2), 2.0); 
80           ConvolutionUtils.t17 = Math.PI * Math.PI; 
81           ConvolutionUtils.t19 = ConvolutionUtils.t16 / ConvolutionUtils.t17; 
82           ConvolutionUtils.t22 = Math.pow(2.0 * y - 2.0 * yc, 2.0); 
83           double ddx = ConvolutionUtils.t2 * ConvolutionUtils.t6 * ConvolutionUtils.t19; 
84           double ddy = ConvolutionUtils.t22 * ConvolutionUtils.t6 * ConvolutionUtils.t19; 
85           ConvolutionUtils.t26 = Math.sqrt(ddx + ddy); 
86           ConvolutionUtils.t27 = ConvolutionUtils.t26 / 4; 
87           return ConvolutionUtils.t27; 
88       } 
89    
90       public static void printMagnitudeOfTheDerivativeOfGauss( 
91               int M, int N, 
92               double sigma) { 
93           float k[][] = getMagnitudeOfTheDerivativeOfGauss(M, N, sigma); 
94           //Mat.printKernel(k,"MagnitudeOfTheDerivativeOfGauss"+k.length); 
95       } 
96    
97       public static float[][] getMagnitudeOfTheDerivativeOfGauss( 
98               int M, int N, 
99               double sigma) { 
100          float k[][] = new float[M][N]; 
101          int xc = M / 2; 
102          int yc = N / 2; 
103          for (int x = 0; x < k.length; x++) 
104              for (int y = 0; y < k[0].length; y++) 
105                  k[x][y] = (float) 
106                          getMagnitudeOfTheDerivativeOfGauss(x, y, xc, yc, sigma); 
107          Mat2.normalize(k); 
108          return k; 
109      } 
110   
111      public static float[][] getGauss3() { 
112          float k[][] = { 
113              {1, 2, 1}, 
114              {2, 4, 2}, 
115              {1, 2, 1} 
116          }; 
117          Mat2.scale(k, 1 / 16.0); 
118          return k; 
119      } 
120   
121      public static float[][] getGauss7() { 
122          float k [][] = { 
123   
124              {1, 1, 2, 2, 2, 1, 1}, 
125              {1, 2, 2, 4, 2, 2, 1}, 
126              {2, 2, 4, 8, 4, 2, 2}, 
127              {2, 4, 8, 16, 8, 4, 2}, 
128              {2, 2, 4, 8, 4, 2, 2}, 
129              {1, 2, 2, 4, 2, 2, 1}, 
130              {1, 1, 2, 2, 2, 1, 1} 
131          }; 
132          Mat2.normalize(k); 
133          return k; 
134      } 
135   
136      public static float[][] getGauss15() { 
137          float k[][] = { 
138              {1.9045144E-7f, 9.671922E-7f, 3.8253193E-6f, 1.1782813E-5f, 2.8265502E-5f, 5.2806907E-5f, 7.6833596E-5f, 8.7063876E-5f, 7.6833596E-5f, 5.2806907E-5f, 2.8265502E-5f, 1.1782813E-5f, 3.8253193E-6f, 9.671922E-7f, 1.9045144E-7f}, 
139              {9.671922E-7f, 4.9118075E-6f, 1.9426576E-5f, 5.9838065E-5f, 1.4354405E-4f, 2.681756E-4f, 3.901932E-4f, 4.4214682E-4f, 3.901932E-4f, 2.681756E-4f, 1.4354405E-4f, 5.9838065E-5f, 1.9426576E-5f, 4.9118075E-6f, 9.671922E-7f}, 
140              {3.8253193E-6f, 1.9426576E-5f, 7.6833596E-5f, 2.3666414E-4f, 5.677278E-4f, 0.0010606551f, 0.001543244f, 0.0017487246f, 0.001543244f, 0.0010606551f, 5.677278E-4f, 2.3666414E-4f, 7.6833596E-5f, 1.9426576E-5f, 3.8253193E-6f}, 
141              {1.1782813E-5f, 5.9838065E-5f, 2.3666414E-4f, 7.2897685E-4f, 0.0017487246f, 0.0032670477f, 0.0047535263f, 0.0053864513f, 0.0047535263f, 0.0032670477f, 0.0017487246f, 7.2897685E-4f, 2.3666414E-4f, 5.9838065E-5f, 1.1782813E-5f}, 
142              {2.8265502E-5f, 1.4354405E-4f, 5.677278E-4f, 0.0017487246f, 0.004194972f, 0.00783724f, 0.011403117f, 0.012921424f, 0.011403117f, 0.00783724f, 0.004194972f, 0.0017487246f, 5.677278E-4f, 1.4354405E-4f, 2.8265502E-5f}, 
143              {5.2806907E-5f, 2.681756E-4f, 0.0010606551f, 0.0032670477f, 0.00783724f, 0.014641892f, 0.021303825f, 0.024140399f, 0.021303825f, 0.014641892f, 0.00783724f, 0.0032670477f, 0.0010606551f, 2.681756E-4f, 5.2806907E-5f}, 
144              {7.6833596E-5f, 3.901932E-4f, 0.001543244f, 0.0047535263f, 0.011403117f, 0.021303825f, 0.030996885f, 0.03512407f, 0.030996885f, 0.021303825f, 0.011403117f, 0.0047535263f, 0.001543244f, 3.901932E-4f, 7.6833596E-5f}, 
145              {8.7063876E-5f, 4.4214682E-4f, 0.0017487246f, 0.0053864513f, 0.012921424f, 0.024140399f, 0.03512407f, 0.039800785f, 0.03512407f, 0.024140399f, 0.012921424f, 0.0053864513f, 0.0017487246f, 4.4214682E-4f, 8.7063876E-5f}, 
146              {7.6833596E-5f, 3.901932E-4f, 0.001543244f, 0.0047535263f, 0.011403117f, 0.021303825f, 0.030996885f, 0.03512407f, 0.030996885f, 0.021303825f, 0.011403117f, 0.0047535263f, 0.001543244f, 3.901932E-4f, 7.6833596E-5f}, 
147              {5.2806907E-5f, 2.681756E-4f, 0.0010606551f, 0.0032670477f, 0.00783724f, 0.014641892f, 0.021303825f, 0.024140399f, 0.021303825f, 0.014641892f, 0.00783724f, 0.0032670477f, 0.0010606551f, 2.681756E-4f, 5.2806907E-5f}, 
148              {2.8265502E-5f, 1.4354405E-4f, 5.677278E-4f, 0.0017487246f, 0.004194972f, 0.00783724f, 0.011403117f, 0.012921424f, 0.011403117f, 0.00783724f, 0.004194972f, 0.0017487246f, 5.677278E-4f, 1.4354405E-4f, 2.8265502E-5f}, 
149              {1.1782813E-5f, 5.9838065E-5f, 2.3666414E-4f, 7.2897685E-4f, 0.0017487246f, 0.0032670477f, 0.0047535263f, 0.0053864513f, 0.0047535263f, 0.0032670477f, 0.0017487246f, 7.2897685E-4f, 2.3666414E-4f, 5.9838065E-5f, 1.1782813E-5f}, 
150              {3.8253193E-6f, 1.9426576E-5f, 7.6833596E-5f, 2.3666414E-4f, 5.677278E-4f, 0.0010606551f, 0.001543244f, 0.0017487246f, 0.001543244f, 0.0010606551f, 5.677278E-4f, 2.3666414E-4f, 7.6833596E-5f, 1.9426576E-5f, 3.8253193E-6f}, 
151              {9.671922E-7f, 4.9118075E-6f, 1.9426576E-5f, 5.9838065E-5f, 1.4354405E-4f, 2.681756E-4f, 3.901932E-4f, 4.4214682E-4f, 3.901932E-4f, 2.681756E-4f, 1.4354405E-4f, 5.9838065E-5f, 1.9426576E-5f, 4.9118075E-6f, 9.671922E-7f}, 
152              {1.9045144E-7f, 9.671922E-7f, 3.8253193E-6f, 1.1782813E-5f, 2.8265502E-5f, 5.2806907E-5f, 7.6833596E-5f, 8.7063876E-5f, 7.6833596E-5f, 5.2806907E-5f, 2.8265502E-5f, 1.1782813E-5f, 3.8253193E-6f, 9.671922E-7f, 1.9045144E-7f}}; 
153  //sum=0.9999999983459134 
154          return k; 
155      } 
156  } 
157