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.raster.*;
\r
41 class NyARDetectMarkerResult
\r
43 public int arcode_id;
\r
44 public int direction;
\r
45 public double confidence;
\r
46 public NyARSquare ref_square;
\r
48 class NyARDetectMarkerResultHolder
\r
50 public NyARDetectMarkerResult[] result_array=new NyARDetectMarkerResult[1];
\r
52 * result_holderを最大i_reserve_size個の要素を格納できるように予約します。
\r
53 * @param i_reserve_size
\r
55 public void reservHolder(int i_reserve_size)
\r
57 if(i_reserve_size>=result_array.length){
\r
58 int new_size=i_reserve_size+5;
\r
59 result_array=new NyARDetectMarkerResult[new_size];
\r
60 for(int i=0;i<new_size;i++){
\r
61 result_array[i]=new NyARDetectMarkerResult();
\r
67 * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから
\r
68 * 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。
\r
71 public class NyARDetectMarker{
\r
72 private static final int AR_SQUARE_MAX=300;
\r
73 private boolean is_continue=false;
\r
74 private NyARMatchPatt_Color_WITHOUT_PCA match_patt;
\r
75 private NyARDetectSquare square;
\r
76 private final NyARSquareList square_list=new NyARSquareList(AR_SQUARE_MAX);
\r
77 private NyARCode[] codes;
\r
78 protected NyARTransMat transmat;
\r
79 private double[] marker_width;
\r
80 private int number_of_code;
\r
82 private NyARColorPatt patt;
\r
84 private NyARDetectMarkerResultHolder result_holder=new NyARDetectMarkerResultHolder();
\r
87 * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。
\r
91 * 検出するマーカーのARCode配列を指定します。配列要素のインデックス番号が、そのままgetARCodeIndex関数で
\r
92 * 得られるARCodeインデックスになります。
\r
93 * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。
\r
94 * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
95 * @param i_marker_width
\r
96 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。
\r
97 * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。
\r
98 * @param i_number_of_code
\r
99 * i_codeに含まれる、ARCodeの数を指定します。
\r
100 * @throws NyARException
\r
102 public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width,int i_number_of_code) throws NyARException
\r
105 this.square=new NyARDetectSquare(i_param);
\r
106 this.transmat=new NyARTransMat_O2(i_param);
\r
109 //比較コードの解像度は全部同じかな?(違うとパターンを複数種つくらないといけないから)
\r
110 int cw=i_code[0].getWidth();
\r
111 int ch=i_code[0].getHeight();
\r
112 for(int i=1;i<i_number_of_code;i++){
\r
113 if(cw!=i_code[i].getWidth() || ch!=i_code[i].getHeight()){
\r
115 throw new NyARException();
\r
119 this.patt=new NyARColorPatt_O3(cw,ch);
\r
120 this.number_of_code=i_number_of_code;
\r
122 this.marker_width=i_marker_width;
\r
124 this.match_patt=new NyARMatchPatt_Color_WITHOUT_PCA();
\r
127 * i_imageにマーカー検出処理を実行し、結果を記録します。
\r
129 * マーカーを検出するイメージを指定します。
\r
131 * 検出閾値を指定します。0~255の範囲で指定してください。
\r
132 * 通常は100~130くらいを指定します。
\r
134 * 見つかったマーカーの数を返します。
\r
135 * マーカーが見つからない場合は0を返します。
\r
136 * @throws NyARException
\r
138 public int detectMarkerLite(NyARRaster i_image,int i_thresh) throws NyARException
\r
140 NyARSquareList l_square_list=this.square_list;
\r
142 square.detectSquare(i_image, i_thresh,l_square_list);
\r
144 final int number_of_square=l_square_list.getSquareNum();
\r
146 if(number_of_square<1){
\r
151 this.result_holder.reservHolder(number_of_square);
\r
153 //1スクエア毎に、一致するコードを決定していく
\r
154 for(int i=0;i<number_of_square;i++)
\r
156 NyARSquare square=l_square_list.getSquare(i);
\r
157 //評価基準になるパターンをイメージから切り出す
\r
158 if(!this.patt.pickFromRaster(i_image,square)){
\r
159 //イメージの切り出しは失敗することもある。
\r
163 if(!this.match_patt.setPatt(this.patt)){
\r
165 throw new NyARException();
\r
169 match_patt.evaluate(codes[0]);
\r
170 double confidence=match_patt.getConfidence();
\r
171 int direction=match_patt.getDirection();
\r
172 for(int i2=1;i2<this.number_of_code;i2++)
\r
175 match_patt.evaluate(codes[i2]);
\r
176 double c2=match_patt.getConfidence();
\r
180 //より一致するARCodeの情報を保存
\r
182 direction =match_patt.getDirection();
\r
186 final NyARDetectMarkerResult result=this.result_holder.result_array[i];
\r
187 result.arcode_id =code_index;
\r
188 result.confidence=confidence;
\r
189 result.direction =direction;
\r
190 result.ref_square=square;
\r
192 return number_of_square;
\r
195 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。
\r
196 * 直前に実行したdetectMarkerLiteが成功していないと使えません。
\r
198 * マーカーのインデックス番号を指定します。
\r
199 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
201 * 結果値を受け取るオブジェクトを指定してください。
\r
202 * @throws NyARException
\r
204 public void getTransmationMatrix(int i_index,NyARTransMatResult o_result) throws NyARException
\r
206 final NyARDetectMarkerResult result=this.result_holder.result_array[i_index];
\r
207 //一番一致したマーカーの位置とかその辺を計算
\r
209 transmat.transMatContinue(result.ref_square,result.direction,marker_width[result.arcode_id],o_result);
\r
211 transmat.transMat(result.ref_square,result.direction,marker_width[result.arcode_id],o_result);
\r
216 * i_indexのマーカーの一致度を返します。
\r
218 * マーカーのインデックス番号を指定します。
\r
219 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
221 * マーカーの一致度を返します。0~1までの値をとります。
\r
222 * 一致度が低い場合には、誤認識の可能性が高くなります。
\r
223 * @throws NyARException
\r
225 public double getConfidence(int i_index)
\r
227 return this.result_holder.result_array[i_index].confidence;
\r
230 * i_indexのマーカーの方位を返します。
\r
232 * マーカーのインデックス番号を指定します。
\r
233 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
235 * 0,1,2,3の何れかを返します。
\r
237 public int getDirection(int i_index)
\r
239 return this.result_holder.result_array[i_index].direction;
\r
242 * i_indexのマーカーのARCodeインデックスを返します。
\r
244 * マーカーのインデックス番号を指定します。
\r
245 * 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。
\r
248 public int getARCodeIndex(int i_index)
\r
250 return this.result_holder.result_array[i_index].arcode_id;
\r
253 * getTransmationMatrixの計算モードを設定します。
\r
254 * @param i_is_continue
\r
255 * TRUEなら、transMatContinueを使用します。
\r
256 * FALSEなら、transMatを使用します。
\r
258 public void setContinueMode(boolean i_is_continue)
\r
260 this.is_continue=i_is_continue;
\r