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.sandbox.x2;
\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.NyARRasterFilter_ARToolkitThreshold;
\r
44 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;
\r
45 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;
\r
50 * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。
\r
53 public class NyARSingleDetectMarker_X2
\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
68 private int _detected_direction;
\r
70 private double _detected_confidence;
\r
72 private NyARSquare _detected_square;
\r
74 private INyARColorPatt _patt;
\r
77 * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。
\r
83 * @param i_marker_width
\r
84 * ARコードの物理サイズを、ミリメートルで指定します。
\r
85 * @throws NyARException
\r
87 public NyARSingleDetectMarker_X2(NyARParam i_param, NyARCode i_code, double i_marker_width,int i_raster_type) throws NyARException
\r
89 final NyARIntSize scr_size=i_param.getScreenSize();
\r
91 this._square_detect = new NyARSquareDetector_X2(i_param.getDistortionFactor(),scr_size);
\r
92 this._transmat = new NyARTransMat_X2(i_param);
\r
93 this._marker_width = i_marker_width;
\r
94 int cw=i_code.getWidth();
\r
95 int ch=i_code.getHeight();
\r
97 this._patt = new NyARColorPatt_O3(cw,ch);
\r
99 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_code);
\r
101 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
103 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);
\r
104 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_raster_type);
\r
108 private NyARBinRaster _bin_raster;
\r
109 private NyARRasterFilter_ARToolkitThreshold _tobin_filter;
\r
110 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
111 private NyARMatchPattDeviationColorData _deviation_data;
\r
115 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
118 * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ
\r
120 * @return マーカーが検出できたかを真偽値で返します。
\r
121 * @throws NyARException
\r
123 public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException
\r
126 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){
\r
127 throw new NyARException();
\r
130 //ラスタを(1/4の画像の)2値イメージに変換する.
\r
131 this._tobin_filter.setThreshold(i_threshold);
\r
132 this._tobin_filter.doFilter(i_raster,this._bin_raster);
\r
135 this._detected_square = null;
\r
136 NyARSquareStack l_square_list = this._square_list;
\r
138 this._square_detect.detectMarker(this._bin_raster, l_square_list);
\r
141 int number_of_square = l_square_list.getLength();
\r
143 if (number_of_square < 1) {
\r
146 boolean result=false;
\r
147 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
148 int square_index = 0;
\r
149 int direction = NyARSquare.DIRECTION_UNKNOWN;
\r
150 double confidence = 0;
\r
151 for(int i=0;i<number_of_square;i++){
\r
152 // 評価基準になるパターンをイメージから切り出す
\r
153 if (!this._patt.pickFromRaster(i_raster, l_square_list.getItem(i).imvertex)){
\r
156 //取得パターンをカラー差分データに変換して評価する。
\r
157 this._deviation_data.setRaster(this._patt);
\r
158 this._match_patt.evaluate(this._deviation_data,mr);
\r
160 final double c2 = mr.confidence;
\r
161 if (confidence > c2) {
\r
164 // もっと一致するマーカーがあったぽい
\r
166 direction = mr.direction;
\r
172 this._detected_square = (NyARSquare)l_square_list.getItem(square_index);
\r
173 this._detected_direction = direction;
\r
174 this._detected_confidence = confidence;
\r
179 * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。
\r
180 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
183 * 変換行列を受け取るオブジェクトを指定します。
\r
184 * @throws NyARException
\r
186 public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException
\r
188 // 一番一致したマーカーの位置とかその辺を計算
\r
189 if (this._is_continue) {
\r
190 this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
192 this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result);
\r
198 * 検出したマーカーの一致度を返します。
\r
200 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
201 * @throws NyARException
\r
203 public double getConfidence()
\r
205 return this._detected_confidence;
\r
209 * 検出したマーカーの方位を返します。
\r
211 * @return 0,1,2,3の何れかを返します。
\r
213 public int getDirection()
\r
215 return this._detected_direction;
\r
219 * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。
\r
221 * @param i_is_continue
\r
222 * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。
\r
224 public void setContinueMode(boolean i_is_continue)
\r
226 this._is_continue = i_is_continue;
\r