OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 0.8 / src / jp / nyatla / nyartoolkit / detector / NyARSingleDetectMarker.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.detector;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.*;\r
36 import jp.nyatla.nyartoolkit.core.match.NyARMatchPatt_Color_WITHOUT_PCA;\r
37 import jp.nyatla.nyartoolkit.core.raster.*;\r
38 /**\r
39  * 1個のマーカーに対する変換行列を計算するクラスです。\r
40  *\r
41  */\r
42 public class NyARSingleDetectMarker{\r
43     private static final int AR_SQUARE_MAX=10;\r
44     private NyARParam param;\r
45     private NyARDetectSquare square;\r
46     private NyARCode code;\r
47     protected NyARTransMat transmat;\r
48     private double marker_width;\r
49     //検出結果の保存用\r
50     private int detected_direction;\r
51     private double detected_confidence;\r
52     private NyARSquare detected_square;\r
53     private NyARColorPatt patt;\r
54     public NyARSingleDetectMarker(NyARParam i_param,NyARCode i_code,double i_marker_width)\r
55     {\r
56         param=i_param;\r
57         //解析オブジェクトを作る\r
58         square=new NyARDetectSquare(AR_SQUARE_MAX,i_param);\r
59         transmat=new NyARTransMat(param);\r
60         //比較コードを保存\r
61         code=i_code;\r
62         marker_width=i_marker_width;\r
63         //評価パターンのホルダを作る\r
64         patt=new NyARColorPatt(code.getWidth(),code.getHeight());\r
65         \r
66     }\r
67     /**\r
68      * i_imageにマーカー検出処理を実行して、結果を保持します。\r
69      * @param dataPtr\r
70      * @param thresh\r
71      * @return\r
72      * マーカーが検出できたかを真偽値で返します。\r
73      * @throws NyARException\r
74      */\r
75     public boolean detectMarkerLite(NyARRaster i_image,int i_thresh) throws NyARException\r
76     {\r
77         detected_square=null;\r
78         //スクエアコードを探す\r
79         square.detectSquare(i_image, i_thresh);\r
80         int number_of_square=square.getSquareCount();\r
81         //コードは見つかった?\r
82         if(number_of_square<1){\r
83             return false;\r
84         }\r
85 \r
86         //コードの一致度を調べる準備\r
87         NyARSquare[] squares=square.getSquareArray();\r
88         //評価基準になるパターンをイメージから切り出す\r
89         patt.pickFromRaster(i_image,squares[0].getMarker());\r
90         \r
91         //パターンの評価オブジェクトを作る。\r
92         NyARMatchPatt_Color_WITHOUT_PCA eva=new NyARMatchPatt_Color_WITHOUT_PCA();\r
93         //パターンを評価器にセット\r
94         if(!eva.setPatt(patt)){\r
95             //計算に失敗した。\r
96             return false;\r
97         }\r
98         //コードと比較する\r
99         eva.evaluate(code);\r
100         int square_index=0;\r
101         int direction=eva.getDirection();\r
102         double confidence=eva.getConfidence();\r
103         for(int i=1;i<number_of_square;i++){\r
104             //次のパターンを取得\r
105             patt.pickFromRaster(i_image,squares[i].getMarker());\r
106             //評価器にセットする。\r
107             eva.setPatt(patt);\r
108             //コードと比較する\r
109             eva.evaluate(code);\r
110             double c2=eva.getConfidence();\r
111             if(confidence>c2){\r
112                 continue;\r
113             }\r
114             //もっと一致するマーカーがあったぽい\r
115             square_index=i;\r
116             direction=eva.getDirection();\r
117             confidence=c2;\r
118         }\r
119         //マーカー情報を保存\r
120         detected_square=squares[square_index];\r
121         detected_direction=direction;\r
122         detected_confidence=confidence;\r
123         return true;\r
124     }\r
125     /**\r
126      * 変換行列を返します。直前に実行したdetectMarkerLiteが成功していないと使えません。\r
127      * @param i_marker_width\r
128      * マーカーの大きさを指定します。\r
129      * @return\r
130      * double[3][4]の変換行列を返します。\r
131      * @throws NyARException\r
132      */\r
133     public NyARMat getTransmationMatrix() throws NyARException\r
134     {\r
135         //一番一致したマーカーの位置とかその辺を計算\r
136         transmat.transMat(detected_square,detected_direction,marker_width);\r
137         return transmat.getTransformationMatrix();\r
138     }\r
139     public double getConfidence()\r
140     {\r
141         return detected_confidence;\r
142     }\r
143     public int getDirection()\r
144     {\r
145         return detected_direction;\r
146     }\r
147     \r
148 \r
149         \r
150         \r
151         \r
152         \r
153         \r
154 //      public static class arUtil_c{\r
155 //              public static final int         arFittingMode   =Config.DEFAULT_FITTING_MODE;\r
156 //              private static final int                arImageProcMode =Config.DEFAULT_IMAGE_PROC_MODE;\r
157 //              public static final int         arTemplateMatchingMode  =Config.DEFAULT_TEMPLATE_MATCHING_MODE;\r
158 //              public static final int         arMatchingPCAMode       =Config.DEFAULT_MATCHING_PCA_MODE;      \r
159                 /*int arInitCparam( ARParam *param )*/\r
160 \r
161 \r
162         \r
163 }\r
164 \r
165         \r
166 \r
167         \r
168         \r
169         \r
170         \r
171         \r
172         \r
173 \r
174         \r
175 \r
176 \r
177         \r
178 \r