2 * Capture Test NyARToolkitCSサンプルプログラム
\r
3 * --------------------------------------------------------------------------------
\r
4 * Copyright (C)2008 R.Iizuka
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
11 * This program is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this framework; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * For further information please contact.
\r
21 * http://nyatla.jp/nyatoolkit/
\r
22 * <airmail(at)ebony.plala.or.jp>
\r
25 package jp.nyatla.nyartoolkit.processor;
\r
27 import jp.nyatla.nyartoolkit.NyARException;
\r
28 import jp.nyatla.nyartoolkit.core.*;
\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.*;
\r
34 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;
\r
35 import jp.nyatla.nyartoolkit.core.types.*;
\r
36 import jp.nyatla.nyartoolkit.nyidmarker.*;
\r
37 import jp.nyatla.nyartoolkit.nyidmarker.data.*;
\r
38 import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*;
\r
40 public abstract class SingleNyIdMarkerProcesser
\r
43 * オーナーが自由に使えるタグ変数です。
\r
50 private int _lost_delay_count = 0;
\r
51 private int _lost_delay = 5;
\r
53 private NyARSquareDetector _square_detect;
\r
54 protected NyARTransMat _transmat;
\r
55 private double _marker_width=100;
\r
57 private NyARSquareStack _square_list = new NyARSquareStack(100);
\r
58 private INyIdMarkerDataEncoder _encoder;
\r
59 private boolean _is_active;
\r
60 private INyIdMarkerData _data_temp;
\r
61 private INyIdMarkerData _data_current;
\r
63 private int _current_threshold;
\r
65 private NyARBinRaster _bin_raster;
\r
67 private NyARRasterFilter_ARToolkitThreshold _tobin_filter = new NyARRasterFilter_ARToolkitThreshold(110);
\r
69 private NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();
\r
72 protected SingleNyIdMarkerProcesser(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,int i_raster_format) throws NyARException
\r
74 NyARIntSize scr_size = i_param.getScreenSize();
\r
76 this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(), scr_size);
\r
77 this._transmat = new NyARTransMat(i_param);
\r
78 this._encoder=i_encoder;
\r
81 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
82 //ワーク用のデータオブジェクトを2個作る
\r
83 this._is_active=false;
\r
84 this._data_temp=i_encoder.createDataInstance();
\r
85 this._data_current=i_encoder.createDataInstance();
\r
86 this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format);
\r
90 public void setMarkerWidth(int i_width)
\r
92 this._marker_width=i_width;
\r
96 public void reset(boolean i_is_force)
\r
98 if (this._data_current!=null && i_is_force == false) {
\r
99 // 強制書き換えでなければイベントコール
\r
100 this.onLeaveHandler();
\r
103 this._data_current = null;
\r
107 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
110 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {
\r
111 throw new NyARException();
\r
113 // ラスタを2値イメージに変換する.
\r
114 this._tobin_filter.setThreshold(this._current_threshold);
\r
115 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
117 NyARSquareStack square_stack = this._square_list;
\r
119 this._square_detect.detectMarker(this._bin_raster, square_stack);
\r
121 if (!this._is_active) {
\r
123 detectNewMarker(i_raster, square_stack);
\r
126 detectExistMarker(i_raster, square_stack);
\r
128 System.out.println(this._current_threshold);
\r
133 private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();
\r
134 private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();
\r
135 private final NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;
\r
137 /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。
\r
139 private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
141 NyIdMarkerParam param=this._marker_param;
\r
142 NyIdMarkerPattern patt_data =this._marker_data;
\r
143 int number_of_square = i_stack.getLength();
\r
144 int square_index = 0;
\r
145 INyIdMarkerData marker_id=null;
\r
146 for (int i = 0; i < number_of_square; i++) {
\r
147 // 評価基準になるパターンをイメージから切り出す
\r
148 if (!this._id_pickup.pickFromRaster(i_raster, (NyARSquare) i_stack.getItem(i), patt_data, param)) {
\r
152 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
155 //認識率が一番高いもの(占有面積が一番大きいもの)を選択する(省略)
\r
157 marker_id=this._data_temp;
\r
162 final boolean is_id_found=updateStatus((NyARSquare) this._square_list.getItem(square_index),marker_id, param);
\r
164 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
166 //マーカがあれば、マーカの周辺閾値を反映
\r
167 this._current_threshold=(this._current_threshold+param.threshold)/2;
\r
169 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
170 this._threshold_detect.analyzeRaster(i_raster);
\r
171 this._current_threshold=(this._current_threshold+this._threshold_detect.getThreshold())/2;
\r
176 /**マーカの継続認識 現在認識中のマーカを優先して認識します。
\r
177 * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。
\r
179 private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
181 NyIdMarkerParam param=this._marker_param;
\r
182 NyIdMarkerPattern patt_data =this._marker_data;
\r
183 int number_of_square = i_stack.getLength();
\r
184 int square_index = 0;
\r
185 INyIdMarkerData marker_id=null;
\r
186 for (int i = 0; i < number_of_square; i++){
\r
188 if (!this._id_pickup.pickFromRaster(i_raster, (NyARSquare) i_stack.getItem(i), patt_data, param)) {
\r
191 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
195 if(!this._data_current.isEqual((this._data_temp))){
\r
199 marker_id=this._data_temp;
\r
203 final boolean is_id_found=updateStatus((NyARSquare) this._square_list.getItem(square_index),marker_id,param);
\r
205 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
207 //マーカがあれば、マーカの周辺閾値を反映
\r
208 this._current_threshold=(this._current_threshold+param.threshold)/2;
\r
210 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
211 this._threshold_detect.analyzeRaster(i_raster);
\r
212 this._current_threshold=(this._current_threshold+this._threshold_detect.getThreshold())/2;
\r
217 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
219 /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
221 private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data,NyIdMarkerParam i_param) throws NyARException
\r
223 boolean is_id_found=false;
\r
224 NyARTransMatResult result = this.__NyARSquare_result;
\r
225 if (!this._is_active) {// 未認識中
\r
226 if (i_marker_data==null) {// 未認識から未認識の遷移
\r
228 this._is_active=false;
\r
229 } else {// 未認識から認識の遷移
\r
230 this._data_current.copyFrom(i_marker_data);
\r
233 this.onEnterHandler(this._data_current);
\r
235 this._transmat.transMat(i_square,i_param.direction, this._marker_width, result);
\r
237 this.onUpdateHandler(i_square, result);
\r
238 this._lost_delay_count = 0;
\r
239 this._is_active=true;
\r
243 if (i_marker_data==null) {
\r
245 this._lost_delay_count++;
\r
246 if (this._lost_delay < this._lost_delay_count) {
\r
248 this.onLeaveHandler();
\r
249 this._is_active=false;
\r
251 } else if(this._data_current.isEqual(i_marker_data)) {
\r
253 this._transmat.transMat(i_square, i_param.direction, this._marker_width, result);
\r
255 this.onUpdateHandler(i_square, result);
\r
256 this._lost_delay_count = 0;
\r
258 } else {// 異なるコードの認識→今はサポートしない。
\r
259 throw new NyARException();
\r
262 return is_id_found;
\r
265 protected abstract void onEnterHandler(INyIdMarkerData i_code);
\r
266 protected abstract void onLeaveHandler();
\r
267 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r