OSDN Git Service

90344cd382f275395107702b4abced45d4295681
[nyartoolkit-and/nyartoolkit-and.git] / trunk / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / x2 / NyARSingleDetectMarker_X2.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.sandbox.x2;\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.rgb.*;\r
40 import jp.nyatla.nyartoolkit.core.raster.*;\r
41 import jp.nyatla.nyartoolkit.core.transmat.*;\r
42 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
43 import jp.nyatla.nyartoolkit.core.rasterfilter.gs2bin.*;\r
44 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilterBuilder_RgbToBin;\r
45 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;\r
46 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;\r
47 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
48 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;\r
49 \r
50 \r
51 /**\r
52  * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。\r
53  * \r
54  */\r
55 public class NyARSingleDetectMarker_X2\r
56 {\r
57         private static final int AR_SQUARE_MAX = 100;\r
58 \r
59         private boolean _is_continue = false;\r
60         private NyARMatchPatt_Color_WITHOUT_PCA _match_patt;\r
61         private INyARSquareDetector _square_detect;\r
62 \r
63         private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);\r
64 \r
65         protected INyARTransMat _transmat;\r
66 \r
67         private double _marker_width;\r
68 \r
69         // 検出結果の保存用\r
70         private int _detected_direction;\r
71 \r
72         private double _detected_confidence;\r
73 \r
74         private NyARSquare _detected_square;\r
75 \r
76         private INyARColorPatt _patt;\r
77 \r
78         /**\r
79          * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。\r
80          * \r
81          * @param i_param\r
82          * カメラパラメータを指定します。\r
83          * @param i_code\r
84          * 検出するARCodeを指定します。\r
85          * @param i_marker_width\r
86          * ARコードの物理サイズを、ミリメートルで指定します。\r
87          * @throws NyARException\r
88          */\r
89         public NyARSingleDetectMarker_X2(NyARParam i_param, NyARCode i_code, double i_marker_width,int i_raster_type) throws NyARException\r
90         {\r
91                 final NyARIntSize scr_size=i_param.getScreenSize();     \r
92                 // 解析オブジェクトを作る\r
93                 this._square_detect = new NyARSquareDetector_X2(i_param.getDistortionFactor(),scr_size);\r
94                 this._transmat = new NyARTransMat_X2(i_param);\r
95                 this._marker_width = i_marker_width;\r
96         int cw=i_code.getWidth();\r
97         int ch=i_code.getHeight();\r
98                 // 評価パターンのホルダを作る\r
99                 this._patt = new NyARColorPatt_O3(cw,ch);\r
100                 // 評価器を作る。\r
101                 this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_code);\r
102                 //2値画像バッファを作る\r
103                 this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);\r
104                 //差分データインスタンスの作成\r
105                 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);\r
106                 this._tobin_filter=new NyARRasterFilterBuilder_RgbToBin(100,i_raster_type);\r
107                 return;\r
108         }\r
109 \r
110         private NyARBinRaster _bin_raster;\r
111         private NyARRasterFilter_ARToolkitThreshold _tobin_filter;\r
112         private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();\r
113         private NyARMatchPattDeviationColorData _deviation_data;\r
114 \r
115         \r
116         /**\r
117          * i_imageにマーカー検出処理を実行し、結果を記録します。\r
118          * \r
119          * @param i_raster\r
120          * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ\r
121          * と一致していなければなりません。\r
122          * @return マーカーが検出できたかを真偽値で返します。\r
123          * @throws NyARException\r
124          */\r
125         public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException\r
126         {\r
127                 //サイズチェック\r
128                 if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){\r
129                         throw new NyARException();\r
130                 }\r
131 \r
132                 //ラスタを(1/4の画像の)2値イメージに変換する.\r
133                 this._tobin_filter.setThreshold(i_threshold);\r
134                 this._tobin_filter.doFilter(i_raster,this._bin_raster);\r
135                 \r
136                 \r
137                 this._detected_square = null;\r
138                 NyARSquareStack l_square_list = this._square_list;\r
139                 // スクエアコードを探す\r
140                 this._square_detect.detectMarker(this._bin_raster, l_square_list);\r
141 \r
142 \r
143                 int number_of_square = l_square_list.getLength();\r
144                 // コードは見つかった?\r
145                 if (number_of_square < 1) {\r
146                         return false;\r
147                 }\r
148                 boolean result=false;\r
149                 NyARMatchPattResult mr=this.__detectMarkerLite_mr;\r
150                 int square_index = 0;\r
151                 int direction = NyARSquare.DIRECTION_UNKNOWN;\r
152                 double confidence = 0;\r
153                 for(int i=0;i<number_of_square;i++){\r
154                         // 評価基準になるパターンをイメージから切り出す\r
155                         if (!this._patt.pickFromRaster(i_raster, l_square_list.getItem(i).imvertex)){\r
156                                 continue;\r
157                         }\r
158                         //取得パターンをカラー差分データに変換して評価する。\r
159                         this._deviation_data.setRaster(this._patt);\r
160                         this._match_patt.evaluate(this._deviation_data,mr);\r
161 \r
162                         final double c2 = mr.confidence;\r
163                         if (confidence > c2) {\r
164                                 continue;\r
165                         }\r
166                         // もっと一致するマーカーがあったぽい\r
167                         square_index = i;\r
168                         direction = mr.direction;\r
169                         confidence = c2;\r
170                         result=true;\r
171                 }\r
172                 \r
173                 // マーカー情報を保存\r
174                 this._detected_square = (NyARSquare)l_square_list.getItem(square_index);\r
175                 this._detected_direction = direction;\r
176                 this._detected_confidence = confidence;\r
177                 return result;\r
178         }\r
179 \r
180         /**\r
181          * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。\r
182          * 直前に実行したdetectMarkerLiteが成功していないと使えません。\r
183          * \r
184          * @param o_result\r
185          * 変換行列を受け取るオブジェクトを指定します。\r
186          * @throws NyARException\r
187          */\r
188         public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException\r
189         {\r
190                 // 一番一致したマーカーの位置とかその辺を計算\r
191                 if (this._is_continue) {\r
192                         this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result);\r
193                 } else {\r
194                         this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result);\r
195                 }\r
196                 return;\r
197         }\r
198 \r
199         /**\r
200          * 検出したマーカーの一致度を返します。\r
201          * \r
202          * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。\r
203          * @throws NyARException\r
204          */\r
205         public double getConfidence()\r
206         {\r
207                 return this._detected_confidence;\r
208         }\r
209 \r
210         /**\r
211          * 検出したマーカーの方位を返します。\r
212          * \r
213          * @return 0,1,2,3の何れかを返します。\r
214          */\r
215         public int getDirection()\r
216         {\r
217                 return this._detected_direction;\r
218         }\r
219 \r
220         /**\r
221          * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。\r
222          * \r
223          * @param i_is_continue\r
224          * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。\r
225          */\r
226         public void setContinueMode(boolean i_is_continue)\r
227         {\r
228                 this._is_continue = i_is_continue;\r
229         }\r
230 }\r