OSDN Git Service

a53d19493d2c58bcd63044bc5ac3345ae7974518
[nyartoolkit-and/nyartoolkit-and.git] / 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\r
14  * modify it under the terms of the GNU Lesser General Public License\r
15  * as published by the Free Software Foundation; either version 3\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 Lesser General Public License for more details\r
22  * \r
23  * You should have received a copy of the GNU Lesser General Public\r
24  * License 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 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
36 \r
37 /**\r
38  * AR_TEMPLATE_MATCHING_BWと同等のルールで マーカを評価します。\r
39  * \r
40  */\r
41 public class NyARMatchPatt_BlackWhite implements INyARMatchPatt\r
42 {\r
43         protected NyARCode _code_patt;  \r
44         protected int _pixels;\r
45         \r
46         public NyARMatchPatt_BlackWhite(int i_width, int i_height)\r
47         {\r
48                 //最適化定数の計算\r
49                 this._pixels=i_height*i_width;\r
50                 return;\r
51         }\r
52         public NyARMatchPatt_BlackWhite(NyARCode i_code_ref)\r
53         {\r
54                 //最適化定数の計算\r
55                 this._pixels=i_code_ref.getWidth()*i_code_ref.getHeight();\r
56                 this._code_patt=i_code_ref;\r
57                 return;\r
58         }       \r
59         /**\r
60          * 比較対象のARCodeをセットします。\r
61          * @throws NyARException\r
62          */\r
63         public void setARCode(NyARCode i_code_ref)\r
64         {\r
65                 this._code_patt=i_code_ref;\r
66                 return;\r
67         }\r
68         /**\r
69          * 現在セットされているコードとパターンを比較して、結果値o_resultを更新します。\r
70          * 比較部分はFor文を16倍展開してあります。\r
71          */\r
72         public boolean evaluate(NyARMatchPattDeviationBlackWhiteData i_patt,NyARMatchPattResult o_result) throws NyARException\r
73         {\r
74                 assert this._code_patt!=null;\r
75 \r
76                 final int[] linput = i_patt.refData();\r
77                 int sum;\r
78                 double max = 0.0;\r
79                 int res = NyARSquare.DIRECTION_UNKNOWN;\r
80                 \r
81 \r
82                 for (int j = 0; j < 4; j++) {\r
83                         //合計値初期化\r
84                         sum=0;\r
85                         final NyARMatchPattDeviationBlackWhiteData code_patt=this._code_patt.getBlackWhiteData(j);\r
86                         final int[] pat_j = code_patt.refData();\r
87                         //<全画素について、比較(FORの1/16展開)/>\r
88                         int i;\r
89                         for(i=this._pixels-1;i>=0;i--){\r
90                                 sum += linput[i] * pat_j[i];\r
91                         }\r
92                         //0.7776737688877927がでればOK\r
93                         final double sum2 = sum / code_patt.getPow() / i_patt.getPow();// sum2 = sum / patpow[k][j]/ datapow;\r
94                         if (sum2 > max) {\r
95                                 max = sum2;\r
96                                 res = j;\r
97                         }\r
98                 }\r
99                 o_result.direction = res;\r
100                 o_result.confidence= max;\r
101                 return true;\r
102         }\r
103 }\r