OSDN Git Service

26f3d24ffb319aaad2732575dc3ae4ba2edb64d7
[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 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.core.match;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.*;\r
36 \r
37 /**\r
38  * AR_TEMPLATE_MATCHING_BWと同等のルールで\r
39  * マーカーを評価します。\r
40  *\r
41  */\r
42 public class NyARMatchPatt_BlackWhite implements ARMatchPatt{\r
43     private double datapow;\r
44     private int width;\r
45     private int height;\r
46     private double cf=0;\r
47     private int dir=0;\r
48     private int ave;\r
49     private int[][][]   input=new int[height][width][3];\r
50     public boolean setPatt(NyARColorPatt i_target_patt) throws NyARException\r
51     {\r
52         width=i_target_patt.getWidth();\r
53         height=i_target_patt.getHeight();\r
54         short[][][] data=i_target_patt.getPatArray();   \r
55         input=new int[height][width][3];\r
56  \r
57         int sum = ave = 0;\r
58         for(int i=0;i<height;i++) {//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
59             for(int i2=0;i2<width;i2++) {//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
60                 ave += (255-data[i][i2][0])+(255-data[i][i2][1])+(255-data[i][i2][2]);\r
61             }\r
62         }\r
63         ave /= (height*width*3);\r
64 \r
65         for(int i=0;i<height;i++){//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
66             for(int i2=0;i2<width;i2++){//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
67                 input[i][i2][0] = ((255-data[i][i2][0]) + (255-data[i][i2][1]) + (255-data[i][i2][2]))/3 - ave;\r
68                 sum += input[i][i2][0]*input[i][i2][0];\r
69             }\r
70         }\r
71         \r
72         datapow = Math.sqrt( (double)sum );\r
73         if( datapow == 0.0 ){\r
74             return false;//            throw new NyARException();\r
75 //            dir.set(0);//*dir  = 0;\r
76 //            cf.set(-1.0);//*cf   = -1.0;\r
77 //            return -1;\r
78         }\r
79         return true;\r
80     }\r
81     public double getConfidence()\r
82     {\r
83         return cf;\r
84     }\r
85     public int getDirection()\r
86     {\r
87         return dir;\r
88     }\r
89     public void evaluate(NyARCode i_code)\r
90     {\r
91         short[][][]     patBW=i_code.getPatBW();//static int    patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
92         double[]        patpowBW=i_code.getPatPowBW();//static double patpowBW[AR_PATT_NUM_MAX][4];\r
93 \r
94         double max=0.0;\r
95         int res=-1;\r
96         //本家が飛ぶ。試験データで0.77767376888がが出ればOKってことで\r
97         for(int j = 0; j < 4; j++ ) {\r
98             int sum = 0;\r
99             for(int i=0;i<height;i++){\r
100                 for(int i2=0;i2<width;i2++){\r
101                 sum += input[i][i2][0]*patBW[j][i][i2];\r
102                 }\r
103             }\r
104             double sum2 = sum / patpowBW[j] / datapow;\r
105             if( sum2 > max ) {\r
106                 max = sum2;\r
107                 res = j;\r
108             }\r
109         }\r
110         dir=res;\r
111         cf=max;       \r
112     }\r
113 }\r