OSDN Git Service

[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / detector / NyARDetectMarker.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.detector;\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.*;\r
40 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
41 import jp.nyatla.nyartoolkit.core.transmat.*;\r
42 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;\r
43 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;\r
44 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
45 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareDetector_Rle;\r
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;\r
47 import jp.nyatla.nyartoolkit.core.types.*;\r
48 \r
49 class NyARDetectMarkerResult\r
50 {\r
51         public int arcode_id;\r
52 \r
53         public int direction;\r
54 \r
55         public double confidence;\r
56 \r
57         public NyARSquare ref_square;\r
58 }\r
59 \r
60 class NyARDetectMarkerResultHolder\r
61 {\r
62         public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1];\r
63 \r
64         /**\r
65          * result_holderを最大i_reserve_size個の要素を格納できるように予約します。\r
66          * \r
67          * @param i_reserve_size\r
68          */\r
69         public void reservHolder(int i_reserve_size)\r
70         {\r
71                 if (i_reserve_size >= result_array.length) {\r
72                         int new_size = i_reserve_size + 5;\r
73                         result_array = new NyARDetectMarkerResult[new_size];\r
74                         for (int i = 0; i < new_size; i++) {\r
75                                 result_array[i] = new NyARDetectMarkerResult();\r
76                         }\r
77                 }\r
78         }\r
79 }\r
80 \r
81 /**\r
82  * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。\r
83  * \r
84  */\r
85 public class NyARDetectMarker\r
86 {\r
87         private static final int AR_SQUARE_MAX = 300;\r
88 \r
89         private boolean _is_continue = false;\r
90 \r
91         private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;\r
92 \r
93         private INyARSquareDetector _square_detect;\r
94 \r
95         private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);\r
96 \r
97         protected INyARTransMat _transmat;\r
98 \r
99         private double[] _marker_width; \r
100 \r
101         // 検出結果の保存用\r
102         private INyARColorPatt _patt;\r
103 \r
104         private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder();\r
105         private NyARMatchPattDeviationColorData _deviation_data;\r
106         /**\r
107          * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。\r
108          * \r
109          * @param i_param\r
110          * カメラパラメータを指定します。\r
111          * @param i_code\r
112          * 検出するマーカーのARCode配列を指定します。\r
113          * 配列要素のインデックス番号が、そのままgetARCodeIndex関数で得られるARCodeインデックスになります。 \r
114          * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。\r
115          * @param i_marker_width\r
116          * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。\r
117          * @param i_number_of_code\r
118          * i_codeに含まれる、ARCodeの数を指定します。\r
119          * @param i_input_raster_type\r
120          * 入力ラスタのピクセルタイプを指定します。この値は、INyARBufferReaderインタフェイスのgetBufferTypeの戻り値を指定します。\r
121          * @throws NyARException\r
122          */\r
123         public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width, int i_number_of_code,int i_input_raster_type) throws NyARException\r
124         {\r
125                 initInstance(i_param,i_code,i_marker_width,i_number_of_code,i_input_raster_type);\r
126                 return;\r
127         }\r
128         protected void initInstance(\r
129                 NyARParam       i_ref_param,\r
130                 NyARCode[]      i_ref_code,\r
131                 double[]        i_marker_width,\r
132                 int                     i_number_of_code,\r
133                 int i_input_raster_type) throws NyARException\r
134         {\r
135 \r
136                 final NyARIntSize scr_size=i_ref_param.getScreenSize();\r
137                 // 解析オブジェクトを作る\r
138 \r
139                 this._transmat = new NyARTransMat(i_ref_param);\r
140                 //各コード用の比較器を作る。\r
141                 this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_number_of_code];\r
142                 final int cw = i_ref_code[0].getWidth();\r
143                 final int ch = i_ref_code[0].getHeight();\r
144                 this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]);\r
145                 for (int i = 1; i < i_number_of_code; i++){\r
146                         //解像度チェック\r
147                         if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) {\r
148                                 throw new NyARException();\r
149                         }\r
150                         this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]);\r
151                 }\r
152                 //NyARToolkitプロファイル\r
153                 this._patt =new NyARColorPatt_Perspective_O2(cw, ch,4,25);\r
154                 this._square_detect =new NyARSquareDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize());\r
155                 this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);\r
156 \r
157                 //実サイズ保存\r
158                 this._marker_width = i_marker_width;\r
159                 //差分データインスタンスの作成\r
160                 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);\r
161                 //2値画像バッファを作る\r
162                 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);\r
163                 return;         \r
164         }\r
165         \r
166         private NyARBinRaster _bin_raster;\r
167 \r
168         private INyARRasterFilter_RgbToBin _tobin_filter;\r
169         private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();\r
170 \r
171         /**\r
172          * i_imageにマーカー検出処理を実行し、結果を記録します。\r
173          * \r
174          * @param i_raster\r
175          * マーカーを検出するイメージを指定します。\r
176          * @param i_thresh\r
177          * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。\r
178          * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。\r
179          * @throws NyARException\r
180          */\r
181         public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException\r
182         {\r
183                 // サイズチェック\r
184                 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) {\r
185                         throw new NyARException();\r
186                 }\r
187 \r
188                 // ラスタを2値イメージに変換する.\r
189                 ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);\r
190                 this._tobin_filter.doFilter(i_raster, this._bin_raster);\r
191 \r
192                 NyARSquareStack l_square_list = this._square_list;\r
193                 // スクエアコードを探す\r
194                 this._square_detect.detectMarker(this._bin_raster, l_square_list);\r
195 \r
196                 final int number_of_square = l_square_list.getLength();\r
197                 // コードは見つかった?\r
198                 if (number_of_square < 1) {\r
199                         // ないや。おしまい。\r
200                         return 0;\r
201                 }\r
202                 // 保持リストのサイズを調整\r
203                 this._result_holder.reservHolder(number_of_square);\r
204                 NyARMatchPattResult mr=this.__detectMarkerLite_mr;\r
205 \r
206                 // 1スクエア毎に、一致するコードを決定していく\r
207                 for (int i = 0; i < number_of_square; i++) {\r
208                         NyARSquare square = (l_square_list.getItem(i));\r
209 \r
210                         // 評価基準になるパターンをイメージから切り出す\r
211                         if (!this._patt.pickFromRaster(i_raster, square)) {\r
212                                 // イメージの切り出しは失敗することもある。\r
213                                 continue;\r
214                         }\r
215                         //取得パターンをカラー差分データに変換する。\r
216                         this._deviation_data.setRaster(this._patt);\r
217                         int square_index = 0;\r
218                         int direction = NyARSquare.DIRECTION_UNKNOWN;\r
219                         double confidence = 0;\r
220                         for(int i2=0;i2<this._match_patt.length;i2++){\r
221                                 this._match_patt[i2].evaluate(this._deviation_data,mr);\r
222 \r
223                                 final double c2 = mr.confidence;\r
224                                 if (confidence > c2) {\r
225                                         continue;\r
226                                 }\r
227                                 // もっと一致するマーカーがあったぽい\r
228                                 square_index = i2;\r
229                                 direction = mr.direction;\r
230                                 confidence = c2;\r
231                         }\r
232                         // i番目のパターン情報を記録する。\r
233                         final NyARDetectMarkerResult result = this._result_holder.result_array[i];\r
234                         result.arcode_id = square_index;\r
235                         result.confidence = confidence;\r
236                         result.direction = direction;\r
237                         result.ref_square = square;\r
238                 }\r
239                 return number_of_square;\r
240         }\r
241 \r
242         /**\r
243          * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。\r
244          * \r
245          * @param i_index\r
246          * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。\r
247          * @param o_result\r
248          * 結果値を受け取るオブジェクトを指定してください。\r
249          * @throws NyARException\r
250          */\r
251         public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException\r
252         {\r
253                 final NyARDetectMarkerResult result = this._result_holder.result_array[i_index];\r
254                 // 一番一致したマーカーの位置とかその辺を計算\r
255                 if (_is_continue) {\r
256                         _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);\r
257                 } else {\r
258                         _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);\r
259                 }\r
260                 return;\r
261         }\r
262 \r
263         /**\r
264          * i_indexのマーカーの一致度を返します。\r
265          * \r
266          * @param i_index\r
267          * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。\r
268          * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。\r
269          * @throws NyARException\r
270          */\r
271         public double getConfidence(int i_index)\r
272         {\r
273                 return this._result_holder.result_array[i_index].confidence;\r
274         }\r
275 \r
276         /**\r
277          * i_indexのマーカーの方位を返します。\r
278          * \r
279          * @param i_index\r
280          * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。\r
281          * @return 0,1,2,3の何れかを返します。\r
282          */\r
283         public int getDirection(int i_index)\r
284         {\r
285                 return this._result_holder.result_array[i_index].direction;\r
286         }\r
287 \r
288         /**\r
289          * i_indexのマーカーのARCodeインデックスを返します。\r
290          * \r
291          * @param i_index\r
292          * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。\r
293          * @return\r
294          */\r
295         public int getARCodeIndex(int i_index)\r
296         {\r
297                 return this._result_holder.result_array[i_index].arcode_id;\r
298         }\r
299 \r
300         /**\r
301          * getTransmationMatrixの計算モードを設定します。\r
302          * \r
303          * @param i_is_continue\r
304          * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。\r
305          */\r
306         public void setContinueMode(boolean i_is_continue)\r
307         {\r
308                 this._is_continue = i_is_continue;\r
309         }\r
310 \r
311 }\r