OSDN Git Service

[TAG]NyARToolkit/2.3.1
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.3.1 / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / x2 / VisualTest.java
1 /* このソースは実験用のソースです。
2  * 動いたり動かなかったりします。
3  * 
4  */
5 package jp.nyatla.nyartoolkit.sandbox.x2;
6
7 import javax.media.*;
8
9 import javax.media.util.BufferToImage;
10 import javax.media.format.*;
11
12 import jp.nyatla.nyartoolkit.NyARException;
13 import jp.nyatla.nyartoolkit.jmf.utils.*;
14 import jp.nyatla.nyartoolkit.sandbox.quadx2.*;
15
16 import jp.nyatla.nyartoolkit.core.*;
17
18 import java.awt.*;
19
20 import jp.nyatla.nyartoolkit.core.labeling.*;
21 import jp.nyatla.nyartoolkit.core.param.*;
22 import jp.nyatla.nyartoolkit.core.raster.*;
23 import jp.nyatla.nyartoolkit.core.rasterfilter.*;
24 import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.*;
25 import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*;
26 import jp.nyatla.utils.j2se.LabelingBufferdImage;
27 import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*;
28
29
30
31 public class VisualTest extends Frame implements JmfCaptureListener
32 {
33         private final String camera_file = "../../Data/camera_para.dat";
34
35         private JmfNyARRaster_RGB _raster;
36
37         private JmfCaptureDevice capture;
38         private NyARParam ap;
39         public VisualTest() throws NyARException, NyARException
40         {
41                 setBounds(0, 0, 640 + 64, 720 + 64);
42                 // キャプチャの準備
43                 JmfCaptureDeviceList list=new JmfCaptureDeviceList();
44                 capture=list.getDevice(0);
45                 capture.setCaptureFormat(320,240,30.0f);
46                 capture.setOnCapture(this);
47
48                 // キャプチャイメージ用のラスタを準備
49                 this._raster = new JmfNyARRaster_RGB(320, 240,capture.getCaptureFormat());
50                 
51                 // AR用カメラパラメタファイルをロード
52                 ap = new NyARParam();
53                 ap.loadARParamFromFile(camera_file);
54                 ap.changeScreenSize(320, 240);          
55                 
56                 
57         }
58
59         private NyARBinRaster _binraster1 = new NyARBinRaster(160,120);
60
61         private NyARGrayscaleRaster _gsraster1 = new NyARGrayscaleRaster(320, 240);
62         private INyARRasterThresholdAnalyzer _tha=new NyARRasterThresholdAnalyzer_DiffHistgram();
63
64         private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240);
65         private LabelingBufferdImage _bimg2 = new LabelingBufferdImage(160, 120);
66         private NyARRasterFilter_ARTTh_Quad _tobin_filter=new NyARRasterFilter_ARTTh_Quad(100);
67
68         public void onUpdateBuffer(Buffer i_buffer)
69         {
70
71                 try {
72                         // キャプチャしたバッファをラスタにセット
73                         _raster.setBuffer(i_buffer);
74
75                         Graphics g = getGraphics();
76                         // キャプチャ画像
77                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
78                         Image img = b2i.createImage(i_buffer);
79                         this.getGraphics().drawImage(img, 32, 32, this);
80
81                         // 画像1
82                         INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbAve();
83 //                      INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbMul();
84                         
85                         filter_rgb2gs.doFilter(_raster, _gsraster1);
86                         this._bimg.drawImage(this._gsraster1);
87                         this.getGraphics().drawImage(this._bimg, 32 + 320, 32, 320 + 320 + 32, 240 + 32, 0, 240, 320, 0, this);
88                         _tha.analyzeRaster(_gsraster1);
89                         NyARRasterFilter_Threshold gs2bin=new NyARRasterFilter_Threshold(_tha.getThreshold());
90                         
91
92                         // 画像2
93                         _tobin_filter.doFilter(_raster, _binraster1);
94                         this._bimg2.drawImage(_binraster1);
95                         this.getGraphics().drawImage(this._bimg2, 32, 32 + 240, 320 + 32, 240 + 32 + 240, 0, 240, 320, 0, this);
96
97                         // 画像3
98                         NyARLabelingImage limage = new NyARLabelingImage(320, 240);
99                         NyARLabeling_ARToolKit labeling = new NyARLabeling_ARToolKit();
100                         labeling.attachDestination(limage);
101                         labeling.labeling(_binraster1);
102                         this._bimg.drawImage(this._gsraster1);
103
104                         NyARSquareStack stack = new NyARSquareStack(100);
105                 } catch (Exception e) {
106                         e.printStackTrace();
107                 }
108
109         }
110
111         private void startCapture()
112         {
113                 try {
114                         capture.start();
115                 } catch (Exception e) {
116                         e.printStackTrace();
117                 }
118         }
119
120         public static void main(String[] args)
121         {
122                 try {
123                         VisualTest mainwin = new VisualTest();
124                         mainwin.setVisible(true);
125                         mainwin.startCapture();
126                 } catch (Exception e) {
127                         e.printStackTrace();
128                 }
129
130         }
131
132 }