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 * Rleラ矩形Detectorのブリッジ
\r
45 private class RleDetector extends NyARSquareContourDetector_Rle
\r
48 public final NyARSquare square=new NyARSquare();
\r
49 public INyIdMarkerData marker_data;
\r
50 public int threshold;
\r
54 private INyARRgbRaster _ref_raster;
\r
56 private INyIdMarkerData _current_data;
\r
57 private final NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();
\r
58 private NyARCoord2Linear _coordline;
\r
59 private INyIdMarkerDataEncoder _encoder;
\r
62 private INyIdMarkerData _data_temp;
\r
63 private INyIdMarkerData _prev_data;
\r
65 public RleDetector(NyARParam i_param,INyIdMarkerDataEncoder i_encoder) throws NyARException
\r
67 super(i_param.getScreenSize());
\r
68 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());
\r
69 this._data_temp=i_encoder.createDataInstance();
\r
70 this._current_data=i_encoder.createDataInstance();
\r
71 this._encoder=i_encoder;
\r
74 private NyARIntPoint2d[] __ref_vertex=new NyARIntPoint2d[4];
\r
76 * Initialize call back handler.
\r
78 public void init(INyARRgbRaster i_raster,INyIdMarkerData i_prev_data)
\r
80 this.marker_data=null;
\r
81 this._prev_data=i_prev_data;
\r
82 this._ref_raster=i_raster;
\r
84 private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();
\r
85 private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();
\r
88 * 矩形が見付かるたびに呼び出されます。
\r
89 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
\r
91 protected void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException
\r
94 if(this.marker_data!=null){
\r
98 NyARIntPoint2d[] vertex=this.__ref_vertex;
\r
99 vertex[0]=i_coord.items[i_vertex_index[0]];
\r
100 vertex[1]=i_coord.items[i_vertex_index[1]];
\r
101 vertex[2]=i_coord.items[i_vertex_index[2]];
\r
102 vertex[3]=i_coord.items[i_vertex_index[3]];
\r
104 NyIdMarkerParam param=this._marker_param;
\r
105 NyIdMarkerPattern patt_data =this._marker_data;
\r
106 // 評価基準になるパターンをイメージから切り出す
\r
107 if (!this._id_pickup.pickFromRaster(this._ref_raster,vertex, patt_data, param)){
\r
111 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
116 if (this._prev_data==null){
\r
118 this._current_data.copyFrom(this._data_temp);
\r
121 if(!this._prev_data.isEqual((this._data_temp))){
\r
122 return;//認識請求のあったIDと違う。
\r
125 //新しく認識、または継続認識中に更新があったときだけ、Square情報を更新する。
\r
126 //ココから先はこの条件でしか実行されない。
\r
127 NyARSquare sq=this.square;
\r
128 //directionを考慮して、squareを更新する。
\r
129 for(int i=0;i<4;i++){
\r
130 int idx=(i+4 - param.direction) % 4;
\r
131 this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coord,sq.line[i]);
\r
133 for (int i = 0; i < 4; i++) {
\r
135 if(!sq.line[i].crossPos(sq.line[(i + 3) % 4],sq.sqvertex[i])){
\r
136 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK
\r
139 this.threshold=param.threshold;
\r
140 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 RleDetector _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 INyIdMarkerData _data_current;
\r
167 protected SingleNyIdMarkerProcesser()
\r
171 private boolean _initialized=false;
\r
172 protected void initInstance(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,double i_marker_width,int i_raster_format) throws NyARException
\r
175 assert(this._initialized==false);
\r
177 NyARIntSize scr_size = i_param.getScreenSize();
\r
179 this._square_detect = new RleDetector(i_param,i_encoder);
\r
180 this._transmat = new NyARTransMat(i_param);
\r
183 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
184 //ワーク用のデータオブジェクトを2個作る
\r
185 this._data_current=i_encoder.createDataInstance();
\r
186 this._tobin_filter =new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);
\r
187 this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format,4);
\r
188 this._initialized=true;
\r
189 this._is_active=false;
\r
190 this._offset=new NyARRectOffset();
\r
191 this._offset.setSquare(i_marker_width);
\r
196 public void setMarkerWidth(int i_width)
\r
198 this._offset.setSquare(i_width);
\r
202 public void reset(boolean i_is_force)
\r
204 if (i_is_force == false && this._is_active){
\r
205 // 強制書き換えでなければイベントコール
\r
206 this.onLeaveHandler();
\r
209 this._is_active=false;
\r
213 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
216 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {
\r
217 throw new NyARException();
\r
219 // ラスタを2値イメージに変換する.
\r
220 this._tobin_filter.setThreshold(this._current_threshold);
\r
221 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
223 // スクエアコードを探す(第二引数に指定したマーカ、もしくは新しいマーカを探す。)
\r
224 this._square_detect.init(i_raster,this._is_active?this._data_current:null);
\r
225 this._square_detect.detectMarker(this._bin_raster);
\r
227 // 認識状態を更新(マーカを発見したなら、current_dataを渡すかんじ)
\r
228 final boolean is_id_found=updateStatus(this._square_detect.square,this._square_detect.marker_data);
\r
230 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
232 //マーカがあれば、マーカの周辺閾値を反映
\r
233 this._current_threshold=(this._current_threshold+this._square_detect.threshold)/2;
\r
235 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
236 int th=this._threshold_detect.analyzeRaster(i_raster);
\r
237 this._current_threshold=(this._current_threshold+th)/2;
\r
243 private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;
\r
244 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
246 /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
248 private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data) throws NyARException
\r
250 boolean is_id_found=false;
\r
251 NyARTransMatResult result = this.__NyARSquare_result;
\r
252 if (!this._is_active) {// 未認識中
\r
253 if (i_marker_data==null) {// 未認識から未認識の遷移
\r
255 this._is_active=false;
\r
256 } else {// 未認識から認識の遷移
\r
257 this._data_current.copyFrom(i_marker_data);
\r
260 this.onEnterHandler(this._data_current);
\r
262 this._transmat.transMat(i_square, this._offset, result);
\r
264 this.onUpdateHandler(i_square, result);
\r
265 this._lost_delay_count = 0;
\r
266 this._is_active=true;
\r
270 if (i_marker_data==null) {
\r
272 this._lost_delay_count++;
\r
273 if (this._lost_delay < this._lost_delay_count) {
\r
275 this.onLeaveHandler();
\r
276 this._is_active=false;
\r
278 } else if(this._data_current.isEqual(i_marker_data)) {
\r
280 this._transmat.transMatContinue(i_square, this._offset, result,result);
\r
282 this.onUpdateHandler(i_square, result);
\r
283 this._lost_delay_count = 0;
\r
285 } else {// 異なるコードの認識→今はサポートしない。
\r
286 throw new NyARException();
\r
289 return is_id_found;
\r
292 protected abstract void onEnterHandler(INyIdMarkerData i_code);
\r
293 protected abstract void onLeaveHandler();
\r
294 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r