OSDN Git Service

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