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.types.*;
\r
35 import jp.nyatla.nyartoolkit.nyidmarker.*;
\r
36 import jp.nyatla.nyartoolkit.nyidmarker.data.INyIdMarkerData;
\r
37 import jp.nyatla.nyartoolkit.nyidmarker.data.INyIdMarkerDataEncoder;
\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
64 private int _base_threshold = 110;
\r
65 private int _current_threshold;
\r
67 private NyARBinRaster _bin_raster;
\r
69 private NyARRasterFilter_ARToolkitThreshold _tobin_filter = new NyARRasterFilter_ARToolkitThreshold(110);
\r
71 private NyARIdMarkerPickup _id_pickup = new NyARIdMarkerPickup();
\r
74 protected SingleNyIdMarkerProcesser(NyARParam i_param,INyIdMarkerDataEncoder i_encoder) throws NyARException
\r
76 NyARIntSize scr_size = i_param.getScreenSize();
\r
78 this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(), scr_size);
\r
79 this._transmat = new NyARTransMat(i_param);
\r
80 this._encoder=i_encoder;
\r
83 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
84 //ワーク用のデータオブジェクトを2個作る
\r
85 this._is_active=false;
\r
86 this._data_temp=i_encoder.createDataInstance();
\r
87 this._data_current=i_encoder.createDataInstance();
\r
91 public void setBaseThreshold(int i_threshold)
\r
93 this._base_threshold = i_threshold;
\r
96 public void setMarkerWidth(int i_width)
\r
98 this._marker_width=i_width;
\r
102 public void reset(boolean i_is_force)
\r
104 if (this._data_current!=null && i_is_force == false) {
\r
105 // 強制書き換えでなければイベントコール
\r
106 this.onLeaveHandler();
\r
109 this._data_current = null;
\r
113 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
116 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {
\r
117 throw new NyARException();
\r
119 // ラスタを2値イメージに変換する.
\r
120 this._tobin_filter.setThreshold(this._current_threshold);
\r
121 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
123 NyARSquareStack square_stack = this._square_list;
\r
125 this._square_detect.detectMarker(this._bin_raster, square_stack);
\r
127 if (!this._is_active) {
\r
129 detectNewMarker(i_raster, square_stack);
\r
132 detectExistMarker(i_raster, square_stack);
\r
138 private final NyARIdMarkerPattern _marker_data=new NyARIdMarkerPattern();
\r
139 private final NyARIdMarkerParam _marker_param=new NyARIdMarkerParam();
\r
143 /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。
\r
145 private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
147 NyARIdMarkerParam param=this._marker_param;
\r
148 NyARIdMarkerPattern patt_data =this._marker_data;
\r
149 int number_of_square = i_stack.getLength();
\r
150 int square_index = 0;
\r
151 INyIdMarkerData marker_id=null;
\r
152 for (int i = 0; i < number_of_square; i++) {
\r
153 // 評価基準になるパターンをイメージから切り出す
\r
154 if (!this._id_pickup.pickFromRaster(i_raster, (NyARSquare) i_stack.getItem(i), patt_data, param)) {
\r
158 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
161 //認識率が一番高いもの(占有面積が一番大きいもの)を選択する(省略)
\r
163 marker_id=this._data_temp;
\r
167 updateStatus((NyARSquare) this._square_list.getItem(square_index),marker_id, param);
\r
170 /**マーカの継続認識 現在認識中のマーカを優先して認識します。
\r
171 * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。
\r
173 private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
175 NyARIdMarkerParam param=this._marker_param;
\r
176 NyARIdMarkerPattern patt_data =this._marker_data;
\r
177 int number_of_square = i_stack.getLength();
\r
178 int square_index = 0;
\r
179 INyIdMarkerData marker_id=null;
\r
180 for (int i = 0; i < number_of_square; i++){
\r
182 if (!this._id_pickup.pickFromRaster(i_raster, (NyARSquare) i_stack.getItem(i), patt_data, param)) {
\r
185 if(!this._encoder.encode(patt_data,this._data_temp)){
\r
189 if(!this._data_current.isEqual((this._data_temp))){
\r
193 marker_id=this._data_temp;
\r
197 updateStatus((NyARSquare) this._square_list.getItem(square_index),marker_id,param);
\r
200 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
202 /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
204 private void updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data,NyARIdMarkerParam i_param) throws NyARException
\r
206 boolean is_id_found=false;
\r
207 NyARTransMatResult result = this.__NyARSquare_result;
\r
208 if (!this._is_active) {// 未認識中
\r
209 if (i_marker_data==null) {// 未認識から未認識の遷移
\r
211 this._is_active=false;
\r
212 } else {// 未認識から認識の遷移
\r
213 this._data_current.copyFrom(i_marker_data);
\r
216 this.onEnterHandler(this._data_current);
\r
218 this._transmat.transMat(i_square,i_param.direction, this._marker_width, result);
\r
220 this.onUpdateHandler(i_square, result);
\r
221 this._lost_delay_count = 0;
\r
222 this._is_active=true;
\r
226 if (i_marker_data==null) {
\r
228 this._lost_delay_count++;
\r
229 if (this._lost_delay < this._lost_delay_count) {
\r
231 this.onLeaveHandler();
\r
232 this._is_active=false;
\r
234 } else if(this._data_current.isEqual(i_marker_data)) {
\r
236 this._transmat.transMat(i_square, i_param.direction, this._marker_width, result);
\r
238 this.onUpdateHandler(i_square, result);
\r
239 this._lost_delay_count = 0;
\r
241 } else {// 異なるコードの認識→今はサポートしない。
\r
242 throw new NyARException();
\r
247 //マーカがあれば、マーカの周辺閾値を反映
\r
248 this._current_threshold=(this._current_threshold+i_param.threshold)/2;
\r
250 //マーカがなければ、探索+DualPTailで基準輝度検索(省略)
\r
251 this._current_threshold=(this._current_threshold+this._base_threshold)/2;
\r
256 protected abstract void onEnterHandler(INyIdMarkerData i_code);
\r
257 protected abstract void onLeaveHandler();
\r
258 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r