OSDN Git Service

[NyARToolKit for java]update document
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / core / match / NyARMatchPatt_BlackWhite.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.core.match;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.*;\r
35 \r
36 /**\r
37  * このクラスは、グレースケールで2パターンの一致度を計算します。\r
38  * 評価アルゴリズムは、ARToolKitの、AR_TEMPLATE_MATCHING_BWと同様です。\r
39  * 比較対象のデータには、{@link NyARMatchPattDeviationBlackWhiteData}クラスの物を使います。\r
40  */\r
41 public class NyARMatchPatt_BlackWhite implements INyARMatchPatt\r
42 {\r
43         /** 基準となるARマーカパターンを格納するオブジェクトへの参照値です。*/\r
44         protected NyARCode _ref_code_patt;\r
45         /** 基準パターンの*/\r
46         protected int _pixels;\r
47         /**\r
48          * コンストラクタ。\r
49          * 基準パターンの解像度を指定して、インスタンスを生成します。\r
50          * このコンストラクタで生成したインスタンスの基準パターンは、NULLになっています。\r
51          * 後で基準パターンを{@link #setARCode}関数で設定してください。\r
52          * @param i_width\r
53          * 基準パターンのサイズ\r
54          * @param i_height\r
55          * 基準パターンのサイズ\r
56          */\r
57         public NyARMatchPatt_BlackWhite(int i_width, int i_height)\r
58         {\r
59                 //最適化定数の計算\r
60                 this._pixels=i_height*i_width;\r
61                 return;\r
62         }\r
63         /**\r
64          * コンストラクタ。\r
65          * 基準パターンを元に、評価インスタンスを生成します。\r
66          * @param i_code_ref\r
67          * セットする基準パターン\r
68          */\r
69         public NyARMatchPatt_BlackWhite(NyARCode i_code_ref)\r
70         {\r
71                 //最適化定数の計算\r
72                 this._pixels=i_code_ref.getWidth()*i_code_ref.getHeight();\r
73                 //基準パターンをセット\r
74                 this._ref_code_patt=i_code_ref;\r
75                 return;\r
76         }       \r
77         /**\r
78          * 基準パターンをセットします。セットできる基準パターンは、コンストラクタに設定したサイズと同じものである必要があります。\r
79          * @param i_code_ref\r
80          * セットする基準パターンを格納したオブジェクト\r
81          * @throws NyARException\r
82          */\r
83         public void setARCode(NyARCode i_code_ref)\r
84         {\r
85                 this._ref_code_patt=i_code_ref;\r
86                 return;\r
87         }\r
88         /**\r
89          * この関数は、現在の基準パターンと検査パターンを比較して、類似度を計算します。\r
90          * @param i_patt\r
91          * 検査パターンを格納したオブジェクトです。このサイズは、基準パターンと一致している必要があります。\r
92          * @param o_result\r
93          * 結果を受け取るオブジェクトです。\r
94          * @return\r
95          * 検査に成功するとtrueを返します。\r
96          * @throws NyARException\r
97          */\r
98         public boolean evaluate(NyARMatchPattDeviationBlackWhiteData i_patt,NyARMatchPattResult o_result) throws NyARException\r
99         {\r
100                 assert this._ref_code_patt!=null;\r
101 \r
102                 final int[] linput = i_patt.refData();\r
103                 int sum;\r
104                 double max = 0.0;\r
105                 int res = NyARMatchPattResult.DIRECTION_UNKNOWN;\r
106                 \r
107 \r
108                 for (int j = 0; j < 4; j++) {\r
109                         //合計値初期化\r
110                         sum=0;\r
111                         final NyARMatchPattDeviationBlackWhiteData code_patt=this._ref_code_patt.getBlackWhiteData(j);\r
112                         final int[] pat_j = code_patt.refData();\r
113                         //<全画素について、比較(FORの1/16展開)/>\r
114                         int i;\r
115                         for(i=this._pixels-1;i>=0;i--){\r
116                                 sum += linput[i] * pat_j[i];\r
117                         }\r
118                         //0.7776737688877927がでればOK\r
119                         final double sum2 = sum / code_patt.getPow() / i_patt.getPow();// sum2 = sum / patpow[k][j]/ datapow;\r
120                         if (sum2 > max) {\r
121                                 max = sum2;\r
122                                 res = j;\r
123                         }\r
124                 }\r
125                 o_result.direction = res;\r
126                 o_result.confidence= max;\r
127                 return true;\r
128         }\r
129 }\r