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.squaredetect.INyARSquareDetector;
\r
44 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
45 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareDetector_Rle;
\r
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;
\r
47 import jp.nyatla.nyartoolkit.core.types.*;
\r
49 class NyARDetectMarkerResult
\r
51 public int arcode_id;
\r
53 public int direction;
\r
55 public double confidence;
\r
57 public NyARSquare ref_square;
\r
60 class NyARDetectMarkerResultHolder
\r
62 public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1];
\r
65 * result_holderを最大i_reserve_size個の要素を格納できるように予約します。
\r
67 * @param i_reserve_size
\r
69 public void reservHolder(int i_reserve_size)
\r
71 if (i_reserve_size >= result_array.length) {
\r
72 int new_size = i_reserve_size + 5;
\r
73 result_array = new NyARDetectMarkerResult[new_size];
\r
74 for (int i = 0; i < new_size; i++) {
\r
75 result_array[i] = new NyARDetectMarkerResult();
\r
82 * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。
\r
85 public class NyARDetectMarker
\r
87 private static final int AR_SQUARE_MAX = 300;
\r
89 private boolean _is_continue = false;
\r
91 private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;
\r
93 private INyARSquareDetector _square_detect;
\r
95 private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);
\r
97 protected INyARTransMat _transmat;
\r
99 private double[] _marker_width;
\r
102 private INyARColorPatt _patt;
\r
104 private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder();
\r
105 private NyARMatchPattDeviationColorData _deviation_data;
\r
107 * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。
\r
112 * 検出するマーカーのARCode配列を指定します。
\r
113 * 配列要素のインデックス番号が、そのままgetARCodeIndex関数で得られるARCodeインデックスになります。
\r
114 * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。
\r
115 * @param i_marker_width
\r
116 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
117 * @param i_number_of_code
\r
118 * i_codeに含まれる、ARCodeの数を指定します。
\r
119 * @param i_input_raster_type
\r
120 * 入力ラスタのピクセルタイプを指定します。この値は、INyARBufferReaderインタフェイスのgetBufferTypeの戻り値を指定します。
\r
121 * @throws NyARException
\r
123 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
125 initInstance(i_param,i_code,i_marker_width,i_number_of_code,i_input_raster_type);
\r
128 protected void initInstance(
\r
129 NyARParam i_ref_param,
\r
130 NyARCode[] i_ref_code,
\r
131 double[] i_marker_width,
\r
132 int i_number_of_code,
\r
133 int i_input_raster_type) throws NyARException
\r
136 final NyARIntSize scr_size=i_ref_param.getScreenSize();
\r
139 this._transmat = new NyARTransMat(i_ref_param);
\r
141 this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_number_of_code];
\r
142 final int cw = i_ref_code[0].getWidth();
\r
143 final int ch = i_ref_code[0].getHeight();
\r
144 this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]);
\r
145 for (int i = 1; i < i_number_of_code; i++){
\r
147 if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) {
\r
148 throw new NyARException();
\r
150 this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]);
\r
152 //NyARToolkitプロファイル
\r
153 this._patt =new NyARColorPatt_Perspective_O2(cw, ch,4,25);
\r
154 this._square_detect =new NyARSquareDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize());
\r
155 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);
\r
158 this._marker_width = i_marker_width;
\r
160 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);
\r
162 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
166 private NyARBinRaster _bin_raster;
\r
168 private INyARRasterFilter_RgbToBin _tobin_filter;
\r
169 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
172 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
175 * マーカーを検出するイメージを指定します。
\r
177 * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。
\r
178 * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。
\r
179 * @throws NyARException
\r
181 public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException
\r
184 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) {
\r
185 throw new NyARException();
\r
188 // ラスタを2値イメージに変換する.
\r
189 ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);
\r
190 this._tobin_filter.doFilter(i_raster, this._bin_raster);
\r
192 NyARSquareStack l_square_list = this._square_list;
\r
194 this._square_detect.detectMarker(this._bin_raster, l_square_list);
\r
196 final int number_of_square = l_square_list.getLength();
\r
198 if (number_of_square < 1) {
\r
203 this._result_holder.reservHolder(number_of_square);
\r
204 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
206 // 1スクエア毎に、一致するコードを決定していく
\r
207 for (int i = 0; i < number_of_square; i++) {
\r
208 NyARSquare square = (l_square_list.getItem(i));
\r
210 // 評価基準になるパターンをイメージから切り出す
\r
211 if (!this._patt.pickFromRaster(i_raster, square)) {
\r
212 // イメージの切り出しは失敗することもある。
\r
215 //取得パターンをカラー差分データに変換する。
\r
216 this._deviation_data.setRaster(this._patt);
\r
217 int square_index = 0;
\r
218 int direction = NyARSquare.DIRECTION_UNKNOWN;
\r
219 double confidence = 0;
\r
220 for(int i2=0;i2<this._match_patt.length;i2++){
\r
221 this._match_patt[i2].evaluate(this._deviation_data,mr);
\r
223 final double c2 = mr.confidence;
\r
224 if (confidence > c2) {
\r
227 // もっと一致するマーカーがあったぽい
\r
229 direction = mr.direction;
\r
232 // i番目のパターン情報を記録する。
\r
233 final NyARDetectMarkerResult result = this._result_holder.result_array[i];
\r
234 result.arcode_id = square_index;
\r
235 result.confidence = confidence;
\r
236 result.direction = direction;
\r
237 result.ref_square = square;
\r
239 return number_of_square;
\r
243 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
246 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
248 * 結果値を受け取るオブジェクトを指定してください。
\r
249 * @throws NyARException
\r
251 public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException
\r
253 final NyARDetectMarkerResult result = this._result_holder.result_array[i_index];
\r
254 // 一番一致したマーカーの位置とかその辺を計算
\r
255 if (_is_continue) {
\r
256 _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);
\r
258 _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);
\r
264 * i_indexのマーカーの一致度を返します。
\r
267 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
268 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
269 * @throws NyARException
\r
271 public double getConfidence(int i_index)
\r
273 return this._result_holder.result_array[i_index].confidence;
\r
277 * i_indexのマーカーの方位を返します。
\r
280 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
281 * @return 0,1,2,3の何れかを返します。
\r
283 public int getDirection(int i_index)
\r
285 return this._result_holder.result_array[i_index].direction;
\r
289 * i_indexのマーカーのARCodeインデックスを返します。
\r
292 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
295 public int getARCodeIndex(int i_index)
\r
297 return this._result_holder.result_array[i_index].arcode_id;
\r
301 * getTransmationMatrixの計算モードを設定します。
\r
303 * @param i_is_continue
\r
304 * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。
\r
306 public void setContinueMode(boolean i_is_continue)
\r
308 this._is_continue = i_is_continue;
\r