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.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
30 * 矩形の追跡は動いてるから、位置予測機能と組み合わせて試すこと。
\r
33 class Tracking extends MarkerTracking_3dTrans
\r
35 Tracking(NyARParam i_param,NyARCode i_code,double i_marker_width,int i_input_raster_type) throws NyARException
\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
49 public class TrTest extends Frame implements JmfCaptureListener,MouseMotionListener
\r
53 private final String PARAM_FILE = "../Data/camera_para.dat";
\r
55 private final static String CARCODE_FILE = "../Data/patt.hiro";
\r
57 private static final long serialVersionUID = -2110888320986446576L;
\r
59 private JmfCaptureDevice _capture;
\r
61 private GLNyARRaster_RGB _capraster;
\r
63 private int W = 320;
\r
65 private int H = 240;
\r
67 private Tracking _tr;
\r
68 TransMat2MarkerRect _trm;
\r
70 public TrTest() throws NyARException
\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
82 NyARParam ar_param = new NyARParam();
\r
83 ar_param.loadARParamFromFile(PARAM_FILE);
\r
84 ar_param.changeScreenSize(W, H);
\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
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
98 public void mouseMoved(MouseEvent A00)
\r
100 mouse_x = A00.getX();
\r
101 mouse_y = A00.getY();
\r
104 public void mouseDragged(MouseEvent A00) {}
\r
108 private final String data_file = "../Data/320x240ABGR.raw";
\r
112 public void draw(GLNyARRaster_RGB i_raster)
\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
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
128 // NyARTransMatResult res = new NyARTransMatResult();
\r
129 // this.detect.getTransmationMatrix(res);
\r
133 // INyARRasterFilter_RgbToBin filter=new NyARRasterFilter_ARToolkitThreshold(110,i_raster.getBufferReader().getBufferType());
\r
134 // NyARSquareStack stack=new NyARSquareStack(10);
\r
136 BufferedImage sink = new BufferedImage(i_raster.getWidth(), i_raster.getHeight(), ColorSpace.TYPE_RGB);
\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
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
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
156 g.drawImage(sink, ins.left, ins.top, this);
\r
164 } catch (Exception e) {
\r
165 e.printStackTrace();
\r
169 public void onUpdateBuffer(Buffer i_buffer)
\r
175 synchronized(this._capraster){
\r
176 this._capraster.setBuffer(i_buffer);
\r
177 draw(this._capraster);
\r
180 } catch (Exception e) {
\r
181 e.printStackTrace();
\r
185 public void startCapture()
\r
188 this._capture.start();
\r
189 } catch (Exception e) {
\r
190 e.printStackTrace();
\r
194 public void startImage()
\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
202 // INyARRgbRaster ra = NyARRgbRaster_BGRA.wrap(buf, W, H);
\r
204 } catch (Exception e) {
\r
205 e.printStackTrace();
\r
210 public static void main(String[] args)
\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