1 package jp.nyatla.nyartoolkit.dev.tracking;
\r
4 import java.awt.color.*;
\r
5 import java.awt.event.*;
\r
6 import java.awt.image.*;
\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
29 * 矩形の追跡は動いてるから、位置予測機能と組み合わせて試すこと。
\r
32 class Tracking extends MarkerTracking_3dTrans
\r
34 Tracking(NyARParam i_param,NyARCode i_code,double i_marker_width,int i_input_raster_type) throws NyARException
\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
48 public class TrTest extends Frame implements JmfCaptureListener,MouseMotionListener
\r
52 private final String PARAM_FILE = "../Data/camera_para.dat";
\r
54 private final static String CARCODE_FILE = "../Data/patt.hiro";
\r
56 private static final long serialVersionUID = -2110888320986446576L;
\r
58 private JmfCaptureDevice _capture;
\r
60 private JmfNyARRaster_RGB _capraster;
\r
62 private int W = 320;
\r
64 private int H = 240;
\r
66 private Tracking _tr;
\r
67 TransMat2MarkerRect _trm;
\r
69 public TrTest() throws NyARException
\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
81 NyARParam ar_param = new NyARParam();
\r
82 ar_param.loadARParamFromFile(PARAM_FILE);
\r
83 ar_param.changeScreenSize(W, H);
\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
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
97 public void mouseMoved(MouseEvent A00)
\r
99 mouse_x = A00.getX();
\r
100 mouse_y = A00.getY();
\r
103 public void mouseDragged(MouseEvent A00) {}
\r
107 private final String data_file = "../Data/320x240ABGR.raw";
\r
111 public void draw(JmfNyARRaster_RGB i_raster)
\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
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
127 // NyARTransMatResult res = new NyARTransMatResult();
\r
128 // this.detect.getTransmationMatrix(res);
\r
132 // INyARRasterFilter_RgbToBin filter=new NyARRasterFilter_ARToolkitThreshold(110,i_raster.getBufferReader().getBufferType());
\r
133 // NyARSquareStack stack=new NyARSquareStack(10);
\r
135 BufferedImage sink = new BufferedImage(i_raster.getWidth(), i_raster.getHeight(), ColorSpace.TYPE_RGB);
\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
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
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
155 g.drawImage(sink, ins.left, ins.top, this);
\r
163 } catch (Exception e) {
\r
164 e.printStackTrace();
\r
168 public void onUpdateBuffer(Buffer i_buffer)
\r
174 synchronized(this._capraster){
\r
175 this._capraster.setBuffer(i_buffer);
\r
176 draw(this._capraster);
\r
179 } catch (Exception e) {
\r
180 e.printStackTrace();
\r
184 public void startCapture()
\r
187 this._capture.start();
\r
188 } catch (Exception e) {
\r
189 e.printStackTrace();
\r
193 public void startImage()
\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
201 // INyARRgbRaster ra = NyARRgbRaster_BGRA.wrap(buf, W, H);
\r
203 } catch (Exception e) {
\r
204 e.printStackTrace();
\r
209 public static void main(String[] args)
\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