OSDN Git Service

[バックアップ]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / nymodel / x2 / NyARSingleDetectMarker_X2.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\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
17  * \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
22  * \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
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.nymodel.x2;\r
33 \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 \r
45 /**\r
46  * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。\r
47  * \r
48  */\r
49 public class NyARSingleDetectMarker_X2\r
50 {\r
51         private static final int AR_SQUARE_MAX = 100;\r
52 \r
53         private boolean _is_continue = false;\r
54         private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;\r
55         private INyARSquareDetector _square_detect;\r
56 \r
57         private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);\r
58 \r
59         private NyARCode _code;\r
60 \r
61         protected INyARTransMat _transmat;\r
62 \r
63         private double _marker_width;\r
64 \r
65         // 検出結果の保存用\r
66         private int _detected_direction;\r
67 \r
68         private double _detected_confidence;\r
69 \r
70         private NyARSquare _detected_square;\r
71 \r
72         private INyARColorPatt _patt;\r
73 \r
74         /**\r
75          * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。\r
76          * \r
77          * @param i_param\r
78          * カメラパラメータを指定します。\r
79          * @param i_code\r
80          * 検出するARCodeを指定します。\r
81          * @param i_marker_width\r
82          * ARコードの物理サイズを、ミリメートルで指定します。\r
83          * @throws NyARException\r
84          */\r
85         public NyARSingleDetectMarker_X2(NyARParam i_param, NyARCode i_code, double i_marker_width) throws NyARException\r
86         {\r
87                 final NyARIntSize scr_size=i_param.getScreenSize();             \r
88                 // 解析オブジェクトを作る\r
89                 this._square_detect = new NyARQrCodeDetector(i_param.getDistortionFactor(),scr_size);\r
90                 this._transmat = new NyARTransMat_X2(i_param);\r
91                 // 比較コードを保存\r
92                 this._code = i_code;\r
93                 this._marker_width = i_marker_width;\r
94                 // 評価パターンのホルダを作る\r
95                 this._patt = new NyARColorPatt_O3(_code.getWidth(), _code.getHeight());\r
96                 // 評価器を作る。\r
97                 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA();\r
98                 //2値画像バッファを作る\r
99                 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);\r
100         }\r
101 \r
102         private NyARBinRaster _bin_raster;\r
103         private NyARRasterFilter_ARToolkitThreshold _tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100);\r
104 \r
105         /**\r
106          * i_imageにマーカー検出処理を実行し、結果を記録します。\r
107          * \r
108          * @param i_raster\r
109          * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ\r
110          * と一致していなければなりません。\r
111          * @return マーカーが検出できたかを真偽値で返します。\r
112          * @throws NyARException\r
113          */\r
114         public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException\r
115         {\r
116                 //サイズチェック\r
117                 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){\r
118                         throw new NyARException();\r
119                 }\r
120 \r
121                 //ラスタを2値イメージに変換する.\r
122                 this._tobin_filter.setThreshold(i_threshold);\r
123                 this._tobin_filter.doFilter(i_raster,this._bin_raster);\r
124                 \r
125                 \r
126                 this._detected_square = null;\r
127                 NyARSquareStack l_square_list = this._square_list;\r
128                 // スクエアコードを探す\r
129                 this._square_detect.detectMarker(this._bin_raster, l_square_list);\r
130 \r
131 \r
132                 int number_of_square = l_square_list.getLength();\r
133                 // コードは見つかった?\r
134                 if (number_of_square < 1) {\r
135                         return false;\r
136                 }\r
137 \r
138                 // 評価基準になるパターンをイメージから切り出す\r
139                 if (!this._patt.pickFromRaster(i_raster, (NyARSquare)l_square_list.getItem(0))) {\r
140                         // パターンの切り出しに失敗\r
141                         return false;\r
142                 }\r
143                 // パターンを評価器にセット\r
144                 if (!this._match_patt.setPatt(this._patt)) {\r
145                         // 計算に失敗した。\r
146                         throw new NyARException();\r
147                 }\r
148                 // コードと比較する\r
149                 this._match_patt.evaluate(this._code);\r
150                 int square_index = 0;\r
151                 int direction = this._match_patt.getDirection();\r
152                 double confidence = this._match_patt.getConfidence();\r
153                 for (int i = 1; i < number_of_square; i++) {\r
154                         // 次のパターンを取得\r
155                         this._patt.pickFromRaster(i_raster, (NyARSquare)l_square_list.getItem(i));\r
156                         // 評価器にセットする。\r
157                         this._match_patt.setPatt(this._patt);\r
158                         // コードと比較する\r
159                         this._match_patt.evaluate(this._code);\r
160                         double c2 = this._match_patt.getConfidence();\r
161                         if (confidence > c2) {\r
162                                 continue;\r
163                         }\r
164                         // もっと一致するマーカーがあったぽい\r
165                         square_index = i;\r
166                         direction = this._match_patt.getDirection();\r
167                         confidence = c2;\r
168                 }\r
169                 // マーカー情報を保存\r
170                 this._detected_square = (NyARSquare)l_square_list.getItem(square_index);\r
171                 this._detected_direction = direction;\r
172                 this._detected_confidence = confidence;\r
173                 return true;\r
174         }\r
175 \r
176         /**\r
177          * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。\r
178          * 直前に実行したdetectMarkerLiteが成功していないと使えません。\r
179          * \r
180          * @param o_result\r
181          * 変換行列を受け取るオブジェクトを指定します。\r
182          * @throws NyARException\r
183          */\r
184         public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException\r
185         {\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
189                 } else {\r
190                         this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result);\r
191                 }\r
192                 return;\r
193         }\r
194 \r
195         /**\r
196          * 検出したマーカーの一致度を返します。\r
197          * \r
198          * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。\r
199          * @throws NyARException\r
200          */\r
201         public double getConfidence()\r
202         {\r
203                 return this._detected_confidence;\r
204         }\r
205 \r
206         /**\r
207          * 検出したマーカーの方位を返します。\r
208          * \r
209          * @return 0,1,2,3の何れかを返します。\r
210          */\r
211         public int getDirection()\r
212         {\r
213                 return this._detected_direction;\r
214         }\r
215 \r
216         /**\r
217          * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。\r
218          * \r
219          * @param i_is_continue\r
220          * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。\r
221          */\r
222         public void setContinueMode(boolean i_is_continue)\r
223         {\r
224                 this._is_continue = i_is_continue;\r
225         }\r
226 }\r