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.sandbox.x2;
\r
27 import jp.nyatla.nyartoolkit.NyARException;
\r
28 import jp.nyatla.nyartoolkit.core.*;
\r
29 import jp.nyatla.nyartoolkit.core.analyzer.threshold.NyARRasterThresholdAnalyzerBuilder_Threshold;
\r
30 import jp.nyatla.nyartoolkit.core.match.*;
\r
31 import jp.nyatla.nyartoolkit.core.param.*;
\r
32 import jp.nyatla.nyartoolkit.core.pickup.*;
\r
33 import jp.nyatla.nyartoolkit.core.raster.*;
\r
34 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
35 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
36 import jp.nyatla.nyartoolkit.core.rasterfilter.gs2bin.*;
\r
37 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilterBuilder_RgbToBin;
\r
38 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;
\r
39 import jp.nyatla.nyartoolkit.core.types.*;
\r
40 import jp.nyatla.nyartoolkit.core.squaredetect.*;
\r
43 * このクラスは、同時に1個のマーカを処理することのできる、アプリケーションプロセッサです。
\r
44 * マーカの出現・移動・消滅を、イベントで通知することができます。
\r
45 * クラスには複数のマーカを登録できます。一つのマーカが見つかると、プロセッサは継続して同じマーカを
\r
46 * 1つだけ認識し続け、見失うまでの間は他のマーカを認識しません。
\r
48 * イベントは、 OnEnter→OnUpdate[n]→OnLeaveの順で発生します。
\r
49 * マーカが見つかるとまずOnEnterが1度発生して、何番のマーカが発見されたかがわかります。
\r
50 * 次にOnUpdateにより、現在の変換行列が連続して渡されます。最後にマーカを見失うと、OnLeave
\r
54 public abstract class SingleARMarkerProcesser_X2
\r
56 /**selectARCodeIndexFromListが値を返す時に使う変数型です。
\r
59 private class TResult_selectARCodeIndex
\r
61 public int direction;
\r
63 public double confidence;
\r
65 public int code_index;
\r
67 /**オーナーが自由に使えるタグ変数です。
\r
71 private int _lost_delay_count = 0;
\r
73 private int _lost_delay = 5;
\r
75 private INyARSquareContourDetector _square_detect;
\r
78 protected NyARTransMat_X2 _transmat;
\r
80 private double _marker_width;
\r
82 private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;
\r
84 private NyARSquareStack _square_list = new NyARSquareStack(100);
\r
86 private INyARColorPatt _patt = null;
\r
88 private double _cf_threshold_new = 0.30;
\r
89 private double _cf_threshold_exist = 0.15;
\r
91 private int _threshold = 110;
\r
93 private NyARBinRaster _bin_raster;
\r
95 private NyARRasterFilter_ARToolkitThreshold _tobin_filter;
\r
97 protected int _current_arcode_index = -1;
\r
99 private NyARMatchPattDeviationColorData _deviation_data;
\r
100 private NyARRasterThresholdAnalyzerBuilder_Threshold _threshold_detect;
\r
102 protected SingleARMarkerProcesser_X2()
\r
108 protected void initInstance(NyARParam i_param,int i_raster_type) throws NyARException
\r
110 NyARIntSize scr_size = i_param.getScreenSize();
\r
113 this._square_detect = new NyARSquareDetector_X2(i_param.getDistortionFactor(), scr_size);
\r
114 this._transmat = new NyARTransMat_X2(i_param);
\r
117 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(110,i_raster_type);
\r
120 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
\r
121 this._threshold_detect=new NyARRasterThresholdAnalyzerBuilder_Threshold(15,i_raster_type,4);
\r
125 /**検出するマーカコードの配列を指定します。 検出状態でこの関数を実行すると、
\r
126 * オブジェクト状態に強制リセットがかかります。
\r
128 public void setARCodeTable(NyARCode[] i_ref_code_table, int i_code_resolution, double i_marker_width)
\r
130 if (this._current_arcode_index != -1) {
\r
134 //検出するマーカセット、情報、検出器を作り直す。(1ピクセル4ポイントサンプリング,マーカのパターン領域は50%)
\r
135 this._patt = new NyARColorPatt_Perspective_O2(i_code_resolution, i_code_resolution,4,25);
\r
136 this._deviation_data=new NyARMatchPattDeviationColorData(i_code_resolution, i_code_resolution);
\r
137 this._marker_width = i_marker_width;
\r
139 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA[i_ref_code_table.length];
\r
140 for(int i=0;i<i_ref_code_table.length;i++){
\r
141 this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code_table[i]);
\r
146 public void reset(boolean i_is_force)
\r
148 if (this._current_arcode_index != -1 && i_is_force == false) {
\r
149 // 強制書き換えでなければイベントコール
\r
150 this.onLeaveHandler();
\r
153 this._current_arcode_index = -1;
\r
157 public void detectMarker(INyARRgbRaster i_raster) throws NyARException
\r
160 assert(this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h));
\r
162 // コードテーブルが無ければここで終わり
\r
163 if (this._match_patt== null) {
\r
167 // ラスタを(1/4の画像の)2値イメージに変換する.
\r
168 this._tobin_filter.setThreshold(this._threshold);
\r
169 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
171 NyARSquareStack square_stack = this._square_list;
\r
173 this._square_detect.detectMarkerCB(this._bin_raster, square_stack);
\r
175 if (this._current_arcode_index == -1) { // マーカ未認識
\r
176 detectNewMarker(i_raster, square_stack);
\r
178 detectExistMarker(i_raster, square_stack, this._current_arcode_index);
\r
184 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
186 /**ARCodeのリストから、最も一致するコード番号を検索します。
\r
188 private boolean selectARCodeIndexFromList(INyARRgbRaster i_raster, NyARSquare i_square, TResult_selectARCodeIndex o_result) throws NyARException
\r
190 // 現在コードテーブルはアクティブ?
\r
191 if (this._match_patt==null) {
\r
194 // 評価基準になるパターンをイメージから切り出す
\r
195 if (!this._patt.pickFromRaster(i_raster, i_square.imvertex)) {
\r
198 //評価データを作成して、評価器にセット
\r
199 this._deviation_data.setRaster(this._patt);
\r
200 final NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
201 int code_index = 0;
\r
205 for (int i = 0; i < this._match_patt.length; i++) {
\r
206 this._match_patt[i].evaluate(this._deviation_data,mr);
\r
207 double c2 = mr.confidence;
\r
211 dir = mr.direction;
\r
214 o_result.code_index = code_index;
\r
215 o_result.direction = dir;
\r
216 o_result.confidence = c1;
\r
220 private TResult_selectARCodeIndex __detect_X_Marker_detect_result = new TResult_selectARCodeIndex();
\r
222 /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。
\r
224 private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException
\r
226 int number_of_square = i_stack.getLength();
\r
229 int code_index = -1;
\r
230 int square_index = 0;
\r
231 TResult_selectARCodeIndex detect_result = this.__detect_X_Marker_detect_result;
\r
232 for (int i = 0; i < number_of_square; i++) {
\r
233 if (!selectARCodeIndexFromList(i_raster, (i_stack.getItem(i)), detect_result)) {
\r
237 if (detect_result.confidence < this._cf_threshold_new) {
\r
240 if (detect_result.confidence < cf) {
\r
244 cf = detect_result.confidence;
\r
245 code_index = detect_result.code_index;
\r
247 dir = detect_result.direction;
\r
250 final boolean is_id_found=updateStatus(this._square_list.getItem(square_index), code_index, cf, dir);
\r
251 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
253 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
254 this._threshold_detect.analyzeRaster(i_raster);
\r
255 this._threshold=(this._threshold+this._threshold_detect.getThreshold())/2;
\r
259 /**マーカの継続認識 現在認識中のマーカを優先して認識します。
\r
260 * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。
\r
262 private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack, int i_current_id) throws NyARException
\r
264 int number_of_square = i_stack.getLength();
\r
267 int code_index = -1;
\r
268 int square_index = 0;
\r
269 TResult_selectARCodeIndex detect_result = this.__detect_X_Marker_detect_result;
\r
270 for (int i = 0; i < number_of_square; i++) {
\r
271 if (!selectARCodeIndexFromList(i_raster,i_stack.getItem(i), detect_result)) {
\r
276 if (detect_result.code_index != i_current_id) {
\r
280 if (detect_result.confidence < this._cf_threshold_exist) {
\r
283 if (detect_result.confidence < cf) {
\r
287 cf = detect_result.confidence;
\r
288 code_index = detect_result.code_index;
\r
289 dir = detect_result.direction;
\r
293 final boolean is_id_found=updateStatus(this._square_list.getItem(square_index), code_index, cf, dir);
\r
294 //閾値フィードバック(detectExistMarkerにもあるよ)
\r
296 //マーカがなければ、探索+DualPTailで基準輝度検索
\r
297 this._threshold_detect.analyzeRaster(i_raster);
\r
298 this._threshold=(this._threshold+this._threshold_detect.getThreshold())/2;
\r
303 private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();
\r
305 /** オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。
\r
306 * 戻り値は、「実際にマーカを発見する事ができたか」です。クラスの状態とは異なります。
\r
308 private boolean updateStatus(NyARSquare i_square, int i_code_index, double i_cf, int i_dir) throws NyARException
\r
310 NyARTransMatResult result = this.__NyARSquare_result;
\r
311 if (this._current_arcode_index < 0) {// 未認識中
\r
312 if (i_code_index < 0) {// 未認識から未認識の遷移
\r
315 } else {// 未認識から認識の遷移
\r
316 this._current_arcode_index = i_code_index;
\r
319 this.onEnterHandler(i_code_index);
\r
321 this._transmat.transMat(i_square, i_dir, this._marker_width, result);
\r
323 this.onUpdateHandler(i_square, result);
\r
324 this._lost_delay_count = 0;
\r
328 if (i_code_index < 0) {// 認識から未認識の遷移
\r
329 this._lost_delay_count++;
\r
330 if (this._lost_delay < this._lost_delay_count) {
\r
332 this._current_arcode_index = -1;
\r
333 this.onLeaveHandler();
\r
336 } else if (i_code_index == this._current_arcode_index) {// 同じARCodeの再認識
\r
339 this._transmat.transMat(i_square, i_dir, this._marker_width, result);
\r
341 this.onUpdateHandler(i_square, result);
\r
342 this._lost_delay_count = 0;
\r
344 } else {// 異なるコードの認識→今はサポートしない。
\r
345 throw new NyARException();
\r
350 protected abstract void onEnterHandler(int i_code);
\r
352 protected abstract void onLeaveHandler();
\r
354 protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);
\r