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
53 * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。
\r
56 public class NyARDetectMarker
\r
58 private class RleDetector extends NyARSquareContourDetector_Rle
\r
61 public NyARDetectMarkerResultStack result_stack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX);
\r
63 public INyARRgbRaster _ref_raster;
\r
65 private INyARColorPatt _inst_patt;
\r
66 private NyARMatchPattDeviationColorData _deviation_data;
\r
67 private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;
\r
68 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
69 private NyARCoord2Linear _coordline;
\r
71 public RleDetector(INyARColorPatt i_inst_patt,NyARCode[] i_ref_code,int i_num_of_code,NyARParam i_param) throws NyARException
\r
73 super(i_param.getScreenSize());
\r
74 final int cw = i_ref_code[0].getWidth();
\r
75 final int ch = i_ref_code[0].getHeight();
\r
76 //NyARMatchPatt_Color_WITHOUT_PCA[]の作成
\r
77 this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_num_of_code];
\r
78 this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]);
\r
79 for (int i = 1; i < i_num_of_code; i++){
\r
81 if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) {
\r
82 throw new NyARException();
\r
84 this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]);
\r
86 this._inst_patt=i_inst_patt;
\r
87 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());
\r
88 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);
\r
91 private NyARIntPoint2d[] __ref_vertex=new NyARIntPoint2d[4];
\r
93 * 矩形が見付かるたびに呼び出されます。
\r
94 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
\r
96 public void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException
\r
98 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
100 NyARIntPoint2d[] vertex=this.__ref_vertex;
\r
101 vertex[0]=i_coord.items[i_vertex_index[0]];
\r
102 vertex[1]=i_coord.items[i_vertex_index[1]];
\r
103 vertex[2]=i_coord.items[i_vertex_index[2]];
\r
104 vertex[3]=i_coord.items[i_vertex_index[3]];
\r
107 if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){
\r
110 //取得パターンをカラー差分データに変換して評価する。
\r
111 this._deviation_data.setRaster(this._inst_patt);
\r
113 //最も一致するパターンを割り当てる。
\r
114 int square_index,direction;
\r
116 this._match_patt[0].evaluate(this._deviation_data,mr);
\r
118 direction=mr.direction;
\r
119 confidence=mr.confidence;
\r
121 for(int i=1;i<this._match_patt.length;i++){
\r
122 this._match_patt[i].evaluate(this._deviation_data,mr);
\r
123 if (confidence > mr.confidence) {
\r
126 // もっと一致するマーカーがあったぽい
\r
128 direction = mr.direction;
\r
129 confidence = mr.confidence;
\r
131 //最も一致したマーカ情報を、この矩形の情報として記録する。
\r
132 final NyARDetectMarkerResult result = this.result_stack.prePush();
\r
133 result.arcode_id = square_index;
\r
134 result.confidence = confidence;
\r
136 final NyARSquare sq=result.square;
\r
137 //directionを考慮して、squareを更新する。
\r
138 for(int i=0;i<4;i++){
\r
139 int idx=(i+4 - direction) % 4;
\r
140 this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coord,sq.line[i]);
\r
142 for (int i = 0; i < 4; i++) {
\r
144 if(!sq.line[i].crossPos(sq.line[(i + 3) % 4],sq.sqvertex[i])){
\r
145 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK
\r
149 public void init(INyARRgbRaster i_raster)
\r
151 this._ref_raster=i_raster;
\r
152 this.result_stack.clear();
\r
156 private static final int AR_SQUARE_MAX = 300;
\r
157 private boolean _is_continue = false;
\r
158 private RleDetector _square_detect;
\r
159 protected INyARTransMat _transmat;
\r
160 private NyARRectOffset[] _offset;
\r
164 * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。
\r
169 * 検出するマーカーのARCode配列を指定します。
\r
170 * 配列要素のインデックス番号が、そのままgetARCodeIndex関数で得られるARCodeインデックスになります。
\r
171 * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。
\r
172 * @param i_marker_width
\r
173 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
174 * @param i_number_of_code
\r
175 * i_codeに含まれる、ARCodeの数を指定します。
\r
176 * @param i_input_raster_type
\r
177 * 入力ラスタのピクセルタイプを指定します。この値は、INyARBufferReaderインタフェイスのgetBufferTypeの戻り値を指定します。
\r
178 * @throws NyARException
\r
180 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
182 initInstance(i_param,i_code,i_marker_width,i_number_of_code,i_input_raster_type);
\r
185 protected void initInstance(
\r
186 NyARParam i_ref_param,
\r
187 NyARCode[] i_ref_code,
\r
188 double[] i_marker_width,
\r
189 int i_number_of_code,
\r
190 int i_input_raster_type) throws NyARException
\r
193 final NyARIntSize scr_size=i_ref_param.getScreenSize();
\r
195 final int cw = i_ref_code[0].getWidth();
\r
196 final int ch = i_ref_code[0].getHeight();
\r
198 this._transmat = new NyARTransMat(i_ref_param);
\r
199 //NyARToolkitプロファイル
\r
200 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
201 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);
\r
204 this._offset = NyARRectOffset.createArray(i_number_of_code);
\r
205 for(int i=0;i<i_number_of_code;i++){
\r
206 this._offset[i].setSquare(i_marker_width[i]);
\r
209 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
213 private NyARBinRaster _bin_raster;
\r
215 private INyARRasterFilter_Rgb2Bin _tobin_filter;
\r
218 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
221 * マーカーを検出するイメージを指定します。
\r
223 * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。
\r
224 * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。
\r
225 * @throws NyARException
\r
227 public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException
\r
230 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) {
\r
231 throw new NyARException();
\r
234 // ラスタを2値イメージに変換する.
\r
235 ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);
\r
236 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
239 this._square_detect.init(i_raster);
\r
240 this._square_detect.detectMarker(this._bin_raster);
\r
243 return this._square_detect.result_stack.getLength();
\r
247 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
250 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
252 * 結果値を受け取るオブジェクトを指定してください。
\r
253 * @throws NyARException
\r
255 public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException
\r
257 final NyARDetectMarkerResult result = this._square_detect.result_stack.getItem(i_index);
\r
258 // 一番一致したマーカーの位置とかその辺を計算
\r
259 if (_is_continue) {
\r
260 _transmat.transMatContinue(result.square, this._offset[result.arcode_id], o_result,o_result);
\r
262 _transmat.transMat(result.square, this._offset[result.arcode_id], o_result);
\r
268 * i_indexのマーカーの一致度を返します。
\r
271 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
272 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
273 * @throws NyARException
\r
275 public double getConfidence(int i_index)
\r
277 return this._square_detect.result_stack.getItem(i_index).confidence;
\r
280 * i_indexのマーカーのARCodeインデックスを返します。
\r
283 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
286 public int getARCodeIndex(int i_index)
\r
288 return this._square_detect.result_stack.getItem(i_index).arcode_id;
\r
292 * getTransmationMatrixの計算モードを設定します。
\r
294 * @param i_is_continue
\r
295 * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。
\r
297 public void setContinueMode(boolean i_is_continue)
\r
299 this._is_continue = i_is_continue;
\r
303 class NyARDetectMarkerResult
\r
305 public int arcode_id;
\r
306 public double confidence;
\r
308 public NyARSquare square=new NyARSquare();
\r
312 class NyARDetectMarkerResultStack extends NyARObjectStack<NyARDetectMarkerResult>
\r
314 public NyARDetectMarkerResultStack(int i_length) throws NyARException
\r
317 this.initInstance(i_length,NyARDetectMarkerResult.class);
\r
320 protected NyARDetectMarkerResult createElement()
\r
322 return new NyARDetectMarkerResult();
\r