OSDN Git Service

[更新]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / processor / SingleNyIdMarkerProcesser.java
1 /* \r
2  * Capture Test NyARToolkitサンプルプログラム\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2009 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.processor;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.analyzer.raster.threshold.*;\r
29 import jp.nyatla.nyartoolkit.core.param.*;\r
30 import jp.nyatla.nyartoolkit.core.raster.*;\r
31 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
32 import jp.nyatla.nyartoolkit.core.transmat.*;\r
33 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;\r
34 import jp.nyatla.nyartoolkit.core.types.*;\r
35 import jp.nyatla.nyartoolkit.nyidmarker.*;\r
36 import jp.nyatla.nyartoolkit.nyidmarker.data.*;\r
37 import jp.nyatla.nyartoolkit.core.squaredetect.*;\r
38 \r
39 public abstract class SingleNyIdMarkerProcesser\r
40 {\r
41         /**\r
42          * オーナーが自由に使えるタグ変数です。\r
43          */\r
44         public Object tag;\r
45 \r
46         /**\r
47          * ロスト遅延の管理\r
48          */\r
49         private int _lost_delay_count = 0;\r
50         private int _lost_delay = 5;\r
51 \r
52         private NyARSquareDetector_Rle _square_detect;\r
53         protected INyARTransMat _transmat;\r
54         private double _marker_width=100;\r
55 \r
56         private NyARSquareStack _square_list = new NyARSquareStack(100);\r
57         private INyIdMarkerDataEncoder _encoder;\r
58         private boolean _is_active;\r
59         private INyIdMarkerData _data_temp;\r
60         private INyIdMarkerData _data_current;\r
61 \r
62         private int _current_threshold=110;\r
63         // [AR]検出結果の保存用\r
64         private NyARBinRaster _bin_raster;\r
65 \r
66         private NyARRasterFilter_ARToolkitThreshold _tobin_filter;\r
67 \r
68         private NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();\r
69 \r
70 \r
71         protected SingleNyIdMarkerProcesser()\r
72         {\r
73                 return;\r
74         }\r
75         private boolean _initialized=false;\r
76         protected void initInstance(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,int i_raster_format) throws NyARException\r
77         {\r
78                 //初期化済?\r
79                 assert this._initialized==false;\r
80                 \r
81                 NyARIntSize scr_size = i_param.getScreenSize();\r
82                 // 解析オブジェクトを作る\r
83                 this._square_detect = new NyARSquareDetector_Rle(i_param.getDistortionFactor(), scr_size);\r
84                 this._transmat = new NyARTransMat(i_param);\r
85                 this._encoder=i_encoder;\r
86 \r
87                 // 2値画像バッファを作る\r
88                 this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);\r
89                 //ワーク用のデータオブジェクトを2個作る\r
90                 this._is_active=false;\r
91                 this._data_temp=i_encoder.createDataInstance();\r
92                 this._data_current=i_encoder.createDataInstance();\r
93                 this._tobin_filter =new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);\r
94                 this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format,4);\r
95                 this._initialized=true;\r
96                 return;\r
97                 \r
98         }\r
99 \r
100         public void setMarkerWidth(int i_width)\r
101         {\r
102                 this._marker_width=i_width;\r
103                 return;\r
104         }\r
105 \r
106         public void reset(boolean i_is_force)\r
107         {\r
108                 if (this._data_current!=null && i_is_force == false) {\r
109                         // 強制書き換えでなければイベントコール\r
110                         this.onLeaveHandler();\r
111                 }\r
112                 // カレントマーカをリセット\r
113                 this._data_current = null;\r
114                 return;\r
115         }\r
116 \r
117         public void detectMarker(INyARRgbRaster i_raster) throws NyARException\r
118         {\r
119                 // サイズチェック\r
120                 if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize().w, i_raster.getSize().h)) {\r
121                         throw new NyARException();\r
122                 }\r
123                 // ラスタを2値イメージに変換する.\r
124                 this._tobin_filter.setThreshold(this._current_threshold);\r
125                 this._tobin_filter.doFilter(i_raster, this._bin_raster);\r
126 \r
127                 NyARSquareStack square_stack = this._square_list;\r
128                 // スクエアコードを探す\r
129                 this._square_detect.detectMarker(this._bin_raster, square_stack);\r
130                 // 認識処理\r
131                 if (!this._is_active) {\r
132                         // マーカ未認識→新規認識\r
133                         detectNewMarker(i_raster, square_stack);\r
134                 } else {\r
135                         // マーカ認識依頼→継続認識\r
136                         detectExistMarker(i_raster, square_stack);\r
137                 }\r
138                 return;\r
139         }\r
140 \r
141         \r
142         private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();\r
143         private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();\r
144         private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;\r
145         \r
146         /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。\r
147          */\r
148         private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException\r
149         {\r
150                 NyIdMarkerParam param=this._marker_param;\r
151                 NyIdMarkerPattern patt_data  =this._marker_data;\r
152                 int number_of_square = i_stack.getLength();\r
153                 NyARSquare current_square=null;\r
154                 INyIdMarkerData marker_id=null;\r
155                 for (int i = 0; i < number_of_square; i++) {\r
156                         // 評価基準になるパターンをイメージから切り出す\r
157                         current_square=i_stack.getItem(i);\r
158                         if (!this._id_pickup.pickFromRaster(i_raster,current_square, patt_data, param)) {\r
159                                 continue;\r
160                         }\r
161                         //エンコード\r
162                         if(!this._encoder.encode(patt_data,this._data_temp)){\r
163                                 continue;\r
164                         }\r
165                         //認識率が一番高いもの(占有面積が一番大きいもの)を選択する(省略)\r
166                         //id認識が成功したら終了\r
167                         marker_id=this._data_temp;\r
168                         break;\r
169                 }\r
170                 \r
171                 // 認識状態を更新\r
172                 final boolean is_id_found=updateStatus(current_square,marker_id, param);\r
173 \r
174                 //閾値フィードバック(detectExistMarkerにもあるよ)\r
175                 if(is_id_found){\r
176                         //マーカがあれば、マーカの周辺閾値を反映\r
177                         this._current_threshold=(this._current_threshold+param.threshold)/2;\r
178                 }else{\r
179                         //マーカがなければ、探索+DualPTailで基準輝度検索\r
180                         int th=this._threshold_detect.analyzeRaster(i_raster);\r
181                         this._current_threshold=(this._current_threshold+th)/2;\r
182                 }\r
183                 return;\r
184         }\r
185 \r
186         /**マーカの継続認識 現在認識中のマーカを優先して認識します。 \r
187          * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。\r
188          */\r
189         private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException\r
190         {\r
191                 NyIdMarkerParam param=this._marker_param;\r
192                 NyIdMarkerPattern patt_data  =this._marker_data;\r
193                 int number_of_square = i_stack.getLength();\r
194                 NyARSquare current_square=null;\r
195                 INyIdMarkerData marker_id=null;\r
196                 for (int i = 0; i < number_of_square; i++){\r
197                         //idマーカを認識\r
198                         current_square=i_stack.getItem(i);\r
199                         if (!this._id_pickup.pickFromRaster(i_raster, current_square, patt_data, param)) {\r
200                                 continue;\r
201                         }\r
202                         if(!this._encoder.encode(patt_data,this._data_temp)){\r
203                                 continue;\r
204                         }\r
205                         //現在認識中のidか確認\r
206                         if(!this._data_current.isEqual((this._data_temp))){\r
207                                 continue;\r
208                         }\r
209                         //現在認識中のものであれば、終了\r
210                         marker_id=this._data_temp;\r
211                         break;\r
212                 }\r
213                 // 認識状態を更新\r
214                 final boolean is_id_found=updateStatus(current_square,marker_id,param);\r
215 \r
216                 //閾値フィードバック(detectExistMarkerにもあるよ)\r
217                 if(is_id_found){\r
218                         //マーカがあれば、マーカの周辺閾値を反映\r
219                         this._current_threshold=(this._current_threshold+param.threshold)/2;\r
220                 }else{\r
221                         //マーカがなければ、探索+DualPTailで基準輝度検索\r
222                         int th=this._threshold_detect.analyzeRaster(i_raster);\r
223                         this._current_threshold=(this._current_threshold+th)/2;\r
224                 }\r
225                 return;\r
226         }\r
227 \r
228         private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();\r
229 \r
230         /**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。\r
231          */\r
232         private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data,NyIdMarkerParam i_param)  throws NyARException\r
233         {\r
234                 boolean is_id_found=false;\r
235                 NyARTransMatResult result = this.__NyARSquare_result;\r
236                 if (!this._is_active) {// 未認識中\r
237                         if (i_marker_data==null) {// 未認識から未認識の遷移\r
238                                 // なにもしないよーん。\r
239                                 this._is_active=false;\r
240                         } else {// 未認識から認識の遷移\r
241                                 this._data_current.copyFrom(i_marker_data);\r
242                                 // イベント生成\r
243                                 // OnEnter\r
244                                 this.onEnterHandler(this._data_current);\r
245                                 // 変換行列を作成\r
246                                 this._transmat.transMat(i_square,i_param.direction, this._marker_width, result);\r
247                                 // OnUpdate\r
248                                 this.onUpdateHandler(i_square, result);\r
249                                 this._lost_delay_count = 0;\r
250                                 this._is_active=true;\r
251                                 is_id_found=true;\r
252                         }\r
253                 } else {// 認識中\r
254                         if (i_marker_data==null) {\r
255                                 // 認識から未認識の遷移\r
256                                 this._lost_delay_count++;\r
257                                 if (this._lost_delay < this._lost_delay_count) {\r
258                                         // OnLeave\r
259                                         this.onLeaveHandler();\r
260                                         this._is_active=false;\r
261                                 }\r
262                         } else if(this._data_current.isEqual(i_marker_data)) {\r
263                                 //同じidの再認識\r
264                                 this._transmat.transMat(i_square, i_param.direction, this._marker_width, result);\r
265                                 // OnUpdate\r
266                                 this.onUpdateHandler(i_square, result);\r
267                                 this._lost_delay_count = 0;\r
268                                 is_id_found=true;\r
269                         } else {// 異なるコードの認識→今はサポートしない。\r
270                                 throw new  NyARException();\r
271                         }\r
272                 }\r
273                 return is_id_found;\r
274         }       \r
275         //通知ハンドラ\r
276         protected abstract void onEnterHandler(INyIdMarkerData i_code);\r
277         protected abstract void onLeaveHandler();\r
278         protected abstract void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result);\r
279 }\r