チェンジセット 3337

差分発生行の前後
無視リスト:
コミット日時:
2010/01/23 23:57:12 (2 年前)
コミッタ:
rokubou
ログメッセージ:

Default marker detector.

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/FLARToolKit/trunk/src/org/libspark/flartoolkit/detector/FLARMultiMarkerDetector.as

    r3233 r3337  
    1 /*  
     1/*  
    22 * PROJECT: FLARToolKit 
    33 * -------------------------------------------------------------------------------- 
     
    99 * Copyright (C)2008 Saqoosha 
    1010 * 
    11  * This program is free software; you can redistribute it and/or 
    12  * modify it under the terms of the GNU General Public License 
    13  * as published by the Free Software Foundation; either version 2 
    14  * of the License, or (at your option) any later version. 
     11 * This program is free software: you can redistribute it and/or modify 
     12 * it under the terms of the GNU General Public License as published by 
     13 * the Free Software Foundation, either version 3 of the License, or 
     14 * (at your option) any later version. 
    1515 *  
    1616 * This program is distributed in the hope that it will be useful, 
     
    1818 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1919 * GNU General Public License for more details. 
    20  *  
     20 * 
    2121 * You should have received a copy of the GNU General Public License 
    22  * along with this framework; if not, write to the Free Software 
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     22 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    2423 *  
    2524 * For further information please contact. 
     
    2827 *  
    2928 */ 
    30  
    31 package org.libspark.flartoolkit.detector { 
    32         import flash.display.BitmapData; 
    33         import flash.geom.Point; 
     29package org.libspark.flartoolkit.detector 
     30
     31        import jp.nyatla.nyartoolkit.as3.core.*; 
     32        import jp.nyatla.nyartoolkit.as3.*; 
     33        import jp.nyatla.nyartoolkit.as3.core.transmat.*; 
     34        import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; 
     35        import jp.nyatla.nyartoolkit.as3.core.rasterfilter.rgb2bin.*; 
     36        import jp.nyatla.nyartoolkit.as3.core.raster.*; 
     37        import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; 
     38        import jp.nyatla.nyartoolkit.as3.core.types.*; 
     39        import jp.nyatla.nyartoolkit.as3.core.pickup.*; 
    3440         
    35         import org.libspark.flartoolkit.FLARException; 
    36         import org.libspark.flartoolkit.core.FLARCode; 
    37         import org.libspark.flartoolkit.core.FLARSquare; 
    38         import org.libspark.flartoolkit.core.FLARSquareDetector; 
    39         import org.libspark.flartoolkit.core.FLARSquareStack; 
    40         import org.libspark.flartoolkit.core.IFLARSquareDetector; 
    41         import org.libspark.flartoolkit.core.match.FLARMatchPatt_Color_WITHOUT_PCA; 
    42         import org.libspark.flartoolkit.core.param.FLARParam; 
    43         import org.libspark.flartoolkit.core.pickup.FLARDynamicRatioColorPatt_O3; 
    44         import org.libspark.flartoolkit.core.pickup.IFLARColorPatt; 
    45         import org.libspark.flartoolkit.core.raster.FLARRaster_BitmapData; 
    46         import org.libspark.flartoolkit.core.raster.IFLARRaster; 
    47         import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData; 
    48         import org.libspark.flartoolkit.core.raster.rgb.IFLARRgbRaster; 
    49         import org.libspark.flartoolkit.core.rasterfilter.rgb2bin.FLARRasterFilter_BitmapDataThreshold; 
    50         import org.libspark.flartoolkit.core.transmat.FLARTransMat; 
    51         import org.libspark.flartoolkit.core.transmat.FLARTransMatResult; 
    52         import org.libspark.flartoolkit.core.transmat.IFLARTransMat; 
    53         import org.libspark.flartoolkit.core.types.FLARIntSize;  
     41        import org.libspark.flartoolkit.core.raster.*; 
     42        import org.libspark.flartoolkit.core.rasterfilter.rgb2bin.*; 
     43        import org.libspark.flartoolkit.core.squaredetect.*; 
     44        import org.libspark.flartoolkit.core.*; 
     45        import org.libspark.flartoolkit.*; 
     46        import org.libspark.flartoolkit.core.param.*; 
     47        import org.libspark.flartoolkit.core.raster.rgb.*; 
     48        import org.libspark.flartoolkit.core.transmat.*; 
     49 
     50 
    5451 
    5552        /** 
    56          * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。 
    57          * 最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。 
     53         * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。 
    5854         *  
    5955         */ 
    60         public class FLARMultiMarkerDetector { 
    61  
    62                 private static const AR_SQUARE_MAX:int = 300; 
    63                 private var _sizeCheckEnabled:Boolean = true
    64  
     56        public class FLARMultiMarkerDetector 
     57        { 
     58 
     59                private var _detect_cb:DetectSquareCB
     60                public static const AR_SQUARE_MAX:int = 300; 
    6561                private var _is_continue:Boolean = false; 
    66  
    67                 private var _match_patt:FLARMatchPatt_Color_WITHOUT_PCA; 
    68  
    69                 private var _square_detect:IFLARSquareDetector; 
    70  
    71                 private const _square_list:FLARSquareStack = new FLARSquareStack(AR_SQUARE_MAX); 
    72  
    73                 private var _codes:Array; // FLARCode[] 
    74  
    75                 protected var _transmat:IFLARTransMat; 
    76  
    77                 private var _marker_width:Array; // double[] 
    78  
    79                 private var _number_of_code:int; 
    80  
    81                 // 検出結果の保存用 
    82                 private var _patt:IFLARColorPatt; 
    83  
    84                 private var _result_holder:FLARMultiMarkerDetectorResultHolder = new FLARMultiMarkerDetectorResultHolder(); 
     62                private var _square_detect:NyARSquareContourDetector; 
     63                protected var _transmat:INyARTransMat; 
     64                private var _offset:Vector.<NyARRectOffset>; 
     65 
    8566 
    8667                /** 
     
    8970                 * @param i_param 
    9071                 * カメラパラメータを指定します。 
    91                  * @param i_code        FLARCode[]  
    92                  * 検出するマーカーのARCode配列を指定します。配列要素のインデックス番号が、そのままgetARCodeIndex関数で 得られるARCodeインデックスになります。 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。 
    93                  * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。 
    94                  * @param i_marker_width        double[]  
     72                 * @param i_code 
     73                 * 検出するマーカーのARCode配列を指定します。 
     74                 * 配列要素のインデックス番号が、そのままgetARCodeIndex関数で得られるARCodeインデックスになります。  
     75                 * 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。 
     76                 * @param i_marker_width 
    9577                 * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。 
    9678                 * @param i_number_of_code 
    9779                 * i_codeに含まれる、ARCodeの数を指定します。 
    98                  * @throws FLARException 
    99                  */ 
    100                 public function FLARMultiMarkerDetector(i_param:FLARParam, i_code:Array, i_marker_width:Array, i_number_of_code:int) { 
    101                         const scr_size:FLARIntSize = i_param.getScreenSize(); 
     80                 * @param i_input_raster_type 
     81                 * 入力ラスタのピクセルタイプを指定します。この値は、INyARBufferReaderインタフェイスのgetBufferTypeの戻り値を指定します。 
     82                 * @throws NyARException 
     83                 */ 
     84                public function FLARMultiMarkerDetector(i_param:FLARParam, i_code:Vector.<FLARCode>, i_marker_width:Vector.<Number>, i_number_of_code:int) 
     85                { 
     86                        initInstance(i_param,i_code,i_marker_width,i_number_of_code); 
     87                        return; 
     88                } 
     89                protected function initInstance( 
     90                        i_ref_param:FLARParam, 
     91                        i_ref_code:Vector.<FLARCode>, 
     92                        i_marker_width:Vector.<Number>, 
     93                        i_number_of_code:int):void 
     94                { 
     95 
     96                        var scr_size:NyARIntSize=i_ref_param.getScreenSize(); 
    10297                        // 解析オブジェクトを作る 
    103                         this._square_detect = new FLARSquareDetector(i_param.getDistortionFactor(), scr_size); 
    104                         this._transmat = new FLARTransMat(i_param); 
    105                         // 比較コードを保存 
    106                         this._codes = i_code; 
    107                         // 比較コードの解像度は全部同じかな?(違うとパターンを複数種つくらないといけないから) 
    108                         const cw:int = i_code[0].getWidth(); 
    109                         const ch:int = i_code[0].getHeight(); 
    110                         const mpw:uint = FLARCode(i_code[0]).markerPercentWidth; 
    111                         const mph:uint = FLARCode(i_code[0]).markerPercentHeight; 
    112                         for (var i:int = 1; i < i_number_of_code; i++) { 
    113                                 if (cw != i_code[i].getWidth() || ch != i_code[i].getHeight()) { 
    114                                         // 違う解像度のが混ざっている。 
    115                                         //throw new FLARException(); 
    116                                         throw new FLARException("all patterns in an application must be the same width and height."); 
    117                                 } 
    118                                  
    119                                 // SOC: added markerPercentWidth/Height checking 
    120                                 if (mpw != i_code[i].markerPercentWidth || mph != i_code[i].markerPercentHeight) { 
    121                                         throw new FLARException("all patterns in an application must have the same ratio of pattern-to-border width and height."); 
    122                                 } 
    123                         } 
    124                          
    125                         // SOC: replaced FLARColorPatt_O3 with FLARDynamicRatioColorPatt_O3, 
    126                         //              to match FLARSingleMarkerDetector and 
    127                         //              to enable better tracking of variable-border-width markers.  
    128 //                      // 評価パターンのホルダを作る 
    129 //                      this._patt = new FLARColorPatt_O3(cw, ch); 
    130                          
    131                         //マーカ幅を算出 
    132                         var markerWidthByDec:Number = i_code[0].markerPercentWidth / 10; 
    133                         //マーカ高を算出 
    134                         var markerHeightByDec:Number = i_code[0].markerPercentHeight / 10; 
    135  
    136                         //評価パターンのホルダを作成 
    137                         this._patt = new FLARDynamicRatioColorPatt_O3(cw, ch, markerWidthByDec, markerHeightByDec); 
    138                          
    139                         this._number_of_code = i_number_of_code; 
    140  
    141                         this._marker_width = i_marker_width; 
    142                         // 評価器を作る。 
    143                         this._match_patt = new FLARMatchPatt_Color_WITHOUT_PCA(); 
     98                        var cw:int = i_ref_code[0].getWidth(); 
     99                        var ch:int = i_ref_code[0].getHeight(); 
     100 
     101                        //detectMarkerのコールバック関数 
     102                        this._detect_cb=new DetectSquareCB( 
     103                                new NyARColorPatt_Perspective_O2(cw, ch,4,25), 
     104                                i_ref_code,i_number_of_code,i_ref_param); 
     105                        this._transmat = new NyARTransMat(i_ref_param); 
     106                        //NyARToolkitプロファイル 
     107                        this._square_detect =new FLARSquareContourDetector(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); 
     108                        this._tobin_filter=new FLARRasterFilter_Threshold(100); 
     109 
     110                        //実サイズ保存 
     111                        this._offset = NyARRectOffset.createArray(i_number_of_code); 
     112                        for(var i:int=0;i<i_number_of_code;i++){ 
     113                                this._offset[i].setSquare(i_marker_width[i]); 
     114                        } 
    144115                        //2値画像バッファを作る 
    145 //                      this._bin_raster = new FLARBinRaster(scr_size.w, scr_size.h); 
    146                         this._bin_raster = new FLARRaster_BitmapData(scr_size.w, scr_size.h); 
    147                 } 
    148  
    149                 private var _bin_raster:IFLARRaster; 
    150  
    151 //              private var _tobin_filter:FLARRasterFilter_ARToolkitThreshold = new FLARRasterFilter_ARToolkitThreshold(100); 
    152                 private var _tobin_filter:FLARRasterFilter_BitmapDataThreshold = new FLARRasterFilter_BitmapDataThreshold(100); 
     116                        this._bin_raster=new FLARBinRaster(scr_size.w,scr_size.h); 
     117                        return;          
     118                } 
     119                 
     120                private var _bin_raster:FLARBinRaster; 
     121 
     122                private var _tobin_filter:INyARRasterFilter_Rgb2Bin; 
    153123 
    154124                /** 
     
    160130                 * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。 
    161131                 * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。 
    162                  * @throws FLARException 
    163                  */ 
    164                 public function detectMarkerLite(i_raster:IFLARRgbRaster, i_threshold:int):int { 
     132                 * @throws NyARException 
     133                 */ 
     134                public function detectMarkerLite(i_raster:FLARRgbRaster_BitmapData,i_threshold:int):int 
     135                { 
    165136                        // サイズチェック 
    166                         if(this._sizeCheckEnabled && !this._bin_raster.getSize().isEqualSizeO(i_raster.getSize())) { 
    167                                 throw new FLARException("サイズ不一致(" + this._bin_raster.getSize() + ":" + i_raster.getSize()); 
     137                        if (!this._bin_raster.getSize().isEqualSize_NyARIntSize(i_raster.getSize())) { 
     138                                throw new NyARException(); 
    168139                        } 
    169140 
    170141                        // ラスタを2値イメージに変換する. 
    171                         // SOC: threshold incoming image according to brightness. 
    172                         //              passing -1 for threshold allows developers to apply custom thresholding algorithms 
    173                         //              prior to passing source image to FLARToolkit. 
    174                         if (i_threshold != -1) { 
    175                                 // apply FLARToolkit thresholding 
    176                                 this._tobin_filter.setThreshold(i_threshold); 
    177                                 this._tobin_filter.doFilter(i_raster, this._bin_raster); 
    178                         } else { 
    179                                 // copy source BitmapData as-is, without applying FLARToolkit thresholding 
    180                                 var srcBitmapData:BitmapData = FLARRgbRaster_BitmapData(i_raster).bitmapData; 
    181                                 var dstBitmapData:BitmapData = FLARRaster_BitmapData(this._bin_raster).bitmapData; 
    182                                 dstBitmapData.copyPixels(srcBitmapData, srcBitmapData.rect, new Point()); 
    183                         } 
    184  
    185                         var l_square_list:FLARSquareStack = this._square_list; 
    186                         // スクエアコードを探す 
    187                         // SOC: begin by detecting all possible markers ('square' outlines (may be rotated in any of three axes relative to camera)) 
    188                         this._square_detect.detectMarker(this._bin_raster, l_square_list); 
    189  
    190                         const number_of_square:int = l_square_list.getLength(); 
    191                         // コードは見つかった? 
    192                         if (number_of_square < 1) { 
    193                                 // ないや。おしまい。 
    194                                 // SOC: if no markers found, exit 
    195                                 return 0; 
    196                         } 
    197                         // 保持リストのサイズを調整 
    198                         // SOC: ensure enough FLARMultiMarkerDetectorResult instances to hold all possible detected markers 
    199                         this._result_holder.reservHolder(number_of_square); 
    200  
    201                         // 1スクエア毎に、一致するコードを決定していく 
    202                         // SOC: loop through all found squares and compare each with all possible patterns 
    203                         var i:int; 
    204                         var square:FLARSquare; 
    205                         var code_index:int; 
    206                         var confidence:Number; 
    207                         var direction:int; 
    208                         var i2:int; 
    209                         var c2:Number; 
    210                         for (i = 0; i < number_of_square; i++) { 
    211                                 square = l_square_list.getItem(i) as FLARSquare; 
    212                                 // 評価基準になるパターンをイメージから切り出す 
    213                                 // SOC: attempt to read a possible pattern from this found square 
    214                                 if (!this._patt.pickFromRaster(i_raster, square)) { 
    215                                         // イメージの切り出しは失敗することもある。 
    216                                         // SOC: if a pattern cannot be extracted, skip to next square 
    217                                         continue; 
    218                                 } 
    219                                 // パターンを評価器にセット 
    220                                 // SOC: not clear on this part... 
    221                                 if (!this._match_patt.setPatt(this._patt)) { 
    222                                         // 計算に失敗した。 
    223                                         throw new FLARException(); 
    224                                 } 
    225                                 // コードと順番に比較していく 
    226                                 // SOC: first, match against first pattern 
    227                                 code_index = 0; 
    228                                 _match_patt.evaluate(_codes[0]); 
    229                                 confidence = _match_patt.getConfidence(); 
    230                                 direction = _match_patt.getDirection(); 
    231                                 //trace(i,0, confidence,"(",square.label.area,")"); 
    232                                 for (i2 = 1;i2 < this._number_of_code; i2++) { 
    233                                         // コードと比較する 
    234                                         // SOC: then, match against each additional pattern, looking for the best possible match 
    235                                         _match_patt.evaluate(_codes[i2]); 
    236                                         c2 = _match_patt.getConfidence(); 
    237                                         //trace(i, i2, c2,"(",square.label.area,")"); 
    238                                         if (confidence > c2) { 
    239                                                 continue; 
    240                                         } 
    241                                         // より一致するARCodeの情報を保存 
    242                                         // SOC: if a better match, store values 
    243                                         code_index = i2; 
    244                                         direction = _match_patt.getDirection(); 
    245                                         confidence = c2; 
    246                                 } 
    247                                 // i番目のパターン情報を保存する。 
    248                                 // SOC: store the values corresponding to the best pattern match 
    249                                 var result:FLARMultiMarkerDetectorResult = this._result_holder.result_array[i]; 
    250                                 result._codeId = code_index; 
    251                                 result._confidence = confidence; 
    252                                 result._direction = direction; 
    253                                 result._square = square; 
    254                         } 
    255                         return number_of_square; 
     142                        (FLARRasterFilter_Threshold(this._tobin_filter)).setThreshold(i_threshold); 
     143                        this._tobin_filter.doFilter(i_raster, this._bin_raster); 
     144 
     145                        //detect 
     146                        this._detect_cb.init(i_raster); 
     147                        this._square_detect.detectMarkerCB(this._bin_raster,this._detect_cb); 
     148 
     149                        //見付かった数を返す。 
     150                        return this._detect_cb.result_stack.getLength(); 
    256151                } 
    257152 
     
    263158                 * @param o_result 
    264159                 * 結果値を受け取るオブジェクトを指定してください。 
    265                  * @throws FLARException 
    266                  */ 
    267                 public function getTransmationMatrix(i_index:int, o_result:FLARTransMatResult):void { 
    268                         const result:FLARMultiMarkerDetectorResult = this._result_holder.result_array[i_index]; 
     160                 * @throws NyARException 
     161                 */ 
     162                public function getTransformMatrix(i_index:int, o_result:FLARTransMatResult):void 
     163                { 
     164                        var result:NyARDetectMarkerResult = this._detect_cb.result_stack.getItem(i_index); 
    269165                        // 一番一致したマーカーの位置とかその辺を計算 
    270166                        if (_is_continue) { 
    271                                 _transmat.transMatContinue(result.square, result.direction, _marker_width[result.codeId], o_result); 
     167                                _transmat.transMatContinue(result.square, this._offset[result.arcode_id], o_result); 
    272168                        } else { 
    273                                 _transmat.transMat(result.square, result.direction, _marker_width[result.codeId], o_result); 
     169                                _transmat.transMat(result.square, this._offset[result.arcode_id], o_result); 
    274170                        } 
    275171                        return; 
    276172                } 
    277173 
    278                 public function getResult(i_index:int):FLARMultiMarkerDetectorResult 
    279                 { 
    280                         return this._result_holder.result_array[i_index]; 
    281                 } 
    282174                /** 
    283175                 * i_indexのマーカーの一致度を返します。 
     
    286178                 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 
    287179                 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。 
    288                  * @throws FLARException 
    289                  */ 
    290                 public function getConfidence(i_index:int):Number { 
    291                         return this._result_holder.result_array[i_index].confidence; 
    292                 } 
    293  
    294                 /** 
    295                  * i_indexのマーカーの方位を返します。 
    296                  *  
    297                  * @param i_index 
    298                  * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 
    299                  * @return 0,1,2,3の何れかを返します。 
    300                  */ 
    301                 public function getDirection(i_index:int):int { 
    302                         return this._result_holder.result_array[i_index].direction; 
    303                 } 
    304  
     180                 * @throws NyARException 
     181                 */ 
     182                public function getConfidence(i_index:int):Number 
     183                { 
     184                        return this._detect_cb.result_stack.getItem(i_index).confidence; 
     185                } 
    305186                /** 
    306187                 * i_indexのマーカーのARCodeインデックスを返します。 
     
    310191                 * @return 
    311192                 */ 
    312                 public function getARCodeIndex(i_index:int):int { 
    313                         // SOC: incorrect property name. 
    314                         //return this._result_holder.result_array[i_index].arcode_id; 
    315                         return this._result_holder.result_array[i_index].codeId; 
     193                public function getARCodeIndex(i_index:int):int 
     194                { 
     195                        return this._detect_cb.result_stack.getItem(i_index).arcode_id; 
    316196                } 
    317197 
     
    322202                 * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。 
    323203                 */ 
    324                 public function setContinueMode(i_is_continue:Boolean):void { 
     204                public function setContinueMode(i_is_continue:Boolean):void 
     205                { 
    325206                        this._is_continue = i_is_continue; 
    326207                } 
     208 
     209        } 
     210} 
     211 
     212import jp.nyatla.nyartoolkit.as3.core.match.*; 
     213import jp.nyatla.nyartoolkit.as3.core.pickup.*; 
     214import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; 
     215import jp.nyatla.nyartoolkit.as3.*; 
     216import jp.nyatla.nyartoolkit.as3.core.types.stack.*; 
     217import jp.nyatla.nyartoolkit.as3.core.match.*; 
     218import jp.nyatla.nyartoolkit.as3.core.param.*; 
     219import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; 
     220import jp.nyatla.nyartoolkit.as3.detector.*; 
     221import jp.nyatla.nyartoolkit.as3.core.types.*; 
     222import org.libspark.flartoolkit.core.* 
     223/** 
     224 * detectMarkerのコールバック関数 
     225 */ 
     226class DetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback 
     227{ 
     228        //公開プロパティ 
     229        public var result_stack:NyARDetectMarkerResultStack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX); 
     230        //参照インスタンス 
     231        public var _ref_raster:INyARRgbRaster; 
     232        //所有インスタンス 
     233        private var _inst_patt:INyARColorPatt; 
     234        private var _deviation_data:NyARMatchPattDeviationColorData; 
     235        private var _match_patt:Vector.<NyARMatchPatt_Color_WITHOUT_PCA>; 
     236        private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); 
     237        private var _coordline:NyARCoord2Linear; 
     238         
     239        public function DetectSquareCB(i_inst_patt:INyARColorPatt, i_ref_code:Vector.<FLARCode>, i_num_of_code:int, i_param:NyARParam) 
     240        { 
     241                var cw:int = i_ref_code[0].getWidth(); 
     242                var ch:int = i_ref_code[0].getHeight(); 
     243 
     244                this._inst_patt=i_inst_patt; 
     245                this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); 
     246                this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); 
     247 
     248                //NyARMatchPatt_Color_WITHOUT_PCA[]の作成 
     249                this._match_patt=new Vector.<NyARMatchPatt_Color_WITHOUT_PCA>(i_num_of_code); 
     250                this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); 
     251                for (var i:int = 1; i < i_num_of_code; i++){ 
     252                        //解像度チェック 
     253                        if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { 
     254                                throw new NyARException(); 
     255                        } 
     256                        this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); 
     257                } 
     258                return; 
     259        } 
     260        private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); 
     261        /** 
     262         * 矩形が見付かるたびに呼び出されます。 
     263         * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 
     264         */ 
     265        public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int ,i_vertex_index:Vector.<int>):void 
     266        { 
     267                var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; 
     268                //輪郭座標から頂点リストに変換 
     269                var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; 
     270                vertex[0].x=i_coordx[i_vertex_index[0]]; 
     271                vertex[0].y=i_coordy[i_vertex_index[0]]; 
     272                vertex[1].x=i_coordx[i_vertex_index[1]]; 
     273                vertex[1].y=i_coordy[i_vertex_index[1]]; 
     274                vertex[2].x=i_coordx[i_vertex_index[2]]; 
     275                vertex[2].y=i_coordy[i_vertex_index[2]]; 
     276                vertex[3].x=i_coordx[i_vertex_index[3]]; 
     277                vertex[3].y=i_coordy[i_vertex_index[3]]; 
     278         
     279                //画像を取得 
     280                if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ 
     281                        return; 
     282                } 
     283                //取得パターンをカラー差分データに変換して評価する。 
     284                this._deviation_data.setRaster(this._inst_patt); 
     285 
     286                //最も一致するパターンを割り当てる。 
     287                var square_index:int,direction:int; 
     288                var confidence:Number; 
     289                this._match_patt[0].evaluate(this._deviation_data,mr); 
     290                square_index=0; 
     291                direction=mr.direction; 
     292                confidence=mr.confidence; 
     293                //2番目以降 
     294                var i:int; 
     295                for(i=1;i<this._match_patt.length;i++){ 
     296                        this._match_patt[i].evaluate(this._deviation_data,mr); 
     297                        if (confidence > mr.confidence) { 
     298                                continue; 
     299                        } 
     300                        // もっと一致するマーカーがあったぽい 
     301                        square_index = i; 
     302                        direction = mr.direction; 
     303                        confidence = mr.confidence; 
     304                } 
     305                //最も一致したマーカ情報を、この矩形の情報として記録する。 
     306                var result:NyARDetectMarkerResult = this.result_stack.prePush(); 
     307                result.arcode_id = square_index; 
     308                result.confidence = confidence; 
     309 
     310                var sq:NyARSquare=result.square; 
     311                //directionを考慮して、squareを更新する。 
     312                for(i=0;i<4;i++){ 
     313                        var idx:int=(i+4 - direction) % 4; 
     314                        this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); 
     315                } 
     316                for (i = 0; i < 4; i++) { 
     317                        //直線同士の交点計算 
     318                        if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ 
     319                                throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK 
     320                        } 
     321                } 
     322        } 
     323        public function init(i_raster:INyARRgbRaster):void 
     324        { 
     325                this._ref_raster=i_raster; 
     326                this.result_stack.clear(); 
    327327                 
    328                 /** 
    329                  * 入力画像のサイズチェックをする/しない的な。(デフォルトではチェックする) 
    330                  */ 
    331                 public function get sizeCheckEnabled():Boolean { 
    332                         return this._sizeCheckEnabled; 
    333                 } 
    334                 public function set sizeCheckEnabled(value:Boolean):void { 
    335                         this._sizeCheckEnabled = value; 
    336                 } 
    337                  
    338                 /** 
    339                  * SOC: added accessor for thresholded BitmapData of source image, 
    340                  * for use in debugging. 
    341                  */ 
    342                 public function get thresholdedBitmapData () :BitmapData { 
    343                         try { 
    344                                 return FLARRaster_BitmapData(this._bin_raster).bitmapData; 
    345                         } catch (e:Error) { 
    346                                 return null; 
    347                         } 
    348                          
    349                         return null; 
    350                 } 
    351                  
    352                 public function get labelingBitmapData () :BitmapData { 
    353                         return FLARSquareDetector(this._square_detect).labelingBitmapData; 
    354                 } 
    355328        } 
    356329} 
    357330 
    358331 
     332class NyARDetectMarkerResult 
     333{ 
     334        public var arcode_id:int; 
     335        public var confidence:Number; 
     336 
     337        public var square:NyARSquare=new NyARSquare(); 
     338} 
     339 
     340 
     341class NyARDetectMarkerResultStack extends NyARObjectStack 
     342{ 
     343        public function NyARDetectMarkerResultStack(i_length:int) 
     344        { 
     345                super(i_length); 
     346        } 
     347        protected override function createArray(i_length:int):Vector.<*> 
     348        { 
     349                var ret:Vector.<NyARDetectMarkerResult>= new Vector.<NyARDetectMarkerResult>(i_length); 
     350                for (var i:int =0; i < i_length; i++){ 
     351                        ret[i] = new NyARDetectMarkerResult(); 
     352                } 
     353                return Vector.<*>(ret); 
     354        }        
     355} 
  • as3/FLARToolKit/trunk/src/org/libspark/flartoolkit/detector/FLARSingleMarkerDetector.as

    r3312 r3337  
    1 /*  
     1/*  
    22 * PROJECT: FLARToolKit 
    33 * -------------------------------------------------------------------------------- 
     
    99 * Copyright (C)2008 Saqoosha 
    1010 * 
    11  * This program is free software; you can redistribute it and/or 
    12  * modify it under the terms of the GNU General Public License 
    13  * as published by the Free Software Foundation; either version 2 
    14  * of the License, or (at your option) any later version. 
     11 * This program is free software: you can redistribute it and/or modify 
     12 * it under the terms of the GNU General Public License as published by 
     13 * the Free Software Foundation, either version 3 of the License, or 
     14 * (at your option) any later version. 
    1515 *  
    1616 * This program is distributed in the hope that it will be useful, 
     
    1818 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1919 * GNU General Public License for more details. 
    20  *  
     20 * 
    2121 * You should have received a copy of the GNU General Public License 
    22  * along with this framework; if not, write to the Free Software 
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     22 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    2423 *  
    2524 * For further information please contact. 
     
    2827 *  
    2928 */ 
    30  
    31 package org.libspark.flartoolkit.detector { 
    32         import org.libspark.flartoolkit.core.pickup.FLARDynamicRatioColorPatt_O3; 
    33         import org.libspark.flartoolkit.FLARException; 
    34         import org.libspark.flartoolkit.core.FLARCode; 
    35         import org.libspark.flartoolkit.core.FLARSquare; 
    36         import org.libspark.flartoolkit.core.FLARSquareDetector; 
    37         import org.libspark.flartoolkit.core.FLARSquareStack; 
    38         import org.libspark.flartoolkit.core.IFLARSquareDetector; 
    39         import org.libspark.flartoolkit.core.match.FLARMatchPatt_Color_WITHOUT_PCA; 
    40         import org.libspark.flartoolkit.core.param.FLARParam; 
    41         import org.libspark.flartoolkit.core.pickup.FLARColorPatt_O3; 
    42         import org.libspark.flartoolkit.core.pickup.IFLARColorPatt; 
    43         import org.libspark.flartoolkit.core.raster.FLARRaster_BitmapData; 
    44         import org.libspark.flartoolkit.core.raster.IFLARRaster; 
    45         import org.libspark.flartoolkit.core.raster.rgb.IFLARRgbRaster; 
    46         import org.libspark.flartoolkit.core.rasterfilter.rgb2bin.FLARRasterFilter_BitmapDataThreshold; 
    47         import org.libspark.flartoolkit.core.rasterfilter.rgb2bin.IFLARRasterFilter_RgbToBin; 
    48         import org.libspark.flartoolkit.core.transmat.FLARTransMat; 
    49         import org.libspark.flartoolkit.core.transmat.FLARTransMatResult; 
    50         import org.libspark.flartoolkit.core.transmat.IFLARTransMat; 
    51         import org.libspark.flartoolkit.core.types.FLARIntSize;  
    52  
    53         /** 
    54          * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。 
    55          * <span lang="en">ARCode image markers to find one that best matches 
    56          * the individual is a class to calculate the transformation matrix.</span> 
    57          */ 
    58         public class FLARSingleMarkerDetector { 
    59  
    60                 private static const AR_SQUARE_MAX:int = 100; 
    61  
    62                 private var _sizeCheckEnabled:Boolean = true; 
     29package org.libspark.flartoolkit.detector 
     30
     31        import flash.display.*; 
     32        import jp.nyatla.nyartoolkit.as3.core.match.*; 
     33        import jp.nyatla.nyartoolkit.as3.core.pickup.*; 
     34        import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; 
     35        import jp.nyatla.nyartoolkit.as3.core.transmat.*; 
     36        import jp.nyatla.nyartoolkit.as3.core.raster.*; 
     37        import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; 
     38        import jp.nyatla.nyartoolkit.as3.core.rasterreader.*; 
     39        import jp.nyatla.nyartoolkit.as3.core.rasterfilter.rgb2bin.*; 
     40        import jp.nyatla.nyartoolkit.as3.core.types.*; 
     41        import org.libspark.flartoolkit.core.raster.*; 
     42        import org.libspark.flartoolkit.core.rasterfilter.rgb2bin.*; 
     43        import org.libspark.flartoolkit.core.squaredetect.*; 
     44        import org.libspark.flartoolkit.core.*; 
     45        import org.libspark.flartoolkit.*; 
     46        import org.libspark.flartoolkit.core.param.*; 
     47        import org.libspark.flartoolkit.core.raster.rgb.*; 
     48        import org.libspark.flartoolkit.core.transmat.*; 
     49        public class FLARSingleMarkerDetector 
     50        {        
    6351                private var _is_continue:Boolean = false; 
    64                 private var _match_patt:FLARMatchPatt_Color_WITHOUT_PCA; 
    65                 private var _square_detect:IFLARSquareDetector; 
    66  
    67                 private const _square_list:FLARSquareStack = new FLARSquareStack(AR_SQUARE_MAX); 
    68  
    69                 private var _code:FLARCode; 
    70  
    71                 protected var _transmat:IFLARTransMat; 
    72  
    73                 private var _marker_width:Number; 
    74  
    75                 // 検出結果の保存用 
    76                 //Save the results for detection 
    77                 private var _detected_direction:int; 
    78                 private var _detected_confidence:Number; 
    79                 private var _detected_square:FLARSquare; 
    80  
    81                 private var _patt:IFLARColorPatt; 
    82                 private var _bin_raster:IFLARRaster; 
    83                 private var _tobin_filter:IFLARRasterFilter_RgbToBin; 
    84  
    85                 public function get filter ():IFLARRasterFilter_RgbToBin { return _tobin_filter; } 
    86                 public function set filter (f:IFLARRasterFilter_RgbToBin):void { if (f != null) _tobin_filter = f; } 
    87  
    88                 /** 
    89                  * 検出するARCodeとカメラパラメータから、1個のARCodeを検出する 
    90                  * <span lang="en">FLARSingleDetectMarkerインスタンスを作ります。 
    91                  * ARCode from the camera parameters to detect and to detect  
    92                  * a single FLARSingleDetectMarker ARCode create an instance.</span> 
    93                  *  
    94                  * @param i_param カメラパラメータを指定します。 
    95                  * <span lang="en">The camera parameters</span> 
    96                  * @param i_code 検出するARCodeを指定します。 
    97                  * <span lang="en">Specify ARCode detection.</span> 
    98                  * @param i_marker_width ARコードの物理サイズを、ミリメートルで指定します。 
    99                  * <span lang="en">The physical size of the code specified in millimeters.</span> 
    100                  * @throws FLARException 
    101                  */ 
    102                 public function FLARSingleMarkerDetector(i_param:FLARParam, i_code:FLARCode, i_marker_width:Number) { 
    103                         const scr_size:FLARIntSize = i_param.getScreenSize();            
     52                private var _square_detect:FLARSquareContourDetector; 
     53                protected var _transmat:INyARTransMat; 
     54                //画処理用 
     55                private var _bin_raster:FLARBinRaster; 
     56                protected var _tobin_filter:INyARRasterFilter_Rgb2Bin; 
     57                private var _detect_cb:DetectSquareCB; 
     58                private var _offset:NyARRectOffset;  
     59 
     60 
     61                public function FLARSingleMarkerDetector(i_ref_param:FLARParam,i_ref_code:FLARCode,i_marker_width:Number) 
     62                { 
     63                        var th:INyARRasterFilter_Rgb2Bin=new FLARRasterFilter_Threshold(100); 
     64                        var patt_inst:INyARColorPatt; 
     65                        var sqdetect_inst:FLARSquareContourDetector; 
     66                        var transmat_inst:INyARTransMat; 
     67 
     68                        patt_inst=new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25); 
     69                        sqdetect_inst=new FLARSquareContourDetector(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); 
     70                        transmat_inst=new NyARTransMat(i_ref_param); 
     71                        initInstance(patt_inst,sqdetect_inst,transmat_inst,th,i_ref_param,i_ref_code,i_marker_width); 
     72                        return; 
     73                } 
     74                protected function initInstance( 
     75                        i_patt_inst:INyARColorPatt, 
     76                        i_sqdetect_inst:FLARSquareContourDetector, 
     77                        i_transmat_inst:INyARTransMat, 
     78                        i_filter:INyARRasterFilter_Rgb2Bin, 
     79                        i_ref_param:FLARParam, 
     80                        i_ref_code:FLARCode, 
     81                        i_marker_width:Number):void 
     82                { 
     83                        var scr_size:NyARIntSize=i_ref_param.getScreenSize();            
    10484                        // 解析オブジェクトを作る 
    105                         //Make the object analysis 
    106                         this._square_detect = new FLARSquareDetector(i_param.getDistortionFactor(), scr_size); 
    107                         this._transmat = new FLARTransMat(i_param); 
    108                         // 比較コードを保存 
    109                         //Save the code comparison 
    110                         this._code = i_code; 
    111                         this._marker_width = i_marker_width; 
    112  
    113                         // 評価パターンのホルダを作る 
    114                         //Make a pattern holder rating 
    115  
    116                         //マーカ幅を算出 
    117                         //Calculate the width marker 
    118                         var markerWidthByDec:Number = this._code.markerPercentWidth/10; 
    119                         //マーカ高を算出 
    120                         //Calculate the height marker 
    121                         var markerHeightByDec:Number = this._code.markerPercentHeight / 10; 
    122  
    123                         //評価パターンのホルダを作成 
    124                         //Create a pattern holder rating 
    125                         this._patt = new FLARDynamicRatioColorPatt_O3(this._code.getWidth(),  
    126                                                                                                                   this._code.getHeight(), 
    127                                                                                                                   markerWidthByDec, 
    128                                                                                                                   markerHeightByDec); 
    129  
    130                         // 評価器を作る。 
    131                         //Make evaluator. 
    132                         this._match_patt = new FLARMatchPatt_Color_WITHOUT_PCA(); 
     85                        this._square_detect = i_sqdetect_inst; 
     86                        this._transmat = i_transmat_inst; 
     87                        this._tobin_filter=i_filter; 
    13388                        //2値画像バッファを作る 
    134                         //Two images to create a buffer value 
    135                         this._bin_raster = new FLARRaster_BitmapData(scr_size.w, scr_size.h); 
    136                         //2値画像化フィルタの作成 
    137                         //Create a filter value of 2 images 
    138                         this._tobin_filter= new FLARRasterFilter_BitmapDataThreshold(100); 
    139                 } 
    140  
     89                        this._bin_raster=new FLARBinRaster(scr_size.w,scr_size.h); 
     90                        //_detect_cb 
     91                        this._detect_cb=new DetectSquareCB(i_patt_inst,i_ref_code,i_ref_param); 
     92                        //オフセットを作成 
     93                        this._offset=new NyARRectOffset(); 
     94                        this._offset.setSquare(i_marker_width); 
     95                        return; 
     96                         
     97                } 
     98                 
    14199                /** 
    142100                 * i_imageにマーカー検出処理を実行し、結果を記録します。 
    143                  * <span lang="en">i_image marker to detect and perform and record the results.</span> 
    144                  *  
    145                  * @param i_raster マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータと一致していなければなりません。 
    146                  * <span lang="en">Specifies the image to detect the marker. The image size must match the camera parameters.</span> 
     101                 *  
     102                 * @param i_raster 
     103                 * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ 
     104                 * と一致していなければなりません。 
    147105                 * @return マーカーが検出できたかを真偽値で返します。 
    148                  * <span lang="en">Returns a boolean value whether the marker was detected.</span> 
    149                  * @throws FLARException 
    150                  */ 
    151                 public function detectMarkerLite(i_raster:IFLARRgbRaster, i_threshold:int):Boolean { 
     106                 * @throws NyARException 
     107                 */ 
     108                public function detectMarkerLite(i_raster:FLARRgbRaster_BitmapData,i_threshold:int):Boolean 
     109                { 
     110                        FLARRasterFilter_Threshold(this._tobin_filter).setThreshold(i_threshold); 
    152111                        //サイズチェック 
    153                         //Check size 
    154                         if (!this._bin_raster.getSize().isEqualSizeO(i_raster.getSize())) { 
    155                                 if (this._sizeCheckEnabled )  
    156                                         throw new FLARException("サイズ不一致(" + this._bin_raster.getSize() + ":" + i_raster.getSize()); 
    157                                 else { 
    158                                         //サイズに合わせて、2値画像バッファを作る 
    159                                         //According to the size of two images to create a buffer value 
    160                                         this._bin_raster = new FLARRaster_BitmapData(i_raster.getSize().w, i_raster.getSize().h); 
    161                                 } 
     112                        if(!this._bin_raster.getSize().isEqualSize_NyARIntSize(i_raster.getSize())){ 
     113                                throw new FLARException(); 
    162114                        } 
    163115 
    164116                        //ラスタを2値イメージに変換する. 
    165                         //Value of 2 to convert the raster image. 
    166                         this._tobin_filter.setThreshold(i_threshold); 
    167                         this._tobin_filter.doFilter(i_raster, this._bin_raster); 
    168                  
    169                  
    170                         this._detected_square = null; 
    171                         var l_square_list:FLARSquareStack = this._square_list; 
    172                         // スクエアコードを探す 
    173                         //Square Code Search 
    174                         this._square_detect.detectMarker(this._bin_raster, l_square_list); 
    175  
    176  
    177                         var number_of_square:int = l_square_list.getLength(); 
    178                         // コードは見つかった? 
    179                         //Code found? 
    180                         if (number_of_square < 1) { 
     117                        this._tobin_filter.doFilter(i_raster,this._bin_raster); 
     118 
     119                        //コールバックハンドラの準備 
     120                        this._detect_cb.init(i_raster); 
     121                        //矩形を探す(戻り値はコールバック関数で受け取る。) 
     122                        this._square_detect.detectMarkerCB(this._bin_raster,_detect_cb); 
     123                        if(this._detect_cb.confidence==0){ 
    181124                                return false; 
    182125                        } 
    183  
    184                         // 評価基準になるパターンをイメージから切り出す 
    185                         //Cut out the pattern from an image which criteria 
    186                         if (!this._patt.pickFromRaster(i_raster, l_square_list.getItem(0) as FLARSquare)) { 
    187                                 // パターンの切り出しに失敗 
    188                                 //Failed to cut out the pattern 
    189                                 return false; 
    190                         } 
    191                         // パターンを評価器にセット 
    192                         //Pattern set evaluator 
    193                         if (!this._match_patt.setPatt(this._patt)) { 
    194                                 // 計算に失敗した。Calculation fails. 
    195                                 return false; 
    196                         } 
    197                         // コードと比較する 
    198                         //Code and compare 
    199                         this._match_patt.evaluate(this._code); 
    200                         var square_index:int = 0; 
    201                         var direction:int = this._match_patt.getDirection(); 
    202                         var confidence:Number = this._match_patt.getConfidence(); 
    203                          
    204                         var i:int; 
    205                         var c2:Number; 
    206                         for (i = 1;i < number_of_square; i++) { 
    207                                 // 次のパターンを取得 
    208                                 //Obtain the following pattern: 
    209                                 this._patt.pickFromRaster(i_raster, l_square_list.getItem(i) as FLARSquare); 
    210                                 // 評価器にセットする。 
    211                                 //To set the evaluator. 
    212                                 this._match_patt.setPatt(this._patt); 
    213                                 // コードと比較する 
    214                                 //Code and compare 
    215                                 this._match_patt.evaluate(this._code); 
    216                                 c2 = this._match_patt.getConfidence(); 
    217                                 if (confidence > c2) { 
    218                                         continue; 
    219                                 } 
    220                                 // もっと一致するマーカーがあったぽい 
    221                                 //Poi was more consistent marker 
    222                                 square_index = i; 
    223                                 direction = this._match_patt.getDirection(); 
    224                                 confidence = c2; 
    225                         } 
    226                         // マーカー情報を保存 
    227                         //Save marker information 
    228                         this._detected_square = l_square_list.getItem(square_index) as FLARSquare; 
    229                         this._detected_direction = direction; 
    230                         this._detected_confidence = confidence; 
    231126                        return true; 
    232127                } 
    233  
    234128                /** 
    235129                 * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。 
    236130                 * 直前に実行したdetectMarkerLiteが成功していないと使えません。 
    237                  * <span lang="en">Transformation matrix to calculate the detected markers, o_result to return a value.  
    238                  * DetectMarkerLite not work before you run and have not been successful.</span"> 
    239                  *  
    240                  * @param o_result 変換行列を受け取るオブジェクトを指定します。 
    241                  * <span lang="en">Specifies the object that receives the transformation matrix.</span> 
    242                  * @throws FLARException 
    243                  */ 
    244                 public function getTransformMatrix(o_result:FLARTransMatResult):void { 
     131                 *  
     132                 * @param o_result 
     133                 * 変換行列を受け取るオブジェクトを指定します。 
     134                 * @throws NyARException 
     135                 */ 
     136                public function getTransformMatrix(o_result:FLARTransMatResult):void 
     137                { 
    245138                        // 一番一致したマーカーの位置とかその辺を計算 
    246139                        if (this._is_continue) { 
    247                                 this._transmat.transMatContinue(this._detected_square, this._detected_direction, this._marker_width, o_result); 
     140                                this._transmat.transMatContinue(this._detect_cb.square,this._offset, o_result); 
    248141                        } else { 
    249                                 this._transmat.transMat(this._detected_square, this._detected_direction, this._marker_width, o_result); 
    250                         } 
    251                         return; 
    252                 } 
    253  
     142                                this._transmat.transMat(this._detect_cb.square,this._offset, o_result); 
     143                        } 
     144                        return; 
     145                } 
    254146                /** 
    255147                 * 検出したマーカーの一致度を返します。 
    256                  * <span lang="en">Returns the coincidence of the marker was detected.</span> 
    257148                 *  
    258149                 * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。 
    259                  * <span lang="en">Returns the coincidence of the marker. Takes a value between 0 and 1. 
    260                  * If a lesser degree of match, the higher the possibility of false positives.</span> 
    261                  * @throws FLARException 
    262                  */ 
    263                 public function getConfidence():Number { 
    264                         return this._detected_confidence; 
    265                 } 
    266  
    267                 /** 
    268                  * 検出したマーカーの方位を返します。 
    269                  *  
    270                  * @return Returns whether any of 0,1,2,3. 
    271                  * 0,1,2,3の何れかを返します。 
    272                  */ 
    273                 public function getDirection():int { 
    274                         return this._detected_direction; 
    275                 } 
    276  
     150                 * @throws NyARException 
     151                 */ 
     152                public function getConfidence():Number 
     153                { 
     154                        return this._detect_cb.confidence; 
     155                } 
    277156                /** 
    278157                 * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。 
     
    280159                 * @param i_is_continue 
    281160                 * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。 
    282                  * If TRUE, transMatCont calculate the compatibility. If FALSE, transMat calculate the compatibility. 
    283                 */ 
    284                 public function setContinueMode(i_is_continue:Boolean):void
     161                 */ 
     162                public function setContinueMode(i_is_continue:Boolean):void 
     163               
    285164                        this._is_continue = i_is_continue; 
    286165                } 
     166        } 
     167} 
     168 
     169import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; 
     170import jp.nyatla.nyartoolkit.as3.core.match.*; 
     171import jp.nyatla.nyartoolkit.as3.core.pickup.*; 
     172import jp.nyatla.nyartoolkit.as3.core.param.*; 
     173import jp.nyatla.nyartoolkit.as3.core.raster.*; 
     174import jp.nyatla.nyartoolkit.as3.core.*; 
     175import jp.nyatla.nyartoolkit.as3.core.raster.*; 
     176import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; 
     177import jp.nyatla.nyartoolkit.as3.*; 
     178import jp.nyatla.nyartoolkit.as3.core.types.*; 
     179 
     180/** 
     181 * detectMarkerのコールバック関数 
     182 */ 
     183class DetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback 
     184{ 
     185        //公開プロパティ 
     186        public var confidence:Number; 
     187        public var square:NyARSquare=new NyARSquare(); 
     188         
     189        //参照インスタンス 
     190        private var _ref_raster:INyARRgbRaster; 
     191        //所有インスタンス 
     192        private var _inst_patt:INyARColorPatt; 
     193        private var _deviation_data:NyARMatchPattDeviationColorData; 
     194        private var _match_patt:NyARMatchPatt_Color_WITHOUT_PCA; 
     195        private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); 
     196        private var _coordline:NyARCoord2Linear; 
     197         
     198        public function DetectSquareCB(i_inst_patt:INyARColorPatt,i_ref_code:NyARCode,i_param:NyARParam) 
     199        { 
     200                this._inst_patt=i_inst_patt; 
     201                this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight()); 
     202                this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); 
     203                this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code); 
     204                return; 
     205        } 
     206        private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); 
     207        /** 
     208         * 矩形が見付かるたびに呼び出されます。 
     209         * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 
     210         */ 
     211        public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int,i_vertex_index:Vector.<int>):void 
     212        { 
     213                var i:int; 
     214                var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; 
     215                //輪郭座標から頂点リストに変換 
     216                var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; 
     217                vertex[0].x=i_coordx[i_vertex_index[0]]; 
     218                vertex[0].y=i_coordy[i_vertex_index[0]]; 
     219                vertex[1].x=i_coordx[i_vertex_index[1]]; 
     220                vertex[1].y=i_coordy[i_vertex_index[1]]; 
     221                vertex[2].x=i_coordx[i_vertex_index[2]]; 
     222                vertex[2].y=i_coordy[i_vertex_index[2]]; 
     223                vertex[3].x=i_coordx[i_vertex_index[3]]; 
     224                vertex[3].y=i_coordy[i_vertex_index[3]]; 
     225         
     226                //画像を取得 
     227                if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ 
     228                        return; 
     229                } 
     230                //取得パターンをカラー差分データに変換して評価する。 
     231                this._deviation_data.setRaster(this._inst_patt); 
     232                if(!this._match_patt.evaluate(this._deviation_data,mr)){ 
     233                        return; 
     234                } 
     235                //現在の一致率より低ければ終了 
     236                if (this.confidence > mr.confidence){ 
     237                        return; 
     238                } 
     239                //一致率の高い矩形があれば、方位を考慮して頂点情報を作成 
     240                var sq:NyARSquare=this.square; 
     241                this.confidence = mr.confidence; 
     242                //directionを考慮して、squareを更新する。 
     243                for(i=0;i<4;i++){ 
     244                        var idx:int=(i+4 - mr.direction) % 4; 
     245                        this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); 
     246                } 
     247                for (i = 0; i < 4; i++) { 
     248                        //直線同士の交点計算 
     249                        if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ 
     250                                throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK 
     251                        } 
     252                } 
     253        } 
     254        public function init(i_raster:INyARRgbRaster):void 
     255        { 
     256                this.confidence=0; 
     257                this._ref_raster=i_raster; 
    287258                 
    288                 /** 
    289                  * @return Total return detected FLARSquare 1. Detection Dekinakattara null. 
    290                  * 検出した FLARSquare 1 個返す。検出できなかったら null。 
    291                  */ 
    292                 public function getSquare():FLARSquare { 
    293                         return this._detected_square; 
    294                 } 
    295                  
    296                 /** 
    297                  * @return FLARSquareStack detected rectangle that contains all the returns. 
    298                  * 検出した全ての四角形を含む FLARSquareStack を返す。 
    299                  */ 
    300                 public function getSquareList():FLARSquareStack { 
    301                         return this._square_list; 
    302                 } 
    303                  
    304                 /** 
    305                  * 入力画像のサイズチェックをする/しない的な。(デフォルトではチェックする) 
    306                  * Check that the input image size / an not. (The default is checked) 
    307                  */ 
    308                 public function get sizeCheckEnabled():Boolean { 
    309                         return this._sizeCheckEnabled; 
    310                 } 
    311                 public function set sizeCheckEnabled(value:Boolean):void { 
    312                         this._sizeCheckEnabled = value; 
    313                 } 
    314259        } 
    315260}