OSDN Git Service

[NyARToolKit for java]update document
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / detector / NyARSingleDetectMarker.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 edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\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
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 program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \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
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.detector;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.*;\r
35 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
36 import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt;\r
37 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
38 \r
39 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;\r
40 import jp.nyatla.nyartoolkit.core.squaredetect.*;\r
41 import jp.nyatla.nyartoolkit.core.pickup.*;\r
42 import jp.nyatla.nyartoolkit.core.types.*;\r
43 import jp.nyatla.nyartoolkit.core.transmat.*;\r
44 \r
45 /**\r
46  * このクラスは、1個のマーカを取り扱うマーカ検出器です。\r
47  * 登録した1個のARマーカに対応するマーカを入力画像から検出し、その変換行列と一致度を返します。\r
48  * <p>簡単な使い方\r
49  * <ol>\r
50  * <li>インスタンスを作成します。パラメータには、計算アルゴリズムと入力画像形式、カメラパラメータ、検出するマーカがあります。\r
51  * <li>{@link #detectMarkerLite}関数に画像と敷居値を入力して、マーカを検出します。\r
52  * <li>マーカが見つかると、インスタンスのプロパティが更新されます。{@link #getConfidence}等の関数を使って、取得したマーカの状態を得ます。\r
53  * <li>以降は、この処理を繰り返してマーカのパラメータを更新します。\r
54  * </ol>\r
55  * </p>\r
56  */\r
57 public class NyARSingleDetectMarker extends NyARCustomSingleDetectMarker\r
58 {\r
59         /** ARToolKit互換のアルゴリズムを選択します。*/\r
60         public final static int PF_ARTOOLKIT_COMPATIBLE=1;\r
61         /** NyARToolKitのアルゴリズムを選択します。*/\r
62         public final static int PF_NYARTOOLKIT=2;\r
63         /** ARToolKit互換アルゴリズムと、NyARToolKitのアルゴリズムの混合です。2D系にNyARToolkit,3D系にARToolKitのアルゴリズムを選択します。*/\r
64         public final static int PF_NYARTOOLKIT_ARTOOLKIT_FITTING=100;\r
65         /** 開発用定数値*/\r
66         public final static int PF_TEST2=201;\r
67         \r
68         /**\r
69          * RleLabelingを使った矩形検出機\r
70          */\r
71         private class RleDetector extends NyARSquareContourDetector_Rle\r
72         {\r
73                 NyARCustomSingleDetectMarker _parent;\r
74                 public RleDetector(NyARCustomSingleDetectMarker i_parent,NyARIntSize i_size) throws NyARException\r
75                 {\r
76                         super(i_size);\r
77                         this._parent=i_parent;\r
78                 }\r
79                 protected void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException\r
80                 {\r
81                         this._parent.updateSquareInfo(i_coord, i_vertex_index);\r
82                 }       \r
83         }\r
84         /**\r
85          * ARTKラべリングを使った矩形検出機へのブリッジ\r
86          */\r
87         class ARTKDetector extends NyARSquareContourDetector_ARToolKit\r
88         {\r
89                 NyARCustomSingleDetectMarker _parent;\r
90                 public ARTKDetector(NyARCustomSingleDetectMarker i_parent,NyARIntSize i_size) throws NyARException\r
91                 {\r
92                         super(i_size);\r
93                         this._parent=i_parent;\r
94                 }\r
95                 protected void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException\r
96                 {\r
97                         this._parent.updateSquareInfo(i_coord, i_vertex_index);\r
98                 }       \r
99         }       \r
100         \r
101         /**\r
102          * コンストラクタです。\r
103          * 指定した1種のマーカを1個検出するインスタンスを作ります。\r
104          * @param i_param\r
105          * カメラパラメータを指定します。このサイズは、{@link #detectMarker}に入力する画像と同じである必要があります。\r
106          * @param i_code\r
107          * 検出するマーカパターンを指定します。\r
108          * @param i_marker_width\r
109          * 正方形マーカの物理サイズをmm単位で指定します。\r
110          * @param i_input_raster_type\r
111          * {@link #detectMarker}に入力するラスタの画素形式を指定します。\r
112          * この値は、{@link INyARRgbRaster#getBufferType}関数の戻り値を利用します。\r
113          * @param i_profile_id\r
114          * 計算アルゴリズムの選択値です。以下の定数のいずれかを指定します。\r
115          * <ul>\r
116          * <li>{@link NyARSingleDetectMarker#PF_ARTOOLKIT_COMPATIBLE}\r
117          * <li>{@link NyARSingleDetectMarker#PF_NYARTOOLKIT}\r
118          * <li>{@link NyARSingleDetectMarker#PF_NYARTOOLKIT_ARTOOLKIT_FITTING}\r
119          * </ul>\r
120          * @throws NyARException\r
121          */\r
122         public NyARSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width,int i_input_raster_type,int i_profile_id) throws NyARException\r
123         {\r
124                 super();\r
125                 initialize(i_param,i_code,i_marker_width,i_input_raster_type,i_profile_id);\r
126                 return;\r
127         }\r
128         /**\r
129          * コンストラクタです。\r
130          * 指定した1種のマーカを1個検出するインスタンスを作ります。\r
131          * i_profile_idに{@link NyARSingleDetectMarker#PF_NYARTOOLKIT}を選択した{@link NyARSingleDetectMarker#NyARSingleDetectMarker(NyARParam, NyARCode, double, int, int)と同じです。\r
132          * @see NyARSingleDetectMarker#NyARSingleDetectMarker(NyARParam, NyARCode, double, int, int)\r
133          * @param i_param\r
134          * Check see also\r
135          * @param i_code\r
136          * Check see also\r
137          * @param i_marker_width\r
138          * Check see also\r
139          * @param i_input_raster_type\r
140          * Check see also\r
141          * @throws NyARException\r
142          */\r
143         public NyARSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width,int i_input_raster_type) throws NyARException\r
144         {\r
145                 super();\r
146                 initialize(i_param,i_code,i_marker_width,i_input_raster_type,PF_NYARTOOLKIT);\r
147                 return;\r
148         }\r
149         /**\r
150          * この関数は、インスタンスを初期化します。\r
151          * 引数は、{@link NyARSingleDetectMarker#NyARSingleDetectMarker}の対応する引数と同じです。\r
152          * @see NyARSingleDetectMarker#NyARSingleDetectMarker(NyARParam, NyARCode, double, int, int)\r
153          * @param i_ref_param\r
154          * Check see also\r
155          * @param i_ref_code\r
156          * Check see also\r
157          * @param i_marker_width\r
158          * Check see also\r
159          * @param i_input_raster_type\r
160          * Check see also\r
161          * @param i_profile_id\r
162          * Check see also\r
163          * @throws NyARException\r
164          */\r
165         private void initialize(\r
166                 NyARParam       i_ref_param,\r
167                 NyARCode        i_ref_code,\r
168                 double          i_marker_width,\r
169                 int i_input_raster_type,\r
170                 int i_profile_id) throws NyARException\r
171         {\r
172                 final NyARRasterFilter_ARToolkitThreshold th=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);\r
173                 INyARColorPatt patt_inst;\r
174                 NyARSquareContourDetector sqdetect_inst;\r
175                 INyARTransMat transmat_inst;\r
176 \r
177                 switch(i_profile_id){\r
178                 case PF_ARTOOLKIT_COMPATIBLE:\r
179                         patt_inst=new NyARColorPatt_O3(i_ref_code.getWidth(), i_ref_code.getHeight());\r
180                         sqdetect_inst=new ARTKDetector(this,i_ref_param.getScreenSize());\r
181                         transmat_inst=new NyARTransMat_ARToolKit(i_ref_param);\r
182                         break;\r
183                 case PF_NYARTOOLKIT_ARTOOLKIT_FITTING:\r
184                         patt_inst=new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25,i_input_raster_type);\r
185                         sqdetect_inst=new RleDetector(this,i_ref_param.getScreenSize());\r
186                         transmat_inst=new NyARTransMat_ARToolKit(i_ref_param);\r
187                         break;\r
188                 case PF_NYARTOOLKIT://default\r
189 //                      patt_inst=new NyARColorPatt_Perspective(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25);\r
190                         patt_inst=new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25,i_input_raster_type);\r
191                         sqdetect_inst=new RleDetector(this,i_ref_param.getScreenSize());\r
192                         transmat_inst=new NyARTransMat(i_ref_param);\r
193                         break;\r
194                 default:\r
195                         throw new NyARException();\r
196                 }\r
197                 super.initInstance(patt_inst,sqdetect_inst,transmat_inst,th,i_ref_param,i_ref_code,i_marker_width);\r
198                 \r
199         }\r
200         /**\r
201          * この関数は、画像からマーカを検出します。\r
202          * 関数は、画像の二値化、ラべリング、矩形検出、パターンの一致判定処理までを行い、画像中にある最も一致したパターンを持つ矩形の座標を、thisの\r
203          * プロパティへ保管します。\r
204          * @param i_raster\r
205          * 検出元の画像を指定します。この画像は、コンストラクタで指定したものと同じサイズ、画素形式に限られます。\r
206          * @param i_threshold\r
207          * 二値化の敷居値を指定します。0&lt;=n<256の間で指定します。\r
208          * @return\r
209          * マーカの検出に成功すると、trueを返します。\r
210          * @throws NyARException\r
211          */\r
212         public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException\r
213         {\r
214                 ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);\r
215                 return super.detectMarkerLite(i_raster);\r
216         }\r
217 }\r