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 version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (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 framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.detector;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.*;
\r
36 import jp.nyatla.nyartoolkit.core.match.*;
\r
37 import jp.nyatla.nyartoolkit.core.param.NyARParam;
\r
38 import jp.nyatla.nyartoolkit.core.pickup.*;
\r
39 import jp.nyatla.nyartoolkit.core.raster.*;
\r
40 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
41 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
42 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
\r
43 import jp.nyatla.nyartoolkit.core.types.*;
\r
45 class NyARDetectMarkerResult
\r
47 public int arcode_id;
\r
49 public int direction;
\r
51 public double confidence;
\r
53 public NyARSquare ref_square;
\r
56 class NyARDetectMarkerResultHolder
\r
58 public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1];
\r
61 * result_holderを最大i_reserve_size個の要素を格納できるように予約します。
\r
63 * @param i_reserve_size
\r
65 public void reservHolder(int i_reserve_size)
\r
67 if (i_reserve_size >= result_array.length) {
\r
68 int new_size = i_reserve_size + 5;
\r
69 result_array = new NyARDetectMarkerResult[new_size];
\r
70 for (int i = 0; i < new_size; i++) {
\r
71 result_array[i] = new NyARDetectMarkerResult();
\r
78 * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。
\r
81 public class NyARDetectMarker
\r
83 private static final int AR_SQUARE_MAX = 300;
\r
85 private boolean _is_continue = false;
\r
87 private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;
\r
89 private INyARSquareDetector _square_detect;
\r
91 private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);
\r
93 private NyARCode[] _codes;
\r
95 protected INyARTransMat _transmat;
\r
97 private double[] _marker_width;
\r
99 private int _number_of_code;
\r
102 private INyARColorPatt _patt;
\r
104 private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder();
\r
107 * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。
\r
112 * 検出するマーカーのARCode配列を指定します。配列要素のインデックス番号が、そのままgetARCodeIndex関数で 得られるARCodeインデックスになります。 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。
\r
113 * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
114 * @param i_marker_width
\r
115 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
116 * @param i_number_of_code
\r
117 * i_codeに含まれる、ARCodeの数を指定します。
\r
118 * @throws NyARException
\r
120 public NyARDetectMarker(NyARParam i_param, NyARCode[] i_code, double[] i_marker_width, int i_number_of_code) throws NyARException
\r
122 final NyARIntSize scr_size=i_param.getScreenSize();
\r
124 this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(),scr_size);
\r
125 this._transmat = new NyARTransMat(i_param);
\r
127 this._codes = i_code;
\r
128 // 比較コードの解像度は全部同じかな?(違うとパターンを複数種つくらないといけないから)
\r
129 final int cw = i_code[0].getWidth();
\r
130 final int ch = i_code[0].getHeight();
\r
131 for (int i = 1; i < i_number_of_code; i++) {
\r
132 if (cw != i_code[i].getWidth() || ch != i_code[i].getHeight()) {
\r
134 throw new NyARException();
\r
138 this._patt = new NyARColorPatt_O3(cw, ch);
\r
139 this._number_of_code = i_number_of_code;
\r
141 this._marker_width = i_marker_width;
\r
143 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA();
\r
145 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
148 private NyARBinRaster _bin_raster;
\r
150 private NyARRasterFilter_ARToolkitThreshold _tobin_filter = new NyARRasterFilter_ARToolkitThreshold(100);
\r
153 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
156 * マーカーを検出するイメージを指定します。
\r
158 * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。
\r
159 * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。
\r
160 * @throws NyARException
\r
162 public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException
\r
165 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) {
\r
166 throw new NyARException();
\r
169 // ラスタを2値イメージに変換する.
\r
170 this._tobin_filter.setThreshold(i_threshold);
\r
171 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
173 NyARSquareStack l_square_list = this._square_list;
\r
175 this._square_detect.detectMarker(this._bin_raster, l_square_list);
\r
177 final int number_of_square = l_square_list.getLength();
\r
179 if (number_of_square < 1) {
\r
184 this._result_holder.reservHolder(number_of_square);
\r
186 // 1スクエア毎に、一致するコードを決定していく
\r
187 for (int i = 0; i < number_of_square; i++) {
\r
188 NyARSquare square = (NyARSquare)l_square_list.getItem(i);
\r
189 // 評価基準になるパターンをイメージから切り出す
\r
190 if (!this._patt.pickFromRaster(i_raster, square)) {
\r
191 // イメージの切り出しは失敗することもある。
\r
195 if (!this._match_patt.setPatt(this._patt)) {
\r
197 throw new NyARException();
\r
200 int code_index = 0;
\r
201 _match_patt.evaluate(_codes[0]);
\r
202 double confidence = _match_patt.getConfidence();
\r
203 int direction = _match_patt.getDirection();
\r
204 for (int i2 = 1; i2 < this._number_of_code; i2++) {
\r
206 _match_patt.evaluate(_codes[i2]);
\r
207 double c2 = _match_patt.getConfidence();
\r
208 if (confidence > c2) {
\r
211 // より一致するARCodeの情報を保存
\r
213 direction = _match_patt.getDirection();
\r
216 // i番目のパターン情報を保存する。
\r
217 final NyARDetectMarkerResult result = this._result_holder.result_array[i];
\r
218 result.arcode_id = code_index;
\r
219 result.confidence = confidence;
\r
220 result.direction = direction;
\r
221 result.ref_square = square;
\r
223 return number_of_square;
\r
227 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
230 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
232 * 結果値を受け取るオブジェクトを指定してください。
\r
233 * @throws NyARException
\r
235 public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException
\r
237 final NyARDetectMarkerResult result = this._result_holder.result_array[i_index];
\r
238 // 一番一致したマーカーの位置とかその辺を計算
\r
239 if (_is_continue) {
\r
240 _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);
\r
242 _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);
\r
248 * i_indexのマーカーの一致度を返します。
\r
251 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
252 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
253 * @throws NyARException
\r
255 public double getConfidence(int i_index)
\r
257 return this._result_holder.result_array[i_index].confidence;
\r
261 * i_indexのマーカーの方位を返します。
\r
264 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
265 * @return 0,1,2,3の何れかを返します。
\r
267 public int getDirection(int i_index)
\r
269 return this._result_holder.result_array[i_index].direction;
\r
273 * i_indexのマーカーのARCodeインデックスを返します。
\r
276 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
279 public int getARCodeIndex(int i_index)
\r
281 return this._result_holder.result_array[i_index].arcode_id;
\r
285 * getTransmationMatrixの計算モードを設定します。
\r
287 * @param i_is_continue
\r
288 * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。
\r
290 public void setContinueMode(boolean i_is_continue)
\r
292 this._is_continue = i_is_continue;
\r