Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
203 chris 1
/*
2
 * PROJECT: NyARToolkit
3
 * --------------------------------------------------------------------------------
4
 * This work is based on the original ARToolKit developed by
5
 *   Hirokazu Kato
6
 *   Mark Billinghurst
7
 *   HITLab, University of Washington, Seattle
8
 * http://www.hitl.washington.edu/artoolkit/
9
 *
10
 * The NyARToolkit is Java version ARToolkit class library.
11
 * Copyright (C)2008 R.Iizuka
12
 *
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this framework; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 *
27
 * For further information please contact.
28
 *      http://nyatla.jp/nyatoolkit/
29
 *      <airmail(at)ebony.plala.or.jp>
30
 *
31
 */
32
package jp.nyatla.nyartoolkit.detector;
33
 
34
import jp.nyatla.nyartoolkit.NyARException;
35
import jp.nyatla.nyartoolkit.core.*;
36
import jp.nyatla.nyartoolkit.core.match.NyARMatchPatt_Color_WITHOUT_PCA;
37
import jp.nyatla.nyartoolkit.core.raster.*;
38
/**
39
 * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。
40
 *
41
 */
42
public class NyARSingleDetectMarker{
43
    private static final int AR_SQUARE_MAX=100;
44
    private boolean is_continue=false;
45
    private NyARMatchPatt_Color_WITHOUT_PCA match_patt;
46
    private NyARDetectSquare square;
47
    private final NyARSquareList square_list=new NyARSquareList(AR_SQUARE_MAX);
48
    private NyARCode code;
49
    protected NyARTransMat transmat;
50
    private double marker_width;
51
    //検出結果の保存用
52
    private int detected_direction;
53
    private double detected_confidence;
54
    private NyARSquare detected_square;
55
    private NyARColorPatt patt;
56
    /**
57
     * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。
58
     * @param i_param
59
     * カメラパラメータを指定します。
60
     * @param i_code
61
     * 検出するARCodeを指定します。
62
     * @param i_marker_width
63
     * ARコードの物理サイズを、ミリメートルで指定します。
64
     * @throws NyARException
65
     */
66
    public NyARSingleDetectMarker(NyARParam i_param,NyARCode i_code,double i_marker_width) throws NyARException
67
    {
68
        //解析オブジェクトを作る
69
        this.square=new NyARDetectSquare(i_param);
70
        this.transmat=new NyARTransMat_O2(i_param);
71
        //比較コードを保存
72
        this.code=i_code;
73
        this.marker_width=i_marker_width;
74
        //評価パターンのホルダを作る
75
        this.patt=new NyARColorPatt_O3(code.getWidth(),code.getHeight());
76
        //評価器を作る。
77
        this.match_patt=new NyARMatchPatt_Color_WITHOUT_PCA(); 
78
    }
79
    /**
80
     * i_imageにマーカー検出処理を実行し、結果を記録します。
81
     * @param i_image
82
     * マーカーを検出するイメージを指定します。
83
     * @param i_thresh
84
     * 検出閾値を指定します。0~255の範囲で指定してください。
85
     * 通常は100~130くらいを指定します。
86
     * @return
87
     * マーカーが検出できたかを真偽値で返します。
88
     * @throws NyARException
89
     */
90
    public boolean detectMarkerLite(NyARRaster i_image,int i_thresh) throws NyARException
91
    {
92
        detected_square=null;
93
        NyARSquareList l_square_list=this.square_list;
94
        //スクエアコードを探す
95
        square.detectSquare(i_image, i_thresh,l_square_list);
96
 
97
        int number_of_square=l_square_list.getSquareNum();
98
        //コードは見つかった?
99
        if(number_of_square<1){
100
            return false;
101
        }
102
 
103
        //評価基準になるパターンをイメージから切り出す
104
        if(!patt.pickFromRaster(i_image,l_square_list.getSquare(0))){
105
            //パターンの切り出しに失敗
106
            return false;
107
        }
108
        //パターンを評価器にセット
109
        if(!this.match_patt.setPatt(patt)){
110
            //計算に失敗した。
111
            throw new NyARException();
112
        }
113
        //コードと比較する
114
        match_patt.evaluate(code);
115
        int square_index=0;
116
        int direction=match_patt.getDirection();
117
        double confidence=match_patt.getConfidence();
118
        for(int i=1;i<number_of_square;i++){
119
            //次のパターンを取得
120
            patt.pickFromRaster(i_image,l_square_list.getSquare(i));
121
            //評価器にセットする。
122
            match_patt.setPatt(patt);
123
            //コードと比較する
124
            match_patt.evaluate(code);
125
            double c2=match_patt.getConfidence();
126
            if(confidence>c2){
127
                continue;
128
            }
129
            //もっと一致するマーカーがあったぽい
130
            square_index=i;
131
            direction=match_patt.getDirection();
132
            confidence=c2;
133
        }
134
        //マーカー情報を保存
135
        detected_square=l_square_list.getSquare(square_index);
136
        detected_direction=direction;
137
        detected_confidence=confidence;
138
        return true;
139
    }
140
    /**
141
     * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。
142
     * 直前に実行したdetectMarkerLiteが成功していないと使えません。
143
     * @param o_result
144
     * 変換行列を受け取るオブジェクトを指定します。
145
     * @throws NyARException
146
     */    
147
    public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException
148
    {
149
        //一番一致したマーカーの位置とかその辺を計算
150
        if(is_continue){
151
            transmat.transMatContinue(detected_square,detected_direction,marker_width,o_result);
152
        }else{
153
            transmat.transMat(detected_square,detected_direction,marker_width,o_result);
154
        }
155
        return;
156
    }    
157
    /**
158
     * 検出したマーカーの一致度を返します。
159
     * @return
160
     * マーカーの一致度を返します。0~1までの値をとります。
161
     * 一致度が低い場合には、誤認識の可能性が高くなります。
162
     * @throws NyARException
163
     */
164
    public double getConfidence()
165
    {
166
        return detected_confidence;
167
    }
168
    /**
169
     * 検出したマーカーの方位を返します。
170
     * @return
171
     * 0,1,2,3の何れかを返します。
172
     */
173
    public int getDirection()
174
    {
175
        return detected_direction;
176
    }
177
    /**
178
     * getTransmationMatrixの計算モードを設定します。
179
     * 初期値はTRUEです。
180
     * @param i_is_continue
181
     * TRUEなら、transMatCont互換の計算をします。
182
     * FALSEなら、transMat互換の計算をします。
183
     */
184
    public void setContinueMode(boolean i_is_continue)
185
    {
186
        this.is_continue=i_is_continue;
187
    }
188
}
189
 
190
 
191
 
192
 
193
 
194
 
195
 
196
 
197
 
198
 
199
 
200
 
201
 
202
 
203