2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\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
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
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
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.detector;
\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.pickup.INyColorPatt;
\r
38 import jp.nyatla.nyartoolkit.core.pickup.NyARColorPatt_O3;
\r
39 import jp.nyatla.nyartoolkit.core.raster.*;
\r
40 import jp.nyatla.nyartoolkit.core.transmat.INyARTransMat;
\r
41 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;
\r
42 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMat_O2;
\r
46 class NyARDetectMarkerResult
\r
48 public int arcode_id;
\r
49 public int direction;
\r
50 public double confidence;
\r
51 public NyARSquare ref_square;
\r
53 class NyARDetectMarkerResultHolder
\r
55 public NyARDetectMarkerResult[] result_array=new NyARDetectMarkerResult[1];
\r
57 * result_holderを最大i_reserve_size個の要素を格納できるように予約します。
\r
58 * @param i_reserve_size
\r
60 public void reservHolder(int i_reserve_size)
\r
62 if(i_reserve_size>=result_array.length){
\r
63 int new_size=i_reserve_size+5;
\r
64 result_array=new NyARDetectMarkerResult[new_size];
\r
65 for(int i=0;i<new_size;i++){
\r
66 result_array[i]=new NyARDetectMarkerResult();
\r
72 * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから
\r
73 * 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。
\r
76 public class NyARDetectMarker{
\r
77 private static final int AR_SQUARE_MAX=300;
\r
78 private boolean is_continue=false;
\r
79 private NyARMatchPatt_Color_WITHOUT_PCA match_patt;
\r
80 private NyARDetectSquare square;
\r
81 private final NyARSquareList square_list=new NyARSquareList(AR_SQUARE_MAX);
\r
82 private NyARCode[] codes;
\r
83 protected INyARTransMat transmat;
\r
84 private double[] marker_width;
\r
85 private int number_of_code;
\r
87 private INyColorPatt patt;
\r
89 private NyARDetectMarkerResultHolder result_holder=new NyARDetectMarkerResultHolder();
\r
92 * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。
\r
96 * 検出するマーカーのARCode配列を指定します。配列要素のインデックス番号が、そのままgetARCodeIndex関数で
\r
97 * 得られるARCodeインデックスになります。
\r
98 * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。
\r
99 * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
100 * @param i_marker_width
\r
101 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。
\r
102 * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
103 * @param i_number_of_code
\r
104 * i_codeに含まれる、ARCodeの数を指定します。
\r
105 * @throws NyARException
\r
107 public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width,int i_number_of_code) throws NyARException
\r
110 this.square=new NyARDetectSquare(i_param,AR_SQUARE_MAX);
\r
111 this.transmat=new NyARTransMat_O2(i_param);
\r
114 //比較コードの解像度は全部同じかな?(違うとパターンを複数種つくらないといけないから)
\r
115 int cw=i_code[0].getWidth();
\r
116 int ch=i_code[0].getHeight();
\r
117 for(int i=1;i<i_number_of_code;i++){
\r
118 if(cw!=i_code[i].getWidth() || ch!=i_code[i].getHeight()){
\r
120 throw new NyARException();
\r
124 this.patt=new NyARColorPatt_O3(cw,ch);
\r
125 this.number_of_code=i_number_of_code;
\r
127 this.marker_width=i_marker_width;
\r
129 this.match_patt=new NyARMatchPatt_Color_WITHOUT_PCA();
\r
132 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
134 * マーカーを検出するイメージを指定します。
\r
136 * 検出閾値を指定します。0~255の範囲で指定してください。
\r
137 * 通常は100~130くらいを指定します。
\r
139 * 見つかったマーカーの数を返します。
\r
140 * マーカーが見つからない場合は0を返します。
\r
141 * @throws NyARException
\r
143 public int detectMarkerLite(INyARRaster i_image,int i_thresh) throws NyARException
\r
145 NyARSquareList l_square_list=this.square_list;
\r
147 square.detectSquare(i_image,i_thresh,l_square_list);
\r
149 final int number_of_square=l_square_list.getCount();
\r
151 if(number_of_square<1){
\r
156 this.result_holder.reservHolder(number_of_square);
\r
158 //1スクエア毎に、一致するコードを決定していく
\r
159 for(int i=0;i<number_of_square;i++)
\r
161 NyARSquare square=l_square_list.getSquare(i);
\r
162 //評価基準になるパターンをイメージから切り出す
\r
163 if(!this.patt.pickFromRaster(i_image,square)){
\r
164 //イメージの切り出しは失敗することもある。
\r
168 if(!this.match_patt.setPatt(this.patt)){
\r
170 throw new NyARException();
\r
174 match_patt.evaluate(codes[0]);
\r
175 double confidence=match_patt.getConfidence();
\r
176 int direction=match_patt.getDirection();
\r
177 for(int i2=1;i2<this.number_of_code;i2++)
\r
180 match_patt.evaluate(codes[i2]);
\r
181 double c2=match_patt.getConfidence();
\r
185 //より一致するARCodeの情報を保存
\r
187 direction =match_patt.getDirection();
\r
191 final NyARDetectMarkerResult result=this.result_holder.result_array[i];
\r
192 result.arcode_id =code_index;
\r
193 result.confidence=confidence;
\r
194 result.direction =direction;
\r
195 result.ref_square=square;
\r
197 return number_of_square;
\r
200 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。
\r
201 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
203 * マーカーのインデックス番号を指定します。
\r
204 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
206 * 結果値を受け取るオブジェクトを指定してください。
\r
207 * @throws NyARException
\r
209 public void getTransmationMatrix(int i_index,NyARTransMatResult o_result) throws NyARException
\r
211 final NyARDetectMarkerResult result=this.result_holder.result_array[i_index];
\r
212 //一番一致したマーカーの位置とかその辺を計算
\r
214 transmat.transMatContinue(result.ref_square,result.direction,marker_width[result.arcode_id],o_result);
\r
216 transmat.transMat(result.ref_square,result.direction,marker_width[result.arcode_id],o_result);
\r
221 * i_indexのマーカーの一致度を返します。
\r
223 * マーカーのインデックス番号を指定します。
\r
224 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
226 * マーカーの一致度を返します。0~1までの値をとります。
\r
227 * 一致度が低い場合には、誤認識の可能性が高くなります。
\r
228 * @throws NyARException
\r
230 public double getConfidence(int i_index)
\r
232 return this.result_holder.result_array[i_index].confidence;
\r
235 * i_indexのマーカーの方位を返します。
\r
237 * マーカーのインデックス番号を指定します。
\r
238 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
240 * 0,1,2,3の何れかを返します。
\r
242 public int getDirection(int i_index)
\r
244 return this.result_holder.result_array[i_index].direction;
\r
247 * i_indexのマーカーのARCodeインデックスを返します。
\r
249 * マーカーのインデックス番号を指定します。
\r
250 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
253 public int getARCodeIndex(int i_index)
\r
255 return this.result_holder.result_array[i_index].arcode_id;
\r
258 * getTransmationMatrixの計算モードを設定します。
\r
259 * @param i_is_continue
\r
260 * TRUEなら、transMatContinueを使用します。
\r
261 * FALSEなら、transMatを使用します。
\r
263 public void setContinueMode(boolean i_is_continue)
\r
265 this.is_continue=i_is_continue;
\r