2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\r
13 * This program is free software: you can redistribute it and/or modify
\r
14 * it under the terms of the GNU General Public License as published by
\r
15 * the Free Software Foundation, either version 3 of the License, or
\r
16 * (at your option) any later version.
\r
18 * This program is distributed in the hope that it will be useful,
\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
21 * GNU General Public License for more details.
\r
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
26 * For further information please contact.
\r
27 * http://nyatla.jp/nyatoolkit/
\r
28 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
31 package jp.nyatla.nyartoolkit.detector;
\r
33 import jp.nyatla.nyartoolkit.NyARException;
\r
34 import jp.nyatla.nyartoolkit.core.*;
\r
35 import jp.nyatla.nyartoolkit.core.match.*;
\r
36 import jp.nyatla.nyartoolkit.core.param.NyARParam;
\r
37 import jp.nyatla.nyartoolkit.core.pickup.*;
\r
38 import jp.nyatla.nyartoolkit.core.raster.*;
\r
39 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
40 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
41 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
\r
42 import jp.nyatla.nyartoolkit.core.squaredetect.NyARCoord2Linear;
\r
43 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
44 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
\r
45 import jp.nyatla.nyartoolkit.core.types.*;
\r
46 import jp.nyatla.nyartoolkit.core.types.stack.NyARObjectStack;
\r
51 * このクラスは、複数のマーカを取り扱うマーカ検出器です。
\r
52 * 登録したn個のARマーカに対応するマーカを入力画像から検出し、その変換行列と一致度を返します。
\r
53 * この関数はn個の登録したマーカに対して、画像中のm個のマーカから、最も位置するものを割り当てる動作をします。
\r
54 * そのため、同一な種類(パターン)のマーカが複数存在する映像を、正しく処理できません。また、同一なマーカパターンを
\r
58 * <li>インスタンスを作成します。パラメータには、計算アルゴリズムと入力画像形式、カメラパラメータ、検出するマーカパターンテーブルがあります。
\r
59 * <li>{@link #detectMarkerLite}関数に画像と敷居値を入力して、マーカを検出します。
\r
60 * <li>検出数が得られるので、インデクス番号を元に、{@link #getConfidence}等の関数を使って、取得したマーカの状態を得ます。
\r
61 * <li>以降は、この処理を繰り返してマーカのパラメータを更新します。
\r
65 public class NyARDetectMarker
\r
68 private class RleDetector extends NyARSquareContourDetector_Rle
\r
71 public NyARDetectMarkerResultStack result_stack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX);
\r
73 public INyARRgbRaster _ref_raster;
\r
75 private INyARColorPatt _inst_patt;
\r
76 private NyARMatchPattDeviationColorData _deviation_data;
\r
77 private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;
\r
78 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
79 private NyARCoord2Linear _coordline;
\r
81 public RleDetector(INyARColorPatt i_inst_patt,NyARCode[] i_ref_code,int i_num_of_code,NyARParam i_param) throws NyARException
\r
83 super(i_param.getScreenSize());
\r
84 final int cw = i_ref_code[0].getWidth();
\r
85 final int ch = i_ref_code[0].getHeight();
\r
86 //NyARMatchPatt_Color_WITHOUT_PCA[]の作成
\r
87 this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_num_of_code];
\r
88 this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]);
\r
89 for (int i = 1; i < i_num_of_code; i++){
\r
91 if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) {
\r
92 throw new NyARException();
\r
94 this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]);
\r
96 this._inst_patt=i_inst_patt;
\r
97 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());
\r
98 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);
\r
101 private NyARIntPoint2d[] __ref_vertex=new NyARIntPoint2d[4];
\r
103 * 矩形が見付かるたびに呼び出されます。
\r
104 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
\r
106 protected void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException
\r
108 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
110 NyARIntPoint2d[] vertex=this.__ref_vertex;
\r
111 vertex[0]=i_coord.items[i_vertex_index[0]];
\r
112 vertex[1]=i_coord.items[i_vertex_index[1]];
\r
113 vertex[2]=i_coord.items[i_vertex_index[2]];
\r
114 vertex[3]=i_coord.items[i_vertex_index[3]];
\r
117 if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){
\r
120 //取得パターンをカラー差分データに変換して評価する。
\r
121 this._deviation_data.setRaster(this._inst_patt);
\r
123 //最も一致するパターンを割り当てる。
\r
124 int square_index,direction;
\r
126 this._match_patt[0].evaluate(this._deviation_data,mr);
\r
128 direction=mr.direction;
\r
129 confidence=mr.confidence;
\r
131 for(int i=1;i<this._match_patt.length;i++){
\r
132 this._match_patt[i].evaluate(this._deviation_data,mr);
\r
133 if (confidence > mr.confidence) {
\r
136 // もっと一致するマーカーがあったぽい
\r
138 direction = mr.direction;
\r
139 confidence = mr.confidence;
\r
141 //最も一致したマーカ情報を、この矩形の情報として記録する。
\r
142 final NyARDetectMarkerResult result = this.result_stack.prePush();
\r
143 result.arcode_id = square_index;
\r
144 result.confidence = confidence;
\r
146 final NyARSquare sq=result.square;
\r
147 //directionを考慮して、squareを更新する。
\r
148 for(int i=0;i<4;i++){
\r
149 int idx=(i+4 - direction) % 4;
\r
150 this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coord,sq.line[i]);
\r
152 for (int i = 0; i < 4; i++) {
\r
154 if(!sq.line[i].crossPos(sq.line[(i + 3) % 4],sq.sqvertex[i])){
\r
155 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK
\r
159 public void init(INyARRgbRaster i_raster)
\r
161 this._ref_raster=i_raster;
\r
162 this.result_stack.clear();
\r
166 /** 変換行列計算器のインスタンス*/
\r
167 private INyARTransMat _transmat;
\r
168 private static final int AR_SQUARE_MAX = 300;
\r
169 private boolean _is_continue = false;
\r
170 private RleDetector _square_detect;
\r
171 private NyARRectOffset[] _offset;
\r
175 * パターンの重複しない複数のマーカを検出する検出器を作成します。
\r
177 * カメラパラメータを指定します。このサイズは、{@link #detectMarkerLite}に入力する画像と同じである必要があります。
\r
179 * 検出するマーカーパターンを格納した、{@link NyARCode}の配列を指定します。配列には、先頭から、0から始まるインデクス番号が割り当てられます。
\r
180 * このインデクス番号は、{@link #getConfidence(int)}等のインデクス番号に使います。
\r
181 * @param i_marker_width
\r
182 * 正方形マーカの物理サイズをmm単位で指定します。
\r
183 * @param i_number_of_code
\r
184 * i_codeの有効な個数を指定します。
\r
185 * @param i_input_raster_type
\r
186 * {@link #detectMarkerLite}に入力するラスタのバッファフォーマットを指定します。
\r
187 * この値は、{@link INyARRgbRaster#getBufferType}関数の戻り値を利用します。
\r
188 * @throws NyARException
\r
190 public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width, int i_number_of_code,int i_input_raster_type) throws NyARException
\r
192 initInstance(i_param,i_code,i_marker_width,i_number_of_code,i_input_raster_type);
\r
196 * この関数は、インスタンスを初期化します。
\r
198 * @see NyARDetectMarker#NyARDetectMarker(NyARParam, NyARCode[], double[], int, int)
\r
199 * @param i_ref_param
\r
201 * @param i_ref_code
\r
203 * @param i_marker_width
\r
205 * @param i_number_of_code
\r
207 * @param i_input_raster_type
\r
209 * @throws NyARException
\r
211 protected void initInstance(
\r
212 NyARParam i_ref_param,
\r
213 NyARCode[] i_ref_code,
\r
214 double[] i_marker_width,
\r
215 int i_number_of_code,
\r
216 int i_input_raster_type) throws NyARException
\r
219 final NyARIntSize scr_size=i_ref_param.getScreenSize();
\r
221 final int cw = i_ref_code[0].getWidth();
\r
222 final int ch = i_ref_code[0].getHeight();
\r
224 this._transmat = new NyARTransMat(i_ref_param);
\r
225 //NyARToolkitプロファイル
\r
226 this._square_detect =new RleDetector(new NyARColorPatt_Perspective_O2(cw, ch,4,25,i_input_raster_type),i_ref_code,i_number_of_code,i_ref_param);
\r
227 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);
\r
230 this._offset = NyARRectOffset.createArray(i_number_of_code);
\r
231 for(int i=0;i<i_number_of_code;i++){
\r
232 this._offset[i].setSquare(i_marker_width[i]);
\r
235 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
239 private NyARBinRaster _bin_raster;
\r
241 private INyARRasterFilter_Rgb2Bin _tobin_filter;
\r
244 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
247 * マーカーを検出するイメージを指定します。
\r
249 * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。
\r
250 * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。
\r
251 * @throws NyARException
\r
253 public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException
\r
256 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) {
\r
257 throw new NyARException();
\r
260 // ラスタを2値イメージに変換する.
\r
261 ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);
\r
262 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
265 this._square_detect.init(i_raster);
\r
266 this._square_detect.detectMarker(this._bin_raster);
\r
269 return this._square_detect.result_stack.getLength();
\r
273 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
276 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
278 * 結果値を受け取るオブジェクトを指定してください。
\r
279 * @throws NyARException
\r
281 public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException
\r
283 final NyARDetectMarkerResult result = this._square_detect.result_stack.getItem(i_index);
\r
284 // 一番一致したマーカーの位置とかその辺を計算
\r
285 if (_is_continue) {
\r
286 _transmat.transMatContinue(result.square, this._offset[result.arcode_id], o_result,o_result);
\r
288 _transmat.transMat(result.square, this._offset[result.arcode_id], o_result);
\r
294 * i_indexのマーカーの一致度を返します。
\r
297 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
298 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
299 * @throws NyARException
\r
301 public double getConfidence(int i_index)
\r
303 return this._square_detect.result_stack.getItem(i_index).confidence;
\r
306 * i_indexのマーカーのARCodeインデックスを返します。
\r
309 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
312 public int getARCodeIndex(int i_index)
\r
314 return this._square_detect.result_stack.getItem(i_index).arcode_id;
\r
318 * getTransmationMatrixの計算モードを設定します。
\r
320 * @param i_is_continue
\r
321 * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。
\r
323 public void setContinueMode(boolean i_is_continue)
\r
325 this._is_continue = i_is_continue;
\r
329 /** 内部クラスです。ユーザが使用することはありません*/
\r
330 class NyARDetectMarkerResult
\r
332 public int arcode_id;
\r
333 public double confidence;
\r
335 public NyARSquare square=new NyARSquare();
\r
339 /** 内部クラスです。ユーザが使用することはありません*/
\r
340 class NyARDetectMarkerResultStack extends NyARObjectStack<NyARDetectMarkerResult>
\r
342 public NyARDetectMarkerResultStack(int i_length) throws NyARException
\r
345 this.initInstance(i_length,NyARDetectMarkerResult.class);
\r
348 protected NyARDetectMarkerResult createElement()
\r
350 return new NyARDetectMarkerResult();
\r