1 package jp.nyatla.nyartoolkit.rpf.sample;
\r
4 import java.awt.event.MouseEvent;
\r
5 import java.awt.event.MouseListener;
\r
6 import java.awt.image.*;
\r
8 import java.io.IOException;
\r
10 import javax.imageio.ImageIO;
\r
12 import jp.nyatla.nyartoolkit.NyARException;
\r
13 import jp.nyatla.nyartoolkit.core.param.NyARParam;
\r
14 import jp.nyatla.nyartoolkit.core.raster.NyARGrayscaleRaster;
\r
15 import jp.nyatla.nyartoolkit.core.raster.rgb.NyARRgbRaster;
\r
16 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;
\r
17 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d;
\r
18 import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;
\r
19 import jp.nyatla.nyartoolkit.core.types.NyARIntRect;
\r
20 import jp.nyatla.nyartoolkit.rpf.mklib.ARTKMarkerTable;
\r
21 import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARReality;
\r
22 import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;
\r
23 import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;
\r
24 import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource;
\r
25 import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_JavaImage;
\r
26 import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;
\r
27 import jp.nyatla.nyartoolkit.rpf.tracker.nyartk.status.NyARRectTargetStatus;
\r
28 import jp.nyatla.nyartoolkit.rpf.tracker.nyartk.status.NyARTargetStatus;
\r
29 import jp.nyatla.nyartoolkit.jmf.utils.*;
\r
30 import jp.nyatla.nyartoolkit.utils.j2se.*;
\r
37 * NyARRealityのテストプログラム。動作保証なし。
\r
41 public class Test_RealityTarget extends Frame implements MouseListener
\r
49 public NyARRealitySource reality_in;
\r
52 class ImageSource extends InputSource
\r
54 public ImageSource(String i_filename) throws NyARException, IOException
\r
56 BufferedImage _src_image;
\r
57 _src_image = ImageIO.read(new File(i_filename));
\r
58 NyARRealitySource_JavaImage ri=new NyARRealitySource_JavaImage(_src_image.getWidth(),_src_image.getHeight(),null,2,100);
\r
59 ri.getBufferedImage().getGraphics().drawImage(_src_image,0,0,null);
\r
64 class LiveSource extends InputSource implements JmfCaptureListener
\r
66 private JmfCaptureDevice _capture;
\r
67 public LiveSource() throws NyARException
\r
70 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
71 this._capture=devlist.getDevice(0);
\r
72 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
\r
73 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
\r
74 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
\r
75 throw new NyARException("キャプチャフォーマットが見つかりません");
\r
78 this._capture.setOnCapture(this);
\r
79 this._capture.start();
\r
80 this.reality_in=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);
\r
85 public void onUpdateBuffer(javax.media.Buffer i_buffer)
\r
88 //キャプチャしたバッファをラスタにセット
\r
89 synchronized(this.reality_in){
\r
90 ((NyARRealitySource_Jmf)(this.reality_in)).setImage(i_buffer);
\r
92 //キャプチャしたイメージを表示用に加工
\r
95 e.printStackTrace();
\r
100 public void startCapture()
\r
103 this._capture.start();
\r
104 } catch (Exception e) {
\r
105 e.printStackTrace();
\r
110 public void mouseClicked(MouseEvent e)
\r
113 int x=e.getX()-this.getInsets().left;
\r
114 int y=e.getY()-this.getInsets().top;
\r
115 System.out.println(x+":"+y);
\r
116 synchronized(this._input_source.reality_in)
\r
118 NyARBufferedImageRaster bmp= new NyARBufferedImageRaster(100,100,NyARBufferType.BYTE1D_R8G8B8_24);
\r
119 for(int i=this._reality.refTargetList().getLength()-1;i>=0;i--)
\r
121 NyARRealityTarget rt=this._reality.refTargetList().getItem(i);
\r
122 if(rt._target_type!=NyARRealityTarget.RT_UNKNOWN && rt._target_type!=NyARRealityTarget.RT_KNOWN){
\r
125 if(rt.isInnerVertexPoint2d(x, y))
\r
127 if(e.getButton()==MouseEvent.BUTTON1){
\r
128 //左ボタンはUNKNOWN→KNOWN
\r
129 if(rt._target_type==NyARRealityTarget.RT_UNKNOWN){
\r
130 ARTKMarkerTable.GetBestMatchTargetResult r=new ARTKMarkerTable.GetBestMatchTargetResult();
\r
131 if(this._mklib.getBestMatchTarget(rt,this._input_source.reality_in,r)){
\r
132 this._reality.changeTargetToKnown(rt,r.artk_direction,80);
\r
135 NyARIntPoint2d[] iv=NyARIntPoint2d.createArray(4);
\r
136 for(int i2=0;i2<4;i2++){
\r
137 iv[i2].x=(int)rt.refTargetVertex()[i2].x;
\r
138 iv[i2].y=(int)rt.refTargetVertex()[i2].y;
\r
140 System.out.println(">>");
\r
141 NyARRgbRaster rgb=new NyARRgbRaster(320,240,NyARBufferType.BYTE1D_R8G8B8_24);
\r
144 this._reality.getRgbPatt2d(this._input_source.reality_in,rt.refTargetVertex(),1,bmp);
\r
145 this.getGraphics().drawImage(bmp.getBufferedImage(),this.getInsets().left,this.getInsets().top+240,null);
\r
149 NyARDoublePoint3d[] p=NyARDoublePoint3d.createArray(4);
\r
150 p[3].x=-40;p[3].y=-40;p[3].z=0;
\r
151 p[2].x=40; p[2].y=-40;p[2].z=0;
\r
152 p[1].x=40; p[1].y=40 ;p[1].z=0;
\r
153 p[0].x=-40;p[0].y=40 ;p[0].z=0;
\r
154 this._reality.getRgbPatt3d(this._input_source.reality_in,p, rt.refTransformMatrix(), 1, bmp);
\r
155 this.getGraphics().drawImage(bmp.getBufferedImage(),this.getInsets().left+100,this.getInsets().top+240,null);
\r
159 NyARDoublePoint3d[] p=NyARDoublePoint3d.createArray(4);
\r
160 p[3].x=-40;p[3].y=-40;p[3].z=0;
\r
161 p[2].x=40; p[2].y=-40;p[2].z=0;
\r
162 p[1].x=40; p[1].y=40 ;p[1].z=0;
\r
163 p[0].x=-80;p[0].y=40 ;p[0].z=0;
\r
164 rt.getRgbPatt3d(this._input_source.reality_in,p,null, 1, bmp);
\r
165 this.getGraphics().drawImage(bmp.getBufferedImage(),this.getInsets().left+200,this.getInsets().top+240,null);
\r
169 rt.getRgbRectPatt3d(this._input_source.reality_in,-80,-80,80,80,1, bmp);
\r
170 this.getGraphics().drawImage(bmp.getBufferedImage(),this.getInsets().left+300,this.getInsets().top+240,null);
\r
174 }else if(e.getButton()==MouseEvent.BUTTON3){
\r
175 //右ボタンはUNKNOWN or KNOWN to dead
\r
177 this._reality.changeTargetToDead(rt);
\r
179 } catch (NyARException e1) {
\r
180 // TODO Auto-generated catch block
\r
181 e1.printStackTrace();
\r
187 } catch (NyARException e1) {
\r
188 // TODO Auto-generated catch block
\r
189 e1.printStackTrace();
\r
192 public void mouseEntered(MouseEvent e){}
\r
193 public void mouseExited(MouseEvent e){}
\r
194 public void mousePressed(MouseEvent e){}
\r
195 public void mouseReleased(MouseEvent e){}
\r
196 ARTKMarkerTable _mklib;
\r
199 private NyARReality _reality;
\r
205 private final static String SAMPLE_FILES = "../Data/320x240ABGR.png";
\r
206 private final static String PARAM_FILE = "../Data/camera_para.dat";
\r
208 private static final long serialVersionUID = -2110888320986446576L;
\r
211 private int W = 320;
\r
212 private int H = 240;
\r
213 InputSource _input_source;
\r
214 public Test_RealityTarget() throws NyARException, Exception
\r
216 setTitle("NyARReality test");
\r
217 Insets ins = this.getInsets();
\r
218 this.setSize(1024 + ins.left + ins.right, 768 + ins.top + ins.bottom);
\r
219 this._param=new NyARParam();
\r
220 this._param.loadARParamFromFile(PARAM_FILE);
\r
221 this._param.changeScreenSize(W,H);
\r
222 this._reality=new NyARReality(this._param.getScreenSize(),10,1000,this._param.getPerspectiveProjectionMatrix(),null,10,10);
\r
223 // this._input_source=new LiveSource();
\r
224 this._input_source=new ImageSource(SAMPLE_FILES);
\r
225 addMouseListener(this);
\r
226 this._mklib= new ARTKMarkerTable(10,16,16,25,25,4);
\r
227 this._mklib.addMarkerFromARPattFile(PATT_HIRO,0,"HIRO",80,80);
\r
231 public void update()
\r
236 synchronized(this._input_source.reality_in){
\r
237 // Date d2 = new Date();
\r
238 // for (int i = 0; i < 1000; i++) {
\r
239 this._reality.progress(this._input_source.reality_in);
\r
241 // Date d = new Date();
\r
242 // System.out.println(d.getTime() - d2.getTime());
\r
244 } catch (Exception e) {
\r
245 e.printStackTrace();
\r
248 BufferedImage _tmp_bf=new BufferedImage(320,240,BufferedImage.TYPE_INT_RGB);
\r
249 private void draw(Graphics ig) throws NyARException
\r
252 Insets ins = this.getInsets();
\r
255 BufferedImage bmp=this._tmp_bf;
\r
256 Graphics g=bmp.getGraphics();
\r
257 NyARRasterImageIO.copy(this._input_source.reality_in.refRgbSource(),bmp);
\r
262 g.setColor(Color.black);
\r
263 g.drawString("Unknown:"+this._reality.getNumberOfUnknown(),200,200);
\r
264 g.drawString("Known:"+this._reality.getNumberOfKnown(),200,210);
\r
265 g.drawString("Dead:"+this._reality.getNumberOfDead(),200,220);
\r
266 NyARRealityTargetList tl=this._reality.refTargetList();
\r
267 for(int i=tl.getLength()-1;i>=0;i--){
\r
268 NyARRealityTarget t=tl.getItem(i);
\r
269 switch(t.getTargetType())
\r
271 case NyARRealityTarget.RT_KNOWN:
\r
274 case NyARRealityTarget.RT_UNKNOWN:
\r
275 drawUnKnownRT(g,t);
\r
282 ig.drawImage(bmp,ins.left,ins.top,null);
\r
285 drawImage(ig,ins.left+320,ins.top,this._input_source.reality_in.refLastTrackSource().refEdgeRaster());
\r
291 private void drawImage(Graphics g,int x,int y,NyARGrayscaleRaster r) throws NyARException
\r
293 BufferedImage _tmp_bf=new BufferedImage(r.getWidth(),r.getHeight(),BufferedImage.TYPE_INT_RGB);
\r
294 NyARRasterImageIO.copy(r, _tmp_bf);
\r
295 g.drawImage(_tmp_bf, x,y, null);
\r
301 private void drawKnownRT(Graphics g,NyARRealityTarget t)
\r
303 g.setColor(Color.GREEN);
\r
304 NyARIntPoint2d b=new NyARIntPoint2d();
\r
305 t.getTargetCenter(b);
\r
306 NyARIntRect r=t._ref_tracktarget._sample_area;
\r
307 g.drawString("[K]("+t.grab_rate+")",b.x,b.y);
\r
308 g.drawRect(r.x,r.y, r.w,r.h);
\r
309 if(t._ref_tracktarget._st_type==NyARTargetStatus.ST_RECT){
\r
310 g.drawString(">"+((NyARRectTargetStatus)(t._ref_tracktarget._ref_status)).detect_type,r.x,r.y+10);
\r
314 private void drawUnKnownRT(Graphics g,NyARRealityTarget t)
\r
316 g.setColor(Color.YELLOW);
\r
317 NyARIntPoint2d b=new NyARIntPoint2d();
\r
318 t.getTargetCenter(b);
\r
319 NyARIntRect r=t._ref_tracktarget._sample_area;
\r
320 g.drawString("[U]("+t.grab_rate+")",b.x,b.y);
\r
321 g.drawRect(r.x,r.y, r.w,r.h);
\r
323 private void drawDeadRT(Graphics g,NyARRealityTarget t)
\r
325 g.setColor(Color.RED);
\r
326 NyARIntRect r=t._ref_tracktarget._sample_area;
\r
327 g.drawString("[D]("+t.grab_rate+")",r.x,r.y);
\r
330 private final static String PATT_HIRO = "../Data/patt.hiro";
\r
334 public void mainloop() throws Exception
\r
339 this.draw(this.getGraphics());
\r
343 public static void main(String[] args)
\r
347 Test_RealityTarget mainwin = new Test_RealityTarget();
\r
348 mainwin.setVisible(true);
\r
349 mainwin.mainloop();
\r
350 // mainwin.startImage();
\r
351 } catch (Exception e) {
\r
352 e.printStackTrace();
\r