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 * オーナーが自由に使えるタグ変数です。
\r
49 private int _lost_delay_count = 0;
\r
50 private int _lost_delay = 5;
\r
52 private NyARSquareDetector_Rle _square_detect;
\r
53 protected INyARTransMat _transmat;
\r
54 private double _marker_width=100;
\r
56 private NyARSquareStack _square_list = new NyARSquareStack(100);
\r
57 private INyIdMarkerDataEncoder _encoder;
\r
58 private boolean _is_active;
\r
59 private INyIdMarkerData _data_temp;
\r
60 private INyIdMarkerData _data_current;
\r
62 private int _current_threshold=110;
\r
64 private NyARBinRaster _bin_raster;
\r
66 private NyARRasterFilter_ARToolkitThreshold _tobin_filter;
\r
68 private NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();
\r
71 protected SingleNyIdMarkerProcesser()
\r
75 private boolean _initialized=false;
\r
76 protected void initInstance(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,int i_raster_format) throws NyARException
\r
79 assert this._initialized==false;
\r
81 NyARIntSize scr_size = i_param.getScreenSize();
\r
83 this._square_detect = new NyARSquareDetector_Rle(i_param.getDistortionFactor(), scr_size);
\r
84 this._transmat = new NyARTransMat(i_param);
\r
85 this._encoder=i_encoder;
\r
88 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
89 //ワーク用のデータオブジェクトを2個作る
\r
90 this._is_active=false;
\r
91 this._data_temp=i_encoder.createDataInstance();
\r
92 this._data_current=i_encoder.createDataInstance();
\r
93 this._tobin_filter =new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);
\r
94 this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format,4);
\r
95 this._initialized=true;
\r
100 public void setMarkerWidth(int i_width)
\r
102 this._marker_width=i_width;
\r
106 public void reset(boolean i_is_force)
\r
108 if (this._data_current!=null && i_is_force == false) {
\r
109 // 強制書き換えでなければイベントコール
\r
110 this.onLeaveHandler();
\r
113 this._data_current = null;
\r
117 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
120 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {
\r
121 throw new NyARException();
\r
123 // ラスタを2値イメージに変換する.
\r
124 this._tobin_filter.setThreshold(this._current_threshold);
\r
125 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
127 NyARSquareStack square_stack = this._square_list;
\r
129 this._square_detect.detectMarker(this._bin_raster, square_stack);
\r
131 if (!this._is_active) {
\r
133 detectNewMarker(i_raster, square_stack);
\r
136 detectExistMarker(i_raster, square_stack);
\r
142 private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();
\r
143 private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();
\r
144 private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;
\r
146 /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。
\r
148 private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
150 NyIdMarkerParam param=this._marker_param;
\r
151 NyIdMarkerPattern patt_data =this._marker_data;
\r
152 int number_of_square = i_stack.getLength();
\r
153 NyARSquare current_square=null;
\r
154 INyIdMarkerData marker_id=null;
\r
155 for (int i = 0; i < number_of_square; i++) {
\r
156 // 評価基準になるパターンをイメージから切り出す
\r
157 current_square=i_stack.getItem(i);
\r
158 if (!this._id_pickup.pickFromRaster(i_raster,current_square, patt_data, param)) {
\r
162 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
165 //認識率が一番高いもの(占有面積が一番大きいもの)を選択する(省略)
\r
167 marker_id=this._data_temp;
\r
172 final boolean is_id_found=updateStatus(current_square,marker_id, param);
\r
174 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
176 //マーカがあれば、マーカの周辺閾値を反映
\r
177 this._current_threshold=(this._current_threshold+param.threshold)/2;
\r
179 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
180 int th=this._threshold_detect.analyzeRaster(i_raster);
\r
181 this._current_threshold=(this._current_threshold+th)/2;
\r
186 /**マーカの継続認識 現在認識中のマーカを優先して認識します。
\r
187 * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。
\r
189 private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
191 NyIdMarkerParam param=this._marker_param;
\r
192 NyIdMarkerPattern patt_data =this._marker_data;
\r
193 int number_of_square = i_stack.getLength();
\r
194 NyARSquare current_square=null;
\r
195 INyIdMarkerData marker_id=null;
\r
196 for (int i = 0; i < number_of_square; i++){
\r
198 current_square=i_stack.getItem(i);
\r
199 if (!this._id_pickup.pickFromRaster(i_raster, current_square, patt_data, param)) {
\r
202 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
206 if(!this._data_current.isEqual((this._data_temp))){
\r
210 marker_id=this._data_temp;
\r
214 final boolean is_id_found=updateStatus(current_square,marker_id,param);
\r
216 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
218 //マーカがあれば、マーカの周辺閾値を反映
\r
219 this._current_threshold=(this._current_threshold+param.threshold)/2;
\r
221 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
222 int th=this._threshold_detect.analyzeRaster(i_raster);
\r
223 this._current_threshold=(this._current_threshold+th)/2;
\r
228 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
230 /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
232 private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data,NyIdMarkerParam i_param) throws NyARException
\r
234 boolean is_id_found=false;
\r
235 NyARTransMatResult result = this.__NyARSquare_result;
\r
236 if (!this._is_active) {// 未認識中
\r
237 if (i_marker_data==null) {// 未認識から未認識の遷移
\r
239 this._is_active=false;
\r
240 } else {// 未認識から認識の遷移
\r
241 this._data_current.copyFrom(i_marker_data);
\r
244 this.onEnterHandler(this._data_current);
\r
246 this._transmat.transMat(i_square,i_param.direction, this._marker_width, result);
\r
248 this.onUpdateHandler(i_square, result);
\r
249 this._lost_delay_count = 0;
\r
250 this._is_active=true;
\r
254 if (i_marker_data==null) {
\r
256 this._lost_delay_count++;
\r
257 if (this._lost_delay < this._lost_delay_count) {
\r
259 this.onLeaveHandler();
\r
260 this._is_active=false;
\r
262 } else if(this._data_current.isEqual(i_marker_data)) {
\r
264 this._transmat.transMat(i_square, i_param.direction, this._marker_width, result);
\r
266 this.onUpdateHandler(i_square, result);
\r
267 this._lost_delay_count = 0;
\r
269 } else {// 異なるコードの認識→今はサポートしない。
\r
270 throw new NyARException();
\r
273 return is_id_found;
\r
276 protected abstract void onEnterHandler(INyIdMarkerData i_code);
\r
277 protected abstract void onLeaveHandler();
\r
278 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r