OSDN Git Service

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