OSDN Git Service

[リリース]NyARToolkit 1.1.1
[nyartoolkit-and/nyartoolkit-and.git] / 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=100;\r
44     private NyARMatchPatt_Color_WITHOUT_PCA match_patt;\r
45     private NyARDetectSquare square;\r
46     private final NyARSquareList square_list=new NyARSquareList(AR_SQUARE_MAX);\r
47     private NyARCode code;\r
48     protected NyARTransMat transmat;\r
49     private double marker_width;\r
50     //検出結果の保存用\r
51     private int detected_direction;\r
52     private double detected_confidence;\r
53     private NyARSquare detected_square;\r
54     private NyARColorPatt patt;\r
55     public NyARSingleDetectMarker(NyARParam i_param,NyARCode i_code,double i_marker_width) throws NyARException\r
56     {\r
57         //解析オブジェクトを作る\r
58         this.square=new NyARDetectSquare(i_param);\r
59         this.transmat=new NyARTransMat_O2(i_param);\r
60         //比較コードを保存\r
61         this.code=i_code;\r
62         this.marker_width=i_marker_width;\r
63         //評価パターンのホルダを作る\r
64         this.patt=new NyARColorPatt_O3(code.getWidth(),code.getHeight());\r
65         //評価器を作る。\r
66         this.match_patt=new NyARMatchPatt_Color_WITHOUT_PCA();  \r
67     }\r
68     /**\r
69      * i_imageにマーカー検出処理を実行して、結果を保持します。\r
70      * @param dataPtr\r
71      * @param thresh\r
72      * @return\r
73      * マーカーが検出できたかを真偽値で返します。\r
74      * @throws NyARException\r
75      */\r
76     public boolean detectMarkerLite(NyARRaster i_image,int i_thresh) throws NyARException\r
77     {\r
78         detected_square=null;\r
79         NyARSquareList l_square_list=this.square_list;\r
80         //スクエアコードを探す\r
81         square.detectSquare(i_image, i_thresh,l_square_list);\r
82         \r
83         int number_of_square=l_square_list.getSquareNum();\r
84         //コードは見つかった?\r
85         if(number_of_square<1){\r
86             return false;\r
87         }\r
88 \r
89         //コードの一致度を調べる準備\r
90 //      NyARSquare[] squares=square.getSquareArray();\r
91         //評価基準になるパターンをイメージから切り出す\r
92         patt.pickFromRaster(i_image,l_square_list.getSquare(0));\r
93         //パターンを評価器にセット\r
94         if(!this.match_patt.setPatt(patt)){\r
95             //計算に失敗した。\r
96             throw new NyARException();\r
97         }\r
98         //コードと比較する\r
99         match_patt.evaluate(code);\r
100         int square_index=0;\r
101         int direction=match_patt.getDirection();\r
102         double confidence=match_patt.getConfidence();\r
103         for(int i=1;i<number_of_square;i++){\r
104             //次のパターンを取得\r
105             patt.pickFromRaster(i_image,l_square_list.getSquare(i));\r
106             //評価器にセットする。\r
107             match_patt.setPatt(patt);\r
108             //コードと比較する\r
109             match_patt.evaluate(code);\r
110             double c2=match_patt.getConfidence();\r
111             if(confidence>c2){\r
112                 continue;\r
113             }\r
114             //もっと一致するマーカーがあったぽい\r
115             square_index=i;\r
116             direction=match_patt.getDirection();\r
117             confidence=c2;\r
118         }\r
119         //マーカー情報を保存\r
120         detected_square=l_square_list.getSquare(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