/Users/lyon/j4p/src/ip/gui/MorphUtils.java

1    /* 
2     * Created by DocJava, Inc. 
3     * User: lyon 
4     * Date: Apr 21, 2003 
5     * Time: 8:41:10 AM 
6     */ 
7    package ip.gui; 
8     
9    import j2d.ShortImageBean; 
10    
11   public class MorphUtils { 
12       public static final 
13               float kv[][] = { 
14                   {0, 1, 0}, 
15                   {0, 1, 0}, 
16                   {0, 1, 0} 
17               }; 
18       public static final 
19               float kh[][] = { 
20                   {0, 0, 0}, 
21                   {1, 1, 1}, 
22                   {0, 0, 0} 
23               }; 
24       public static final 
25               float kCross[][] = { 
26                   {0, 1, 0}, 
27                   {1, 1, 1}, 
28                   {0, 1, 0} 
29               }; 
30       public static final 
31               float kSquare[][] = { 
32                   {1, 1, 1}, 
33                   {1, 1, 1}, 
34                   {1, 1, 1} 
35               }; 
36       public static final 
37               float kThinTop[][] = { 
38                   {0, 1, 0}, 
39                   {1, 1, 1}, 
40                   {0, 0, 0} 
41               }; 
42       public static final 
43               float kThinBottom[][] = { 
44                   {0, 0, 0}, 
45                   {1, 1, 1}, 
46                   {0, 1, 0} 
47               }; 
48       public static final 
49               float kOutline[][] = { 
50                   {0, 1, 1, 1, 0}, 
51                   {1, 1, 1, 1, 1}, 
52                   {1, 1, 1, 1, 1}, 
53                   {1, 1, 1, 1, 1}, 
54                   {0, 1, 1, 1, 0} 
55               }; 
56    
57       public static short[][] dilategs(short f[][], float k[][]) { 
58           int uc = k.length / 2; 
59           int vc = k[0].length / 2; 
60           int w = f.length; 
61           int h = f[0].length; 
62    
63           short o[][] = new short[w][h]; 
64           short max = 0; 
65           short sum = 0; 
66    
67           for (int x = uc; x < w - uc; x++) { 
68               for (int y = vc; y < h - vc; y++) { 
69                   max = 0; 
70                   sum = 0; 
71                   for (int v = -vc; v <= vc; v++) 
72                       for (int u = -uc; u <= uc; u++) 
73                           if (k[u + uc][v + vc] == 1) { 
74                               sum = f[x - u][y - v]; 
75                               if (sum > max) 
76                                   max = sum; 
77                           } 
78    
79                   o[x][y] = max; 
80               } 
81           } 
82           return o; 
83       } 
84    
85       public static short[][] dilate(short f[][], float k[][]) { 
86           int uc = k.length / 2; 
87           int vc = k[0].length / 2; 
88           int w = f.length; 
89           int h = f[0].length; 
90           short o[][] = new short[w][h]; 
91           short sum; 
92    
93           for (int x = uc; x < w - uc; x++) { 
94               for (int y = vc; y < h - vc; y++) { 
95                   sum = 0; 
96                   for (int v = -vc; v <= vc; v++) 
97                       for (int u = -uc; u <= uc; u++) 
98                           if (k[u + uc][v + vc] == 1) 
99                               if (f[x - u][y - v] > sum) 
100                                  sum = f[x - u][y - v]; 
101                  o[x][y] = sum; 
102              } 
103          } 
104          return o; 
105      } 
106   
107      public static short redPassSuen(ShortImageBean sib, boolean firstPass) { 
108          boolean p[] = new boolean[8]; 
109          short c = 0; 
110          int w = sib.getWidth(); 
111          int h = sib.getHeight(); 
112          for (int x = 1; x < w - 1; x++) { 
113              for (int y = 1; y < h - 1; y++) { 
114                  sib.getG()[x][y] = 0; // use g for 1st pass 
115                  if (sib.getR()[x][y] == 0) continue; 
116                  p[0] = sib.getR()[x][y + 1] != 0; 
117                  p[1] = sib.getR()[x + 1][y + 1] != 0; 
118                  p[2] = sib.getR()[x + 1][y] != 0; 
119                  p[3] = sib.getR()[x + 1][y - 1] != 0; 
120                  p[4] = sib.getR()[x][y - 1] != 0; 
121                  p[5] = sib.getR()[x - 1][y - 1] != 0; 
122                  p[6] = sib.getR()[x - 1][y] != 0; 
123                  p[7] = sib.getR()[x - 1][y + 1] != 0; 
124                  int n = numberOfNeighbors(p); 
125                  if ((n < 2) || (n > 6)) continue; 
126                  if (numberOf01Transitions(p) != 1) continue; 
127   
128                  if (firstPass) { 
129                      if ((p[0] && p[2] && p[4])) 
130                          continue; 
131                      if ((p[2] && p[4] && p[6])) 
132                          continue; 
133                      sib.getG()[x][y] = 255; 
134                      c++; 
135                  } else { 
136                      if ((p[0] && p[2] && p[6])) 
137                          continue; 
138                      if ((p[0] && p[4] && p[6])) 
139                          continue; 
140                      sib.getG()[x][y] = 255; 
141                      c++; 
142   
143                  } 
144              } 
145          } 
146          return c; 
147      } 
148   
149      public static short[][] erodegs(short f[][], float k[][]) { 
150          int uc = k.length / 2; 
151          int vc = k[0].length / 2; 
152          int w = f.length; 
153          int h = f[0].length; 
154          short o[][] = new short[w][h]; 
155          short min = 0; 
156          short sum = 0; 
157   
158          for (int x = uc; x < w - uc; x++) { 
159              for (int y = vc; y < h - vc; y++) { 
160                  min = 255; 
161                  sum = 0; 
162                  for (int v = -vc; v <= vc; v++) 
163                      for (int u = -uc; u <= uc; u++) 
164                          if (k[u + uc][v + vc] == 1) { 
165                              sum = f[x - u][y - v]; 
166                              if (sum < min) 
167                                  min = sum; 
168                          } 
169                  o[x][y] = min; 
170              } 
171          } 
172          return o; 
173      } 
174   
175      public static int numberOfNeighbors(boolean p[]) { 
176          int n = 0; 
177          if (p[0]) n++; 
178          if (p[1]) n++; 
179          if (p[2]) n++; 
180          if (p[3]) n++; 
181          if (p[4]) n++; 
182          if (p[5]) n++; 
183          if (p[6]) n++; 
184          if (p[7]) n++; 
185          return n; 
186      } 
187   
188      public static int numberOf01Transitions(boolean p[]) { 
189          int n = 0; 
190          if ((!p[0]) && p[1]) n++; 
191          if ((!p[1]) && p[2]) n++; 
192          if ((!p[2]) && p[3]) n++; 
193          if ((!p[3]) && p[4]) n++; 
194          if ((!p[4]) && p[5]) n++; 
195          if ((!p[5]) && p[6]) n++; 
196          if ((!p[6]) && p[7]) n++; 
197          if ((!p[7]) && p[0]) n++; 
198          return n; 
199      } 
200   
201      public static float[][] getKv() { 
202          return kv; 
203      } 
204   
205      public static float[][] getKh() { 
206          return kh; 
207      } 
208   
209      public static float[][] getKcross() { 
210          return kCross; 
211      } 
212   
213      public static float[][] getKsquare() { 
214          return kSquare; 
215      } 
216   
217      public static float[][] getKthintop() { 
218          return kThinTop; 
219      } 
220   
221      public static float[][] getKthinbottom() { 
222          return kThinBottom; 
223      } 
224   
225      public static float[][] getKoutline() { 
226          return kOutline; 
227      } 
228   
229      public static short[][] erode(short f[][], float k[][]) { 
230          int uc = k.length / 2; 
231          int vc = k[0].length / 2; 
232          int w = f.length; 
233          int h = f[0].length; 
234   
235          short o[][] = new short[w][h]; 
236          short sum = 0; 
237   
238          for (int x = uc; x < w - uc; x++) { 
239              for (int y = vc; y < h - vc; y++) { 
240                  sum = 255; 
241                  for (int v = -vc; v <= vc; v++) 
242                      for (int u = -uc; u <= uc; u++) 
243                          if (k[u + uc][v + vc] == 1) 
244                              if (f[x - u][y - v] < sum) 
245                                  sum = f[x - u][y - v]; 
246                  o[x][y] = sum; 
247              } 
248          } 
249          return o; 
250      } 
251  } 
252