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.rgb.*;
\r
40 import jp.nyatla.nyartoolkit.core.raster.*;
\r
41 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
42 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;
\r
43 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
\r
44 import jp.nyatla.nyartoolkit.core.types.*;
\r
45 import jp.nyatla.nyartoolkit.core.squaredetect.*;
\r
48 * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。
\r
49 * 変換行列を求めるには、detectMarkerLite関数にラスタイメージを入力して、計算対象の矩形を特定します。
\r
50 * detectMarkerLiteが成功すると、getTransmationMatrix等の関数が使用可能な状態になり、変換行列を求めることができます。
\r
54 public class NyARCustomSingleDetectMarker
\r
56 private static final int AR_SQUARE_MAX = 100;
\r
58 private boolean _is_continue = false;
\r
59 private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;
\r
60 private INyARSquareDetector _square_detect;
\r
62 private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);
\r
64 protected INyARTransMat _transmat;
\r
66 private double _marker_width;
\r
68 private int _detected_direction;
\r
69 private double _detected_confidence;
\r
70 private NyARSquare _detected_square;
\r
71 private INyARColorPatt _patt;
\r
73 private NyARBinRaster _bin_raster;
\r
74 protected INyARRasterFilter_RgbToBin _tobin_filter;
\r
76 private NyARMatchPattDeviationColorData _deviation_data;
\r
78 * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。
\r
84 * @param i_marker_width
\r
85 * ARコードの物理サイズを、ミリメートルで指定します。
\r
87 * RGB→BIN変換フィルタを指定します。
\r
88 * @throws NyARException
\r
90 public NyARCustomSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width,INyARRasterFilter_RgbToBin i_filter) throws NyARException
\r
92 final NyARIntSize scr_size=i_param.getScreenSize();
\r
94 // this._square_detect = new NyARSquareDetector_ARToolKit(i_param.getDistortionFactor(),scr_size);
\r
95 this._square_detect = new NyARSquareDetector_Rle(i_param.getDistortionFactor(),scr_size);
\r
96 this._transmat = new NyARTransMat(i_param);
\r
98 this._marker_width = i_marker_width;
\r
100 // this._patt = new NyARColorPatt_O1(i_code.getWidth(), i_code.getHeight());
\r
101 this._patt = new NyARColorPatt_O3(i_code.getWidth(), i_code.getHeight());
\r
102 // this._patt = new NyARColorPatt_Perspective(i_code.getWidth(), i_code.getHeight(),25);
\r
104 this._deviation_data=new NyARMatchPattDeviationColorData(i_code.getWidth(),i_code.getHeight());
\r
106 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_code);
\r
109 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
110 this._tobin_filter=i_filter;
\r
114 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
117 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
120 * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ
\r
122 * @return マーカーが検出できたかを真偽値で返します。
\r
123 * @throws NyARException
\r
125 public boolean detectMarkerLite(INyARRgbRaster i_raster) throws NyARException
\r
128 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){
\r
129 throw new NyARException();
\r
133 this._tobin_filter.doFilter(i_raster,this._bin_raster);
\r
136 this._detected_square = null;
\r
137 NyARSquareStack l_square_list = this._square_list;
\r
139 this._square_detect.detectMarker(this._bin_raster, l_square_list);
\r
142 int number_of_square = l_square_list.getLength();
\r
144 if (number_of_square < 1) {
\r
148 boolean result=false;
\r
149 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
150 int square_index = 0;
\r
151 int direction = NyARSquare.DIRECTION_UNKNOWN;
\r
152 double confidence = 0;
\r
153 for(int i=0;i<number_of_square;i++){
\r
154 // 評価基準になるパターンをイメージから切り出す
\r
155 if (!this._patt.pickFromRaster(i_raster, l_square_list.getItem(i))){
\r
158 //取得パターンをカラー差分データに変換して評価する。
\r
159 this._deviation_data.setRaster(this._patt);
\r
160 if(!this._match_patt.evaluate(this._deviation_data,mr)){
\r
163 final double c2 = mr.confidence;
\r
164 if (confidence > c2) {
\r
167 // もっと一致するマーカーがあったぽい
\r
169 direction = mr.direction;
\r
175 this._detected_square = l_square_list.getItem(square_index);
\r
176 this._detected_direction = direction;
\r
177 this._detected_confidence = confidence;
\r
182 * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。
\r
183 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
186 * 変換行列を受け取るオブジェクトを指定します。
\r
187 * @throws NyARException
\r
189 public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException
\r
191 // 一番一致したマーカーの位置とかその辺を計算
\r
192 if (this._is_continue) {
\r
193 this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
195 this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
200 * 画面上のマーカ頂点情報を配列へ取得します。
\r
202 * 4要素以上の配列を指定して下さい。先頭の4要素に値がコピーされます。
\r
204 public void getSquarePosition(NyARIntPoint2d[] o_point)
\r
206 NyARIntPoint2d.copyArray(this._detected_square.imvertex,o_point);
\r
210 * 画面上のマーカ頂点情報を配列へのリファレンスを返します。
\r
211 * 返されたオブジェクトはクラスに所有し続けられています。クラスのメンバ関数を実行すると内容が書き変わります。
\r
212 * 外部でデータをストックする場合は、getSquarePositionで複製して下さい。
\r
215 public NyARIntPoint2d[] refSquarePosition()
\r
217 return this._detected_square.imvertex;
\r
222 * 検出したマーカーの一致度を返します。
\r
224 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
225 * @throws NyARException
\r
227 public double getConfidence()
\r
229 return this._detected_confidence;
\r
233 * 検出したマーカーの方位を返します。
\r
235 * @return 0,1,2,3の何れかを返します。
\r
237 public int getDirection()
\r
239 return this._detected_direction;
\r
243 * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。
\r
245 * @param i_is_continue
\r
246 * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。
\r
248 public void setContinueMode(boolean i_is_continue)
\r
250 this._is_continue = i_is_continue;
\r