OSDN Git Service

[リリース]NyARToolkit 1.0.0
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / match / NyARMatchPatt_Color_WITH_PCA.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.NyARCode;\r
36 import jp.nyatla.nyartoolkit.core.NyARColorPatt;\r
37 \r
38 /**\r
39  * AR_TEMPLATE_MATCHING_COLORかつAR_MATCHING_WITH_PCAと同等のルールで\r
40  * マーカーを評価します。\r
41  *\r
42  */\r
43 public class NyARMatchPatt_Color_WITH_PCA implements ARMatchPatt{\r
44     private final int           EVEC_MAX=10;//#define   EVEC_MAX     10\r
45     private int         evec_dim;//static int    evec_dim;\r
46     private int[][][]   input;\r
47     private double[][][][]      evec;//static double evec[EVEC_MAX][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
48     private double[][]          epat=new double[4][EVEC_MAX];//static double epat[AR_PATT_NUM_MAX][4][EVEC_MAX];\r
49     private int ave;\r
50     private double datapow;\r
51 \r
52     private int width;\r
53     private int height;\r
54     private double cf=0;\r
55     private int dir=0;//向きか!\r
56     public double getConfidence(){\r
57         return cf;\r
58     }\r
59     public int getDirection(){\r
60         return dir;\r
61     }\r
62     public boolean setPatt(NyARColorPatt i_target_patt) throws NyARException\r
63     {\r
64         width=i_target_patt.getWidth();\r
65         height=i_target_patt.getHeight();\r
66         int[][][] data=i_target_patt.getPatArray();\r
67         \r
68         input=new int[height][width][3];\r
69         evec=new double[EVEC_MAX][height][width][3];//static double evec[EVEC_MAX][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
70         int sum;\r
71 \r
72         sum = ave = 0;\r
73         for(int i=0;i<height;i++) {//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
74             for(int i2=0;i2<width;i2++) {//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
75                 ave += (255-data[i][i2][0])+(255-data[i][i2][1])+(255-data[i][i2][2]);\r
76             }\r
77         }\r
78         ave /= (height*width*3);\r
79 \r
80         for(int i=0;i<height;i++){//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
81             for(int i2=0;i2<width;i2++){//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
82                 for(int i3=0;i3<3;i3++){\r
83                     input[i][i2][i3] = (255-data[i][i2][i3]) - ave;\r
84                     sum += input[i][i2][i3]*input[i][i2][i3];\r
85                 }\r
86             }\r
87         }\r
88         datapow = Math.sqrt( (double)sum );\r
89         if(datapow == 0.0){\r
90             return false;//throw new NyARException();\r
91 //            dir.set(0);//*dir  = 0;\r
92 //            cf.set(-1.0);//*cf   = -1.0;\r
93 //            return -1;\r
94         }\r
95         return true;\r
96     }\r
97     /**\r
98      * public int pattern_match(short[][][] data,IntPointer dir,DoublePointer cf)\r
99 \r
100      */\r
101     public void evaluate(NyARCode i_code)\r
102     {\r
103         int[][][][] pat=i_code.getPat();\r
104         double[] patpow=i_code.getPatPow();\r
105         double[]        invec=new double[EVEC_MAX];\r
106 \r
107         double max = 0.0; // fix VC7 compiler warning: uninitialized variable\r
108        //確認\r
109         for(int i = 0; i < evec_dim; i++ ) {\r
110             invec[i] = 0.0;\r
111             for(int j = 0; j <height; j++ ){//for(int j = 0; j <Config.AR_PATT_SIZE_Y; j++ ){\r
112                 for(int j2 = 0; j2 <width; j2++ ){\r
113                     for(int j3 = 0; j3 <3; j3++ ){\r
114                         invec[i] += evec[i][j][j2][j3] * input[j][j2][j3];//invec[i] += evec[i][j] * input[j];\r
115                     }\r
116                 }\r
117             }\r
118             invec[i] /= datapow;\r
119         }\r
120 \r
121         double min = 10000.0;\r
122         int res=-1;\r
123         for(int j = 0; j < 4; j++ ) {\r
124             double sum2 = 0;\r
125             for(int i = 0; i < evec_dim; i++ ) {\r
126                 sum2 += (invec[i] - epat[j][i]) * (invec[i] - epat[j][i]);\r
127             }\r
128             if( sum2 < min ) {\r
129                 min = sum2;\r
130                 res = j;\r
131 //              res2 = k;//kは常にインスタンスを刺すから、省略可能\r
132             }\r
133         }\r
134 \r
135         int sum = 0;\r
136         for(int i=0;i<height;i++){//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
137             for(int i2=0;i2<width;i2++){//for(int i2=0;i<Config.AR_PATT_SIZE_X;i2++){\r
138                 for(int i3=0;i3<3;i3++){\r
139                     sum += input[i][i2][i3]*pat[res][i][i2][i3];//sum += input[i][i2][i3]*pat[res2][res][i][i2][i3];\r
140                 }\r
141             }\r
142         }\r
143         max = sum / patpow[res] / datapow;\r
144         dir=res;\r
145         cf=max;\r
146     }\r
147 }\r