2 * Capture Test NyARToolkitサンプルプログラム
\r
3 * --------------------------------------------------------------------------------
\r
4 * The NyARToolkit is Java edition ARToolKit class library.
\r
5 * Copyright (C)2008-2009 Ryo Iizuka
\r
7 * This program is free software: you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation, either version 3 of the License, or
\r
10 * (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
20 * For further information please contact.
\r
21 * http://nyatla.jp/nyatoolkit/
\r
22 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
25 package jp.nyatla.nyartoolkit.processor;
\r
27 import jp.nyatla.nyartoolkit.NyARException;
\r
28 import jp.nyatla.nyartoolkit.core.analyzer.raster.threshold.*;
\r
29 import jp.nyatla.nyartoolkit.core.param.*;
\r
30 import jp.nyatla.nyartoolkit.core.raster.*;
\r
31 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
32 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
33 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;
\r
34 import jp.nyatla.nyartoolkit.core.types.*;
\r
35 import jp.nyatla.nyartoolkit.nyidmarker.*;
\r
36 import jp.nyatla.nyartoolkit.nyidmarker.data.*;
\r
37 import jp.nyatla.nyartoolkit.core.squaredetect.*;
\r
39 public abstract class SingleNyIdMarkerProcesser
\r
42 * detectMarkerのコールバック関数
\r
44 private class DetectSquareCB implements NyARSquareContourDetector.IDetectMarkerCallback
\r
47 public final NyARSquare square=new NyARSquare();
\r
48 public INyIdMarkerData marker_data;
\r
49 public int threshold;
\r
53 private INyARRgbRaster _ref_raster;
\r
55 private INyIdMarkerData _current_data;
\r
56 private final NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();
\r
57 private NyARCoord2Linear _coordline;
\r
58 private INyIdMarkerDataEncoder _encoder;
\r
61 private INyIdMarkerData _data_temp;
\r
62 private INyIdMarkerData _prev_data;
\r
64 public DetectSquareCB(NyARParam i_param,INyIdMarkerDataEncoder i_encoder)
\r
66 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());
\r
67 this._data_temp=i_encoder.createDataInstance();
\r
68 this._current_data=i_encoder.createDataInstance();
\r
69 this._encoder=i_encoder;
\r
72 private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4);
\r
74 * Initialize call back handler.
\r
76 public void init(INyARRgbRaster i_raster,INyIdMarkerData i_prev_data)
\r
78 this.marker_data=null;
\r
79 this._prev_data=i_prev_data;
\r
80 this._ref_raster=i_raster;
\r
82 private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();
\r
83 private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();
\r
86 * 矩形が見付かるたびに呼び出されます。
\r
87 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
\r
89 public void onSquareDetect(NyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException
\r
92 if(this.marker_data!=null){
\r
96 NyARIntPoint2d[] vertex=this.__tmp_vertex;
\r
97 vertex[0].x=i_coordx[i_vertex_index[0]];
\r
98 vertex[0].y=i_coordy[i_vertex_index[0]];
\r
99 vertex[1].x=i_coordx[i_vertex_index[1]];
\r
100 vertex[1].y=i_coordy[i_vertex_index[1]];
\r
101 vertex[2].x=i_coordx[i_vertex_index[2]];
\r
102 vertex[2].y=i_coordy[i_vertex_index[2]];
\r
103 vertex[3].x=i_coordx[i_vertex_index[3]];
\r
104 vertex[3].y=i_coordy[i_vertex_index[3]];
\r
106 NyIdMarkerParam param=this._marker_param;
\r
107 NyIdMarkerPattern patt_data =this._marker_data;
\r
108 // 評価基準になるパターンをイメージから切り出す
\r
109 if (!this._id_pickup.pickFromRaster(this._ref_raster,vertex, patt_data, param)){
\r
113 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
118 if (this._prev_data==null){
\r
120 this._current_data.copyFrom(this._data_temp);
\r
123 if(!this._prev_data.isEqual((this._data_temp))){
\r
124 return;//認識請求のあったIDと違う。
\r
127 //新しく認識、または継続認識中に更新があったときだけ、Square情報を更新する。
\r
128 //ココから先はこの条件でしか実行されない。
\r
129 NyARSquare sq=this.square;
\r
130 //directionを考慮して、squareを更新する。
\r
131 for(int i=0;i<4;i++){
\r
132 int idx=(i+4 - param.direction) % 4;
\r
133 this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]);
\r
135 for (int i = 0; i < 4; i++) {
\r
137 if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){
\r
138 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK
\r
141 this.threshold=param.threshold;
\r
142 this.marker_data=this._current_data;//みつかった。
\r
146 * オーナーが自由に使えるタグ変数です。
\r
153 private int _lost_delay_count = 0;
\r
154 private int _lost_delay = 5;
\r
156 private NyARSquareContourDetector_Rle _square_detect;
\r
157 protected INyARTransMat _transmat;
\r
158 private NyARRectOffset _offset;
\r
159 private boolean _is_active;
\r
160 private int _current_threshold=110;
\r
162 private NyARBinRaster _bin_raster;
\r
163 private NyARRasterFilter_ARToolkitThreshold _tobin_filter;
\r
164 private DetectSquareCB _callback;
\r
165 private INyIdMarkerData _data_current;
\r
168 protected SingleNyIdMarkerProcesser()
\r
172 private boolean _initialized=false;
\r
173 protected void initInstance(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,double i_marker_width,int i_raster_format) throws NyARException
\r
176 assert(this._initialized==false);
\r
178 NyARIntSize scr_size = i_param.getScreenSize();
\r
180 this._square_detect = new NyARSquareContourDetector_Rle(scr_size);
\r
181 this._transmat = new NyARTransMat(i_param);
\r
182 this._callback=new DetectSquareCB(i_param,i_encoder);
\r
185 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
186 //ワーク用のデータオブジェクトを2個作る
\r
187 this._data_current=i_encoder.createDataInstance();
\r
188 this._tobin_filter =new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);
\r
189 this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format,4);
\r
190 this._initialized=true;
\r
191 this._is_active=false;
\r
192 this._offset=new NyARRectOffset();
\r
193 this._offset.setSquare(i_marker_width);
\r
198 public void setMarkerWidth(int i_width)
\r
200 this._offset.setSquare(i_width);
\r
204 public void reset(boolean i_is_force)
\r
206 if (i_is_force == false && this._is_active){
\r
207 // 強制書き換えでなければイベントコール
\r
208 this.onLeaveHandler();
\r
211 this._is_active=false;
\r
215 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
218 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {
\r
219 throw new NyARException();
\r
221 // ラスタを2値イメージに変換する.
\r
222 this._tobin_filter.setThreshold(this._current_threshold);
\r
223 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
225 // スクエアコードを探す(第二引数に指定したマーカ、もしくは新しいマーカを探す。)
\r
226 this._callback.init(i_raster,this._is_active?this._data_current:null);
\r
227 this._square_detect.detectMarkerCB(this._bin_raster, this._callback);
\r
229 // 認識状態を更新(マーカを発見したなら、current_dataを渡すかんじ)
\r
230 final boolean is_id_found=updateStatus(this._callback.square,this._callback.marker_data);
\r
232 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
234 //マーカがあれば、マーカの周辺閾値を反映
\r
235 this._current_threshold=(this._current_threshold+this._callback.threshold)/2;
\r
237 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
238 int th=this._threshold_detect.analyzeRaster(i_raster);
\r
239 this._current_threshold=(this._current_threshold+th)/2;
\r
245 private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;
\r
246 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
248 /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
250 private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data) throws NyARException
\r
252 boolean is_id_found=false;
\r
253 NyARTransMatResult result = this.__NyARSquare_result;
\r
254 if (!this._is_active) {// 未認識中
\r
255 if (i_marker_data==null) {// 未認識から未認識の遷移
\r
257 this._is_active=false;
\r
258 } else {// 未認識から認識の遷移
\r
259 this._data_current.copyFrom(i_marker_data);
\r
262 this.onEnterHandler(this._data_current);
\r
264 this._transmat.transMat(i_square, this._offset, result);
\r
266 this.onUpdateHandler(i_square, result);
\r
267 this._lost_delay_count = 0;
\r
268 this._is_active=true;
\r
272 if (i_marker_data==null) {
\r
274 this._lost_delay_count++;
\r
275 if (this._lost_delay < this._lost_delay_count) {
\r
277 this.onLeaveHandler();
\r
278 this._is_active=false;
\r
280 } else if(this._data_current.isEqual(i_marker_data)) {
\r
282 this._transmat.transMatContinue(i_square, this._offset, result);
\r
284 this.onUpdateHandler(i_square, result);
\r
285 this._lost_delay_count = 0;
\r
287 } else {// 異なるコードの認識→今はサポートしない。
\r
288 throw new NyARException();
\r
291 return is_id_found;
\r
294 protected abstract void onEnterHandler(INyIdMarkerData i_code);
\r
295 protected abstract void onLeaveHandler();
\r
296 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r