OSDN Git Service

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