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.rgb.*;
\r
39 import jp.nyatla.nyartoolkit.core.raster.*;
\r
40 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
41 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;
\r
42 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
\r
43 import jp.nyatla.nyartoolkit.core.types.*;
\r
44 import jp.nyatla.nyartoolkit.core.squaredetect.*;
\r
47 * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。
\r
48 * 変換行列を求めるには、detectMarkerLite関数にラスタイメージを入力して、計算対象の矩形を特定します。
\r
49 * detectMarkerLiteが成功すると、getTransmationMatrix等の関数が使用可能な状態になり、変換行列を求めることができます。
\r
53 public class NyARCustomSingleDetectMarker
\r
55 private static final int AR_SQUARE_MAX = 100;
\r
57 private boolean _is_continue = false;
\r
58 private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;
\r
59 private INyARSquareDetector _square_detect;
\r
61 private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);
\r
63 protected INyARTransMat _transmat;
\r
65 private double _marker_width;
\r
67 private int _detected_direction;
\r
68 private double _detected_confidence;
\r
69 private NyARSquare _detected_square;
\r
70 private INyARColorPatt _patt;
\r
72 private NyARBinRaster _bin_raster;
\r
73 protected INyARRasterFilter_RgbToBin _tobin_filter;
\r
75 private NyARMatchPattDeviationColorData _deviation_data;
\r
77 protected NyARCustomSingleDetectMarker()
\r
81 protected void initInstance(
\r
82 INyARColorPatt i_patt_inst,
\r
83 INyARSquareDetector i_sqdetect_inst,
\r
84 INyARTransMat i_transmat_inst,
\r
85 INyARRasterFilter_RgbToBin i_filter,
\r
86 NyARParam i_ref_param,
\r
87 NyARCode i_ref_code,
\r
88 double i_marker_width) throws NyARException
\r
90 final NyARIntSize scr_size=i_ref_param.getScreenSize();
\r
92 this._square_detect = i_sqdetect_inst;
\r
93 this._transmat = i_transmat_inst;
\r
94 this._tobin_filter=i_filter;
\r
96 this._marker_width = i_marker_width;
\r
98 this._patt = i_patt_inst;
\r
100 this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight());
\r
102 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code);
\r
104 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
109 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
112 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
115 * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ
\r
117 * @return マーカーが検出できたかを真偽値で返します。
\r
118 * @throws NyARException
\r
120 public boolean detectMarkerLite(INyARRgbRaster i_raster) throws NyARException
\r
123 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){
\r
124 throw new NyARException();
\r
128 this._tobin_filter.doFilter(i_raster,this._bin_raster);
\r
131 this._detected_square = null;
\r
132 NyARSquareStack l_square_list = this._square_list;
\r
134 this._square_detect.detectMarker(this._bin_raster, l_square_list);
\r
137 int number_of_square = l_square_list.getLength();
\r
139 if (number_of_square < 1) {
\r
143 boolean result=false;
\r
144 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
145 int square_index = 0;
\r
146 int direction = NyARSquare.DIRECTION_UNKNOWN;
\r
147 double confidence = 0;
\r
148 for(int i=0;i<number_of_square;i++){
\r
149 // 評価基準になるパターンをイメージから切り出す
\r
150 if (!this._patt.pickFromRaster(i_raster, l_square_list.getItem(i).imvertex)){
\r
153 //取得パターンをカラー差分データに変換して評価する。
\r
154 this._deviation_data.setRaster(this._patt);
\r
155 if(!this._match_patt.evaluate(this._deviation_data,mr)){
\r
158 final double c2 = mr.confidence;
\r
159 if (confidence > c2) {
\r
162 // もっと一致するマーカーがあったぽい
\r
164 direction = mr.direction;
\r
170 this._detected_square = l_square_list.getItem(square_index);
\r
171 this._detected_direction = direction;
\r
172 this._detected_confidence = confidence;
\r
177 * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。
\r
178 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
181 * 変換行列を受け取るオブジェクトを指定します。
\r
182 * @throws NyARException
\r
184 public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException
\r
186 // 一番一致したマーカーの位置とかその辺を計算
\r
187 if (this._is_continue) {
\r
188 this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
190 this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
195 * 画面上のマーカ頂点情報を配列へ取得します。
\r
197 * 4要素以上の配列を指定して下さい。先頭の4要素に値がコピーされます。
\r
199 public void getSquarePosition(NyARIntPoint2d[] o_point)
\r
201 NyARIntPoint2d.copyArray(this._detected_square.imvertex,o_point);
\r
205 * 画面上のマーカ頂点情報を配列へのリファレンスを返します。
\r
206 * 返されたオブジェクトはクラスに所有し続けられています。クラスのメンバ関数を実行すると内容が書き変わります。
\r
207 * 外部でデータをストックする場合は、getSquarePositionで複製して下さい。
\r
210 public NyARIntPoint2d[] refSquarePosition()
\r
212 return this._detected_square.imvertex;
\r
217 * 検出したマーカーの一致度を返します。
\r
219 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
220 * @throws NyARException
\r
222 public double getConfidence()
\r
224 return this._detected_confidence;
\r
228 * 検出したマーカーの方位を返します。
\r
230 * @return 0,1,2,3の何れかを返します。
\r
232 public int getDirection()
\r
234 return this._detected_direction;
\r
238 * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。
\r
240 * @param i_is_continue
\r
241 * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。
\r
243 public void setContinueMode(boolean i_is_continue)
\r
245 this._is_continue = i_is_continue;
\r