OSDN Git Service

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