OSDN Git Service

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