/Users/lyon/j4p/src/ip/gui/frames/MorphFrame.java

1    package ip.gui.frames; 
2     
3    import ip.gui.MorphUtils; 
4    import j2d.ShortImageBean; 
5    import math.Mat2; 
6     
7    import java.awt.*; 
8    import java.awt.event.ActionEvent; 
9     
10   public class MorphFrame extends EdgeFrame { 
11    
12    
13       private Menu morphMenu = getMenu("Morph"); 
14       private Menu morphEdgeMenu = getMenu("Edge"); 
15       private Menu dilateMenu = getMenu("Dilate"); 
16       private Menu erodeMenu = getMenu("Erode"); 
17       private Menu openMorphMenu = getMenu("Open"); 
18       private Menu closeMorphMenu = getMenu("Close"); 
19       private Menu insideContourMenu = getMenu("Inside Contour"); 
20       private Menu outsideContourMenu = getMenu("Outside Contour"); 
21       private Menu middleContourMenu = getMenu("Middle Contour"); 
22       private Menu serraMenu = getMenu("Serra Transform (experimental)"); 
23       private Menu morphColorMenu = getMenu("Color (experimental)"); 
24    
25       private MenuItem dilateh_mi = addMenuItem(dilateMenu, "h"); 
26       private MenuItem dilatev_mi = addMenuItem(dilateMenu, "v"); 
27       private MenuItem dilateSquare_mi = addMenuItem(dilateMenu, "square"); 
28       private MenuItem dilateCross_mi = addMenuItem(dilateMenu, "cross"); 
29    
30       private MenuItem erodeh_mi = addMenuItem(erodeMenu, "h"); 
31       private MenuItem erodeh5_mi = addMenuItem(erodeMenu, "hx5"); 
32       private MenuItem erodev_mi = addMenuItem(erodeMenu, "v"); 
33       private MenuItem erodeSquare_mi = addMenuItem(erodeMenu, "square"); 
34       private MenuItem erodeCross_mi = addMenuItem(erodeMenu, "cross"); 
35    
36       private MenuItem openh_mi = addMenuItem(openMorphMenu, "h"); 
37       private MenuItem openv_mi = addMenuItem(openMorphMenu, "v"); 
38       private MenuItem openSquare_mi = addMenuItem(openMorphMenu, "square"); 
39       private MenuItem openCross_mi = addMenuItem(openMorphMenu, "cross"); 
40    
41       private MenuItem closeh_mi = addMenuItem(closeMorphMenu, "h"); 
42       private MenuItem closev_mi = addMenuItem(closeMorphMenu, "v"); 
43       private MenuItem closeSquare_mi = addMenuItem(closeMorphMenu, "square"); 
44       private MenuItem closeCross_mi = addMenuItem(closeMorphMenu, "cross"); 
45    
46       private MenuItem insideContourh_mi = addMenuItem(insideContourMenu, "h"); 
47       private MenuItem insideContourv_mi = addMenuItem(insideContourMenu, "v"); 
48       private MenuItem insideContourSquare_mi = addMenuItem(insideContourMenu, "square"); 
49       private MenuItem insideContourCross_mi = addMenuItem(insideContourMenu, "cross"); 
50    
51       private MenuItem outsideContourh_mi = addMenuItem(outsideContourMenu, "h"); 
52       private MenuItem outsideContourv_mi = addMenuItem(outsideContourMenu, "v"); 
53       private MenuItem outsideContourSquare_mi = addMenuItem(outsideContourMenu, "square"); 
54       private MenuItem outsideContourCross_mi = 
55               addMenuItem(outsideContourMenu, "[x]cross"); 
56    
57       private MenuItem middleContourh_mi = addMenuItem(middleContourMenu, "h"); 
58       private MenuItem middleContourv_mi = addMenuItem(middleContourMenu, "v"); 
59       private MenuItem middleContourSquare_mi = addMenuItem(middleContourMenu, "square"); 
60       private MenuItem middleContourCross_mi = addMenuItem(middleContourMenu, "cross"); 
61    
62       private MenuItem serrah_mi = addMenuItem(serraMenu, "h"); 
63       private MenuItem serrav_mi = addMenuItem(serraMenu, "v"); 
64       private MenuItem serraSquare_mi = addMenuItem(serraMenu, "square"); 
65       private MenuItem serraCross_mi = addMenuItem(serraMenu, "cross"); 
66    
67       private MenuItem skeleton_mi = addMenuItem(morphEdgeMenu, "[s]keleton"); 
68       private MenuItem thin_mi = addMenuItem(morphEdgeMenu, "[t]hin"); 
69    
70    
71       private MenuItem hatMorph_mi = addMenuItem(morphEdgeMenu, "hat Morph"); 
72    
73       private MenuItem colorDilateErode_mi = addMenuItem(morphColorMenu, "[E-c]olor Dilate-Erode"); 
74       private MenuItem colorDilate_mi = addMenuItem(morphColorMenu, "[E-d]color Dilate"); 
75       private MenuItem colorErode_mi = addMenuItem(morphColorMenu, "[E-e]color Erode"); 
76       private MenuItem colorOpen_mi = addMenuItem(morphColorMenu, "color open"); 
77       private MenuItem colorClose_mi = addMenuItem(morphColorMenu, "color close"); 
78       private MenuItem colorPyramid_mi = addMenuItem(morphColorMenu, "[p]yramid"); 
79    
80       public void actionPerformed(ActionEvent e) { 
81           if (match(e, thin_mi)) { 
82               thin(); 
83               return; 
84           } 
85    
86           if (match(e, colorPyramid_mi)) { 
87               colorPyramid(MorphUtils.kSquare); 
88               return; 
89           } 
90           if (match(e, colorOpen_mi)) { 
91               colorOpen(MorphUtils.kSquare); 
92               return; 
93           } 
94           if (match(e, colorClose_mi)) { 
95               colorClose(MorphUtils.kSquare); 
96               return; 
97           } 
98           if (match(e, serraSquare_mi)) { 
99               serra(MorphUtils.kSquare); 
100              return; 
101          } 
102   
103          if (match(e, serrah_mi)) { 
104              serra(MorphUtils.kh); 
105              return; 
106          } 
107   
108          if (match(e, serrav_mi)) { 
109              serra(MorphUtils.kv); 
110              return; 
111          } 
112   
113          if (match(e, serraCross_mi)) { 
114              serra(MorphUtils.kCross); 
115              return; 
116          } 
117   
118          if (match(e, colorErode_mi)) { 
119              colorErode(MorphUtils.kSquare); 
120              return; 
121          } 
122          if (match(e, colorDilate_mi)) { 
123              colorDilate(MorphUtils.kSquare); 
124              return; 
125          } 
126          if (match(e, colorDilateErode_mi)) { 
127              colorDilateErode(MorphUtils.kSquare); 
128              return; 
129          } 
130          if (match(e, skeleton_mi)) { 
131              skeleton(); 
132              return; 
133          } 
134          if (match(e, hatMorph_mi)) { 
135              hat13v2(); 
136              thresh(); 
137              insideContour(MorphUtils.kh); 
138              return; 
139          } 
140   
141          if (match(e, middleContourh_mi)) { 
142              middleContour(MorphUtils.kh); 
143              return; 
144          } 
145          if (match(e, middleContourv_mi)) { 
146              middleContour(MorphUtils.kv); 
147              return; 
148          } 
149          if (match(e, middleContourSquare_mi)) { 
150              middleContour(MorphUtils.kSquare); 
151              return; 
152          } 
153          if (match(e, middleContourCross_mi)) { 
154              middleContour(MorphUtils.kCross); 
155              return; 
156          } 
157          if (match(e, insideContourh_mi)) { 
158              insideContour(MorphUtils.kh); 
159              return; 
160          } 
161          if (match(e, insideContourv_mi)) { 
162              insideContour(MorphUtils.kv); 
163              return; 
164          } 
165          if (match(e, insideContourSquare_mi)) { 
166              insideContour(MorphUtils.kSquare); 
167              return; 
168          } 
169          if (match(e, insideContourCross_mi)) { 
170              insideContour(MorphUtils.kCross); 
171              return; 
172          } 
173   
174          if (match(e, outsideContourh_mi)) { 
175              outsideContour(MorphUtils.kh); 
176              return; 
177          } 
178          if (match(e, outsideContourv_mi)) { 
179              outsideContour(MorphUtils.kv); 
180              return; 
181          } 
182          if (match(e, outsideContourSquare_mi)) { 
183              outsideContour(MorphUtils.kSquare); 
184              return; 
185          } 
186          if (match(e, outsideContourCross_mi)) { 
187              outsideContour(MorphUtils.kCross); 
188              return; 
189          } 
190          if (match(e, openh_mi)) { 
191              open(MorphUtils.kh); 
192              return; 
193          } 
194          if (match(e, openv_mi)) { 
195              open(MorphUtils.kv); 
196              return; 
197          } 
198          if (match(e, openSquare_mi)) { 
199              open(MorphUtils.kSquare); 
200              return; 
201          } 
202          if (match(e, openCross_mi)) { 
203              open(MorphUtils.kCross); 
204              return; 
205          } 
206          if (match(e, closeh_mi)) { 
207              close(MorphUtils.kh); 
208              return; 
209          } 
210          if (match(e, closev_mi)) { 
211              close(MorphUtils.kv); 
212              return; 
213          } 
214          if (match(e, closeSquare_mi)) { 
215              close(MorphUtils.kSquare); 
216              return; 
217          } 
218          if (match(e, closeCross_mi)) { 
219              close(MorphUtils.kCross); 
220              return; 
221          } 
222          if (match(e, erodeCross_mi)) { 
223              erode(MorphUtils.kCross); 
224              return; 
225          } 
226          if (match(e, erodeSquare_mi)) { 
227              erode(MorphUtils.kSquare); 
228              return; 
229          } 
230   
231          if (match(e, erodeh5_mi)) { 
232              erode(MorphUtils.kh); 
233              erode(MorphUtils.kh); 
234              erode(MorphUtils.kh); 
235              erode(MorphUtils.kh); 
236              erode(MorphUtils.kh); 
237              return; 
238          } 
239          if (match(e, erodeh_mi)) { 
240              erode(MorphUtils.kh); 
241              return; 
242          } 
243          if (match(e, erodev_mi)) { 
244              erode(MorphUtils.kv); 
245              return; 
246          } 
247          if (match(e, dilateCross_mi)) { 
248              dilate(MorphUtils.kCross); 
249              return; 
250          } 
251          if (match(e, dilateSquare_mi)) { 
252              dilate(MorphUtils.kSquare); 
253              return; 
254          } 
255          if (match(e, dilateh_mi)) { 
256              dilate(MorphUtils.kh); 
257              return; 
258          } 
259          if (match(e, dilatev_mi)) { 
260              dilate(MorphUtils.kv); 
261              return; 
262          } 
263          super.actionPerformed(e); 
264   
265      } 
266   
267      public void colorPyramid(float k[][]) { 
268          short[][] r = MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getR(), k), k); 
269          shortImageBean.setR(r); 
270          setG(MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getG(), k), k)); 
271          setB(MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getB(), k), k)); 
272          short[][] r1 = MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getR(), k), k); 
273          shortImageBean.setR(r1); 
274          setG(MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getG(), k), k)); 
275          setB(MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getB(), k), k)); 
276          resample(2); 
277          short2Image(); 
278      } 
279   
280      public void resample2(int ratio) { 
281          child = new MorphFrame("MorphFrame"); 
282          ImageFrameInterface frame = child; 
283          int width1 = getImageWidth() / 2; 
284          frame.setImageWidth(width1); 
285          ImageFrameInterface frame1 = child; 
286          int height1 = getImageHeight() / 2; 
287          frame1.setImageHeight(height1); 
288          short[][] r = Mat2.resample(shortImageBean.getR(), 2); 
289          shortImageBean.setR(r); 
290          child.setG(Mat2.resample(shortImageBean.getG(), 2)); 
291          child.setB(Mat2.resample(shortImageBean.getB(), 2)); 
292      } 
293   
294      public void resample(int ratio) { 
295          int width1 = getImageWidth() / 2; 
296          setImageWidth(width1); 
297          int height1 = getImageHeight() / 2; 
298          setImageHeight(height1); 
299          short[][] r = Mat2.resample(shortImageBean.getR(), 2); 
300          shortImageBean.setR(r); 
301          setG(Mat2.resample(shortImageBean.getG(), 2)); 
302          setB(Mat2.resample(shortImageBean.getB(), 2)); 
303      } 
304   
305      public void colorOpen(float k[][]) { 
306          short[][] r = MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getR(), k), k); 
307          shortImageBean.setR(r); 
308          setG(MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getG(), k), k)); 
309          setB(MorphUtils.dilategs(MorphUtils.erodegs(shortImageBean.getB(), k), k)); 
310          short2Image(); 
311      } 
312   
313      public void colorClose(float k[][]) { 
314          short[][] r = MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getR(), k), k); 
315          shortImageBean.setR(r); 
316          setG(MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getG(), k), k)); 
317          setB(MorphUtils.erodegs(MorphUtils.dilategs(shortImageBean.getB(), k), k)); 
318          short2Image(); 
319      } 
320   
321      public void open(float k[][]) { 
322          short[][] r = MorphUtils.dilate(MorphUtils.erode(shortImageBean.getR(), k), k); 
323          shortImageBean.setR(r); 
324          shortImageBean.copyRedToGreenAndBlue(); 
325          short2Image(); 
326      } 
327   
328      public void close(float k[][]) { 
329          short[][] r = MorphUtils.erode(MorphUtils.dilate(shortImageBean.getR(), k), k); 
330          shortImageBean.setR(r); 
331          shortImageBean.copyRedToGreenAndBlue(); 
332          short2Image(); 
333      } 
334   
335      public void serra(float k[][]) { 
336          short[][] r = MorphUtils.erode(shortImageBean.getR(), k); 
337          shortImageBean.setR(r); 
338          setG(MorphUtils.erode(complement(shortImageBean.getG()), MorphUtils.kSquare)); 
339          short[][] r1 = intersect(shortImageBean.getR(), shortImageBean.getG()); 
340          shortImageBean.setR(r1); 
341          shortImageBean.copyRedToGreenAndBlue(); 
342          short2Image(); 
343      } 
344   
345      public short[][] intersect(short a[][], short b[][]) { 
346          int w = a.length; 
347          int h = a[0].length; 
348          short c[][] = new short[w][h]; 
349          for (int x = 0; x < w; x++) 
350              for (int y = 0; y < h; y++) 
351                  if (a[x][y] == b[x][y]) 
352                      c[x][y] = a[x][y]; 
353          return c; 
354      } 
355   
356      public short[][] complement(short s[][]) { 
357          int w = s.length; 
358          int h = s[0].length; 
359          short sComp[][] = new short[w][h]; 
360          for (int x = 0; x < w; x++) 
361              for (int y = 0; y < h; y++) 
362                  sComp[x][y] = (short) (255 - s[x][y]); 
363          return sComp; 
364      } 
365   
366      public static void main(String args[]) { 
367          MorphFrame ef = new MorphFrame("MorphFrame"); 
368          ef.show(); 
369      } 
370   
371   
372      public void dilate(float k[][]) { 
373          short[][] r = MorphUtils.dilate(shortImageBean.getG(), k); 
374          shortImageBean.setR(r); 
375          shortImageBean.copyRedToGreenAndBlue(); 
376          short2Image(); 
377      } 
378   
379      public void erode(float k[][]) { 
380          short[][] r = MorphUtils.erode(shortImageBean.getG(), k); 
381          shortImageBean.setR(r); 
382          shortImageBean.copyRedToGreenAndBlue(); 
383          short2Image(); 
384      } 
385   
386      public void colorDilateErode(float k[][]) { 
387          for (int i = 0; i < 5; i++) 
388              colorDilate(k); 
389          for (int i = 0; i < 5; i++) 
390              colorErode(k); 
391      } 
392   
393      public void colorDilate(float k[][]) { 
394          short[][] r = 
395                  MorphUtils.dilategs( 
396                          shortImageBean.getR(), 
397                          k); 
398          shortImageBean.setR(r); 
399          setG(MorphUtils.dilategs( 
400                  shortImageBean.getG(), k)); 
401          setB(MorphUtils.dilategs( 
402                  shortImageBean.getB(), k)); 
403          short2Image(); 
404      } 
405   
406      public void colorErode(float k[][]) { 
407          short[][] r = MorphUtils.erodegs( 
408                  shortImageBean.getR(), k); 
409          shortImageBean.setR(r); 
410          setG(MorphUtils.erodegs( 
411                  shortImageBean.getG(), k)); 
412          setB(MorphUtils.erodegs( 
413                  shortImageBean.getB(), k)); 
414          short2Image(); 
415      } 
416   
417   
418      public void insideContour(float k[][]) { 
419          short[][] r = Mat2.subtract(shortImageBean.getG(), MorphUtils.erode(shortImageBean.getG(), k)); 
420          shortImageBean.setR(r); 
421          shortImageBean.copyRedToGreenAndBlue(); 
422          short2Image(); 
423      } 
424   
425      public void outsideContour(float k[][]) { 
426          short[][] r = Mat2.subtract(MorphUtils.dilate(shortImageBean.getG(), k), shortImageBean.getG()); 
427          shortImageBean.setR(r); 
428          shortImageBean.copyRedToGreenAndBlue(); 
429          short2Image(); 
430      } 
431   
432      public void middleContour(float k[][]) { 
433          short[][] r = Mat2.subtract( 
434                  MorphUtils.dilate(shortImageBean.getG(), k), 
435                  MorphUtils.erode(shortImageBean.getG(), k)); 
436          shortImageBean.setR(r); 
437          shortImageBean.copyRedToGreenAndBlue(); 
438          short2Image(); 
439      } 
440   
441      public void thin() { 
442          skeletonRedPassSuen(true); 
443          skeletonRedPassSuen(false); 
444          shortImageBean.copyRedToGreenAndBlue(); 
445          short2Image(); 
446      } 
447   
448      public void skeleton() { 
449          while ( 
450                  skeletonRedPassSuen(true) && 
451                  skeletonRedPassSuen(false)) { 
452          } 
453          shortImageBean.copyRedToGreenAndBlue(); 
454          short2Image(); 
455      } 
456   
457   
458      // p7 p0 p1 
459      // p6 --- p2 
460      // p5 p4 p3 
461      public boolean skeletonRedPassSuen( 
462              boolean firstPass) { 
463          ShortImageBean sib = shortImageBean; 
464          short c = MorphUtils.redPassSuen(sib, firstPass); 
465          //System.out.println("c="+c); 
466          if (c == 0) return false; 
467          deleteFlagedPoints(); 
468          return true; 
469      } 
470   
471      public void deleteFlagedPoints() { 
472          for (int x = 1; x < getImageWidth() - 1; x++) 
473              for (int y = 1; y < getImageHeight() - 1; y++) 
474                  if (shortImageBean.getG()[x][y] != 0) 
475                      shortImageBean.getR()[x][y] = 0; 
476      } 
477   
478   
479      MorphFrame(String title) { 
480          super(title); 
481          morphMenu.add(erodeMenu); 
482          morphMenu.add(dilateMenu); 
483          morphMenu.add(openMorphMenu); 
484          morphMenu.add(closeMorphMenu); 
485          morphMenu.add(insideContourMenu); 
486          morphMenu.add(middleContourMenu); 
487          morphMenu.add(outsideContourMenu); 
488          morphMenu.add(morphEdgeMenu); 
489          morphMenu.add(morphColorMenu); 
490          morphMenu.add(serraMenu); 
491          getSpatialFilterMenu().add(morphMenu); 
492   
493      } 
494   
495   
496  }