OSDN Git Service

[リリース]NyARToolkit 0.5
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / match / NyARMatchPatt_Color_WITHOUT_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.*;\r
36 \r
37 /**\r
38  * AR_TEMPLATE_MATCHING_COLORかつAR_MATCHING_WITHOUT_PCAと同等のルールで\r
39  * マーカーを評価します。\r
40  *\r
41  */\r
42 public class NyARMatchPatt_Color_WITHOUT_PCA implements ARMatchPatt{\r
43     private int[][][]   input;\r
44     private int ave;\r
45     private double datapow;\r
46 \r
47     private int width;\r
48     private int height;\r
49     private double cf=0;\r
50     private int dir=0;\r
51     public double getConfidence(){\r
52         return cf;\r
53     }\r
54     public int getDirection(){\r
55         return dir;\r
56     }\r
57     public void setPatt(NyARColorPatt i_target_patt) throws NyARException\r
58     {\r
59         width=i_target_patt.getWidth();\r
60         height=i_target_patt.getHeight();\r
61         short[][][] data=i_target_patt.getPatArray();\r
62         \r
63         input=new int[height][width][3];\r
64         int sum;\r
65 \r
66         sum = ave = 0;\r
67         for(int i=0;i<height;i++) {//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
68             for(int i2=0;i2<width;i2++) {//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
69                 ave += (255-data[i][i2][0])+(255-data[i][i2][1])+(255-data[i][i2][2]);\r
70             }\r
71         }\r
72         ave /= (height*width*3);\r
73 \r
74         for(int i=0;i<height;i++){//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
75             for(int i2=0;i2<width;i2++){//for(int i2=0;i2<Config.AR_PATT_SIZE_X;i2++){\r
76                 for(int i3=0;i3<3;i3++){\r
77                     input[i][i2][i3] = (255-data[i][i2][i3]) - ave;\r
78                     sum += input[i][i2][i3]*input[i][i2][i3];\r
79                 }\r
80             }\r
81         }\r
82         datapow = Math.sqrt( (double)sum );\r
83         if(datapow == 0.0){\r
84             throw new NyARException();\r
85 //            dir.set(0);//*dir  = 0;\r
86 //            cf.set(-1.0);//*cf   = -1.0;\r
87 //            return -1;\r
88         }       \r
89     }\r
90     /**\r
91      * public int pattern_match(short[][][] data,IntPointer dir,DoublePointer cf)\r
92 \r
93      */\r
94     public void evaluate(NyARCode i_code)\r
95     {\r
96         int[][][][] pat=i_code.getPat();\r
97         double[] patpow=i_code.getPatPow();\r
98         int res= -1;\r
99         double max=0.0;\r
100         for(int j = 0; j < 4; j++ ) {\r
101             int sum = 0;\r
102             for(int i=0;i<height;i++){//for(int i=0;i<Config.AR_PATT_SIZE_Y;i++){\r
103                 for(int i2=0;i2<width;i2++){\r
104                     for(int i3=0;i3<3;i3++){\r
105                         sum += input[i][i2][i3]*pat[j][i][i2][i3];//sum += input[i][i2][i3]*pat[k][j][i][i2][i3];\r
106                     }\r
107                 }\r
108             }\r
109             double sum2 = sum / patpow[j] / datapow;//sum2 = sum / patpow[k][j] / datapow;\r
110             if( sum2 > max ){\r
111                 max = sum2;\r
112                 res = j;\r
113             }\r
114         }\r
115         dir=res;\r
116         cf=max;\r
117     }\r
118 }