OSDN Git Service

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