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.types.NyARLinear;
\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
57 * detectMarkerのコールバック関数
\r
59 private class DetectSquareCB implements NyARSquareContourDetector.IDetectMarkerCallback
\r
62 public double confidence;
\r
63 public NyARSquare square=new NyARSquare();
\r
66 private INyARRgbRaster _ref_raster;
\r
68 private INyARColorPatt _inst_patt;
\r
69 private NyARMatchPattDeviationColorData _deviation_data;
\r
70 private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;
\r
71 private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();
\r
72 private NyARCoord2Linear _coordline;
\r
74 public DetectSquareCB(INyARColorPatt i_inst_patt,NyARCode i_ref_code,NyARParam i_param)
\r
76 this._inst_patt=i_inst_patt;
\r
77 this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight());
\r
78 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());
\r
79 this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code);
\r
82 private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4);
\r
84 * 矩形が見付かるたびに呼び出されます。
\r
85 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
\r
87 public void onSquareDetect(NyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException
\r
89 NyARMatchPattResult mr=this.__detectMarkerLite_mr;
\r
91 NyARIntPoint2d[] vertex=this.__tmp_vertex;
\r
92 vertex[0].x=i_coordx[i_vertex_index[0]];
\r
93 vertex[0].y=i_coordy[i_vertex_index[0]];
\r
94 vertex[1].x=i_coordx[i_vertex_index[1]];
\r
95 vertex[1].y=i_coordy[i_vertex_index[1]];
\r
96 vertex[2].x=i_coordx[i_vertex_index[2]];
\r
97 vertex[2].y=i_coordy[i_vertex_index[2]];
\r
98 vertex[3].x=i_coordx[i_vertex_index[3]];
\r
99 vertex[3].y=i_coordy[i_vertex_index[3]];
\r
102 if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){
\r
105 //取得パターンをカラー差分データに変換して評価する。
\r
106 this._deviation_data.setRaster(this._inst_patt);
\r
107 if(!this._match_patt.evaluate(this._deviation_data,mr)){
\r
111 if (this.confidence > mr.confidence){
\r
114 //一致率の高い矩形があれば、方位を考慮して頂点情報を作成
\r
115 NyARSquare sq=this.square;
\r
116 this.confidence = mr.confidence;
\r
117 //directionを考慮して、squareを更新する。
\r
118 for(int i=0;i<4;i++){
\r
119 int idx=(i+4 - mr.direction) % 4;
\r
120 this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]);
\r
122 for (int i = 0; i < 4; i++) {
\r
124 if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){
\r
125 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK
\r
129 public final void init(INyARRgbRaster i_raster)
\r
132 this._ref_raster=i_raster;
\r
137 private boolean _is_continue = false;
\r
138 private NyARSquareContourDetector _square_detect;
\r
139 protected INyARTransMat _transmat;
\r
141 private NyARBinRaster _bin_raster;
\r
142 protected INyARRasterFilter_Rgb2Bin _tobin_filter;
\r
143 private DetectSquareCB _detect_cb;
\r
144 private NyARRectOffset _offset;
\r
147 protected NyARCustomSingleDetectMarker()
\r
151 protected void initInstance(
\r
152 INyARColorPatt i_patt_inst,
\r
153 NyARSquareContourDetector i_sqdetect_inst,
\r
154 INyARTransMat i_transmat_inst,
\r
155 INyARRasterFilter_Rgb2Bin i_filter,
\r
156 NyARParam i_ref_param,
\r
157 NyARCode i_ref_code,
\r
158 double i_marker_width) throws NyARException
\r
160 final NyARIntSize scr_size=i_ref_param.getScreenSize();
\r
162 this._square_detect = i_sqdetect_inst;
\r
163 this._transmat = i_transmat_inst;
\r
164 this._tobin_filter=i_filter;
\r
166 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);
\r
168 this._detect_cb=new DetectSquareCB(i_patt_inst,i_ref_code,i_ref_param);
\r
170 this._offset=new NyARRectOffset();
\r
171 this._offset.setSquare(i_marker_width);
\r
180 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
183 * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ
\r
185 * @return マーカーが検出できたかを真偽値で返します。
\r
186 * @throws NyARException
\r
188 public boolean detectMarkerLite(INyARRgbRaster i_raster) throws NyARException
\r
191 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){
\r
192 throw new NyARException();
\r
196 this._tobin_filter.doFilter(i_raster,this._bin_raster);
\r
199 this._detect_cb.init(i_raster);
\r
200 //矩形を探す(戻り値はコールバック関数で受け取る。)
\r
201 this._square_detect.detectMarkerCB(this._bin_raster,_detect_cb);
\r
202 if(this._detect_cb.confidence==0){
\r
208 * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。
\r
209 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
212 * 変換行列を受け取るオブジェクトを指定します。
\r
213 * @throws NyARException
\r
215 public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException
\r
217 // 一番一致したマーカーの位置とかその辺を計算
\r
218 if (this._is_continue) {
\r
219 this._transmat.transMatContinue(this._detect_cb.square,this._offset, o_result);
\r
221 this._transmat.transMat(this._detect_cb.square,this._offset, o_result);
\r
229 public NyARSquare refSquare()
\r
231 return this._detect_cb.square;
\r
234 * 検出したマーカーの一致度を返します。
\r
236 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。
\r
237 * @throws NyARException
\r
239 public double getConfidence()
\r
241 return this._detect_cb.confidence;
\r
244 * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。
\r
246 * @param i_is_continue
\r
247 * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。
\r
249 public void setContinueMode(boolean i_is_continue)
\r
251 this._is_continue = i_is_continue;
\r
257 public Object[] _getProbe()
\r
259 Object[] r=new Object[1];
\r
260 r[0]=this._detect_cb._inst_patt;
\r