OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / test / jp / nyatla / nyartoolkit / dev / tracking / TrTest.java
1 package jp.nyatla.nyartoolkit.dev.tracking;\r
2 \r
3 import java.awt.*;\r
4 import java.awt.color.*;\r
5 import java.awt.event.*;\r
6 import java.awt.image.*;\r
7 import java.io.*;\r
8 import javax.media.Buffer;\r
9 import jp.nyatla.nyartoolkit.NyARException;\r
10 import jp.nyatla.nyartoolkit.core.NyARCode;\r
11 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
12 import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt;\r
13 import jp.nyatla.nyartoolkit.core.pickup.NyARColorPatt_Perspective_O2;\r
14 import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster;\r
15 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;\r
16 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector;\r
17 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;\r
18 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;\r
19 import jp.nyatla.nyartoolkit.core.transmat.INyARTransMat;\r
20 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMat;\r
21 import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
22 import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
23 import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
24 import jp.nyatla.nyartoolkit.jogl.utils.GLNyARRaster_RGB;\r
25 import jp.nyatla.nyartoolkit.utils.j2se.NyARRasterImageIO;\r
26 import jp.nyatla.nyartoolkit.core.types.*;\r
27 \r
28 /**\r
29  * @todo\r
30  * 矩形の追跡は動いてるから、位置予測機能と組み合わせて試すこと。\r
31  *\r
32  */\r
33 class Tracking extends MarkerTracking_3dTrans\r
34 {\r
35         Tracking(NyARParam i_param,NyARCode i_code,double i_marker_width,int i_input_raster_type) throws NyARException\r
36         {\r
37                 super();\r
38                 initInstance(\r
39                                 new NyARColorPatt_Perspective_O2(i_code.getWidth(), i_code.getHeight(),4,25),\r
40                                 new NyARSquareContourDetector_Rle(i_param.getDistortionFactor(),i_param.getScreenSize()),\r
41                                 new NyARTransMat(i_param),\r
42                                 new NyARRasterFilter_ARToolkitThreshold(120,i_input_raster_type),\r
43                                 i_param,\r
44                                 i_code,\r
45                                 i_marker_width);\r
46         }\r
47 }\r
48 \r
49 public class TrTest extends Frame implements JmfCaptureListener,MouseMotionListener\r
50 {\r
51 \r
52 \r
53         private final String PARAM_FILE = "../Data/camera_para.dat";\r
54 \r
55         private final static String CARCODE_FILE = "../Data/patt.hiro";\r
56 \r
57         private static final long serialVersionUID = -2110888320986446576L;\r
58 \r
59         private JmfCaptureDevice _capture;\r
60 \r
61         private GLNyARRaster_RGB _capraster;\r
62 \r
63         private int W = 320;\r
64 \r
65         private int H = 240;\r
66 \r
67         private Tracking _tr;\r
68         TransMat2MarkerRect _trm;\r
69 \r
70         public TrTest() throws NyARException\r
71         {\r
72                 setTitle("JmfCaptureTest");\r
73                 Insets ins = this.getInsets();\r
74                 this.setSize(1024 + ins.left + ins.right, 768 + ins.top + ins.bottom);\r
75                 JmfCaptureDeviceList dl = new JmfCaptureDeviceList();\r
76                 this._capture = dl.getDevice(0);\r
77                 if (!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB, W, H, 30.0f)) {\r
78                         if (!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV, W, H, 30.0f)) {\r
79                                 throw new NyARException("キャプチャフォーマットが見つかりません。");\r
80                         }\r
81                 }\r
82                 NyARParam ar_param = new NyARParam();\r
83                 ar_param.loadARParamFromFile(PARAM_FILE);\r
84                 ar_param.changeScreenSize(W, H);\r
85 \r
86                 NyARCode code = new NyARCode(16, 16);\r
87                 code.loadARPattFromFile(CARCODE_FILE);\r
88                 this._capraster = new GLNyARRaster_RGB(ar_param, this._capture.getCaptureFormat());\r
89                 this._capture.setOnCapture(this);\r
90 \r
91                 addMouseMotionListener(this);\r
92                 this._tr=new Tracking(ar_param,code,80,this._capraster.getBufferType());\r
93                 this._trm=new TransMat2MarkerRect(ar_param);\r
94                 return;\r
95         }\r
96         int mouse_x;\r
97         int mouse_y;\r
98     public void mouseMoved(MouseEvent A00)\r
99     {\r
100         mouse_x = A00.getX();\r
101         mouse_y = A00.getY();\r
102     }\r
103 \r
104     public void mouseDragged(MouseEvent A00) {}\r
105 \r
106 \r
107 \r
108         private final String data_file = "../Data/320x240ABGR.raw";\r
109 \r
110         \r
111 \r
112         public void draw(GLNyARRaster_RGB i_raster)\r
113         {\r
114                 try {\r
115                         Insets ins = this.getInsets();\r
116                         Graphics g = getGraphics();\r
117                         this._tr.detectMarkerLite(i_raster);\r
118                         Object[] probe=this._tr._probe();\r
119                         MarkerPositionTable mpt=(MarkerPositionTable)probe[0];\r
120                         NextFrameMarkerStack fs=(NextFrameMarkerStack)probe[2];\r
121                         \r
122                         {// ピックアップ画像の表示\r
123                                 // 矩形抽出\r
124 //                              INyARRasterFilter_RgbToBin to_binfilter = NyARRasterFilterBuilder_ARToolkitThreshold.createFilter(110, i_raster.getBufferReader().getBufferType());\r
125 //                              to_binfilter.doFilter(i_raster, this._bin_raster);\r
126 //                              if (this.detect.detectMarkerLite(i_raster, 100)) {\r
127 //\r
128 //                                      NyARTransMatResult res = new NyARTransMatResult();\r
129 //                                      this.detect.getTransmationMatrix(res);\r
130 //                              }\r
131                                 {// 撮影画像\r
132                                         \r
133 //                                      INyARRasterFilter_RgbToBin filter=new NyARRasterFilter_ARToolkitThreshold(110,i_raster.getBufferReader().getBufferType());\r
134 //                                      NyARSquareStack stack=new NyARSquareStack(10);\r
135                                         \r
136                                         BufferedImage sink = new BufferedImage(i_raster.getWidth(), i_raster.getHeight(), ColorSpace.TYPE_RGB);\r
137 \r
138                                         {//元画像\r
139                                                 NyARRasterImageIO.copy(i_raster, sink);\r
140                                                 Graphics g2=sink.getGraphics();\r
141                                                 g2.setColor(Color.RED);\r
142                                                 MarkerPositionTable.Item[] item=mpt.selectAllItems();\r
143                                                 for(int i=0;i<item.length;i++){\r
144                                                         if(item[i].is_empty){\r
145                                                                 continue;\r
146                                                         }\r
147                                                         NyARIntRect re=new NyARIntRect();\r
148                                                         this._trm.convert(item[i], re);\r
149                                                         g2.drawRect(re.x,re.y,re.w,re.h);\r
150                                                         g2.drawString(Integer.toString(item[i].sirial),re.x,re.y);\r
151                                                 }\r
152                                                 g2.setColor(Color.CYAN);\r
153                                                 for(int i=0;i<fs.getLength();i++){\r
154                                                         g2.drawRect((int)(fs.getItem(i).vertex0.x-1),(int)(fs.getItem(i).vertex0.y-1),2,2);\r
155                                                 }\r
156                                                 g.drawImage(sink, ins.left, ins.top, this);\r
157                                         }\r
158                                 }\r
159 \r
160                                 {// 信号取得テスト\r
161 \r
162                                 }\r
163                         }\r
164                 } catch (Exception e) {\r
165                         e.printStackTrace();\r
166                 }\r
167         }\r
168 \r
169         public void onUpdateBuffer(Buffer i_buffer)\r
170         {\r
171                 try {\r
172 \r
173                         {// ピックアップ画像の表示\r
174                                 // 矩形抽出\r
175                                 synchronized(this._capraster){\r
176                                         this._capraster.setBuffer(i_buffer);\r
177                                         draw(this._capraster);\r
178                                 }\r
179                         }\r
180                 } catch (Exception e) {\r
181                         e.printStackTrace();\r
182                 }\r
183         }\r
184 \r
185         public void startCapture()\r
186         {\r
187                 try {\r
188                         this._capture.start();\r
189                 } catch (Exception e) {\r
190                         e.printStackTrace();\r
191                 }\r
192         }\r
193 \r
194         public void startImage()\r
195         {\r
196                 try {\r
197                         // 試験イメージの読み出し(320x240 BGRAのRAWデータ)\r
198                         File f = new File(data_file);\r
199                         FileInputStream fs = new FileInputStream(data_file);\r
200                         byte[] buf = new byte[(int) f.length() * 4];\r
201                         fs.read(buf);\r
202 //                      INyARRgbRaster ra = NyARRgbRaster_BGRA.wrap(buf, W, H);\r
203 //                      draw(ra);\r
204                 } catch (Exception e) {\r
205                         e.printStackTrace();\r
206                 }\r
207 \r
208         }\r
209 \r
210         public static void main(String[] args)\r
211         {\r
212 \r
213                 try {\r
214                         TrTest mainwin = new TrTest();\r
215                         mainwin.setVisible(true);\r
216                         mainwin.startCapture();\r
217                         // mainwin.startImage();\r
218                 } catch (Exception e) {\r
219                         e.printStackTrace();\r
220                 }\r
221         }\r
222 \r
223 }\r