チェンジセット 1772

差分発生行の前後
無視リスト:
コミット日時:
2008/10/30 19:03:30 (2 ヶ月前)
コミッタ:
tarotarorg
ログメッセージ:

キューブ型マーカの認識に向けて格闘中

ファイル:

凡例:

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

    r1755 r1772  
    5050                private var _right:FLARCode; 
    5151                private var _size:int; 
     52                private var _space:int; 
    5253 
    5354                /** 
     
    6061                 * @param       right 
    6162                 * @param       size 
     63                 * @param       space 
    6264                 */ 
    6365                public function CubeMarker(top:FLARCode, 
     
    6769                                                                        left:FLARCode = null, 
    6870                                                                        right:FLARCode = null, 
    69                                                                         size:int = 80)  
     71                                                                        size:int = 80, 
     72                                                                        space:int = 20 )  
    7073                { 
    7174                        this.top = top; 
     
    118121                public function set front(value:FLARCode):void  
    119122                { 
    120                         _front = value; 
     123                        if (_front == null || this.checkResolution(_front, value)) _front = value; 
     124                        else throw new FLARException("コード[front]の解像度が不正です。"); 
    121125                } 
    122126                 
     
    125129                public function set back(value:FLARCode):void  
    126130                { 
    127                         _back = value; 
     131                        if (_back == null || this.checkResolution(_back, value)) _back = value; 
     132                        else throw new FLARException("コード[back]の解像度が不正です。"); 
    128133                } 
    129134                 
     
    132137                public function set left(value:FLARCode):void  
    133138                { 
    134                         _left = value; 
     139                        if (_left == null || this.checkResolution(_left, value)) _left = value; 
     140                        else throw new FLARException("コード[left]の解像度が不正です。"); 
    135141                } 
    136142                 
  • as3/FLARToolKit/trunk/src/org/libspark/flartoolkit/detector/FLARCubeMarkerDetector.as

    r1755 r1772  
    3030 
    3131package org.libspark.flartoolkit.detector { 
     32        import flash.utils.Dictionary; 
    3233        import org.libspark.flartoolkit.core.raster.FLARRaster_BitmapData; 
    3334        import org.libspark.flartoolkit.core.raster.IFLARRaster; 
     
    5859 
    5960                private static const AR_SQUARE_MAX:int = 300; 
     61                 
     62                /** 
     63                 * マーカ検索時の、画像バッファと入力画像のサイズ比較を行うかどうか 
     64                 */ 
    6065                private var _sizeCheckEnabled:Boolean = true; 
    6166 
     67                /** 
     68                 *  
     69                 */ 
    6270                private var _is_continue:Boolean = false; 
    6371 
     72                /** 
     73                 * パターン評価器 
     74                 */ 
    6475                private var _match_patt:FLARMatchPatt_Color_WITHOUT_PCA; 
    6576 
     77                /** 
     78                 * 入力画像からマーカ候補となるSquareを検出するDetector 
     79                 */ 
    6680                private var _square_detect:IFLARSquareDetector; 
    6781 
     82                /** 
     83                 * 入力画像内のSquare一覧 
     84                 */ 
    6885                private const _square_list:FLARSquareStack = new FLARSquareStack(AR_SQUARE_MAX); 
    6986 
    70                 private var _codes:Array; // FLARCode[] 
     87                /** 
     88                 * キューブ型マーカ 
     89                 */ 
    7190                private var _marker:CubeMarker; 
    72                 private var _codeResult:Array; 
    73  
     91 
     92                /** 
     93                 *  
     94                 */ 
    7495                protected var _transmat:IFLARTransMat; 
    7596 
     97                /** 
     98                 * マーカの幅 
     99                 */ 
    76100                private var _marker_width:int; 
    77  
    78                 private var _number_of_code:int; 
    79101 
    80102                // 検出結果の保存用 
    81103                private var _patt:IFLARColorPatt; 
    82104 
    83                 private var _result_holder:FLARMultiMarkerDetectorResultHolder = new FLARMultiMarkerDetectorResultHolder(); 
    84  
    85                  /** 
    86                   *  
     105                /** 
     106                 * 「この一致度を越えたら、その時点でキューブ発見としてよい」と言う一致度の閾値 
     107                 */ 
     108                private var _immidiateEndConfidence:Number; 
     109                 
     110                /** 
     111                 * 「Squareに対して全マーカを検査した結果、最大一致度がこの値を超えていたらマーカ発見としてよい」と言う一致度の閾値 
     112                 */ 
     113                private var _endConfidence:Number; 
     114 
     115                /** 
     116                 * 検出時のフラグ 
     117                 */ 
     118                private var _detectFlag:uint; 
     119                private static const DETECT_FLAGS:Dictionary = new Dictionary(); 
     120 
     121                /** 
     122                 *  
    87123                  * @param      i_param カメラパラメータを指定します。 
    88124                  * @param      i_code  キューブ型マーカを指定します。 
    89                   */ 
    90                 public function FLARCubeMarkerDetector(i_param:FLARParam, i_code:CubeMarker) { 
     125                 * @param       immidateEndConfidence 
     126                 * @param       endConfidence 
     127                 */ 
     128                public function FLARCubeMarkerDetector(i_param:FLARParam, i_code:CubeMarker,  
     129                                                                                                immidateEndConfidence:Number = 0.9,endConfidence:Number = 0.8) { 
    91130                         
     131                        DETECT_FLAGS[CubeMarkerDirection.TOP] = 1; 
     132                        DETECT_FLAGS[CubeMarkerDirection.BOTTOM] = 2; 
     133                        DETECT_FLAGS[CubeMarkerDirection.FRONT] = 4; 
     134                        DETECT_FLAGS[CubeMarkerDirection.BACK] = 8; 
     135                        DETECT_FLAGS[CubeMarkerDirection.LEFT] = 16; 
     136                        DETECT_FLAGS[CubeMarkerDirection.RIGHT] = 32; 
     137 
     138                        //閾値を設定 
     139                        this.setEndConfidences(immidateEndConfidence, endConfidence); 
     140 
    92141                        //scrとは、「screen」の略。srcの打ち間違いではない事に注意! 
    93142                        const scr_size:FLARIntSize = i_param.getScreenSize(); 
     
    97146                        this._transmat = new FLARTransMat(i_param); 
    98147                         
    99                         // 比較コードを保存 
     148                        // キューブ型マーカを保存 
    100149                        this._marker = i_code; 
    101150 
     151                        //キューブ型マーカの内、nullの面を調査し、保持 
     152                        this.checkNullMarker(); 
     153                         
    102154                        // 評価パターンのホルダを作る 
    103155                        this._patt = new FLARColorPatt_O3(this._marker.top.getWidth(), this._marker.top.getWidth()); 
     
    112164                        this._bin_raster = new FLARRaster_BitmapData(scr_size.w, scr_size.h); 
    113165                } 
    114  
     166                 
     167                private function checkNullMarker():void 
     168                { 
     169                        this._detectFlag = 0; 
     170                        if (this._marker == null) return; 
     171                        this._detectFlag &= this._marker.top == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.TOP]; 
     172                        this._detectFlag &= this._marker.bottom == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.BOTTOM]; 
     173                        this._detectFlag &= this._marker.front == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.FRONT]; 
     174                        this._detectFlag &= this._marker.back == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.BACK]; 
     175                        this._detectFlag &= this._marker.left == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.LEFT]; 
     176                        this._detectFlag &= this._marker.right == null ? 0 : DETECT_FLAGS[CubeMarkerDirection.RIGHT]; 
     177                } 
     178 
     179                /** 
     180                 * 終了閾値を設定する。 
     181                 * @param       immidate 
     182                 * @param       end 
     183                 */ 
     184                private function setEndConfidences(immidate:Number, end:Number):void 
     185                { 
     186                        if (immidate < end) { 
     187                                throw new ArgumentError("即時終了条件" + immidate + "は、終了条件" + end + "以上である必要があります。"); 
     188                        } 
     189                        this._immidiateEndConfidence = immidate; 
     190                        this._endConfidence = end; 
     191                } 
    115192                /** 
    116193                 * #detectMarkerLiteで使用する作業領域 
     
    125202                /** 
    126203                 * i_rasterにマーカー検出処理を実行し、結果を記録します。 
     204                 * 即時終了条件: 
     205                 * 1.一致度がthis._immidiateEndConfidenceを超えるマーカが見つかった場合⇒そのマーカが一致とし、即時終了 
    127206                 * 終了条件: 
    128                  * 1.一致度が0.9を超えるマーカが見つかった場合⇒そのマーカが一致とし、即時終了 
    129                  * 2.Squareに対する全マーカ発見後、一致度が0.75を超えるマーカが見つかり、かつそれがSquareに対する最高一致度⇒そのマーカが一致とみなす 
     207                 * 1.Squareに対する全マーカ評価後、そのSquareに対して最も高い一致度がthis._endConfidenceを超える 
     208                 *   マーカが見つかった場合⇒そのマーカが一致とみなし、終了 
    130209                 * 探索条件: 
    131210                 * 1.Squareに対して最高一致度を出したマーカは、他のSquareで評価しない 
     
    145224                public function detectMarkerLite(i_raster:IFLARRgbRaster, i_threshold:int):Object { 
    146225                        // サイズチェック 
    147                         if(this._sizeCheckEnabled && !this._bin_raster.getSize().isEqualSizeO(i_raster.getSize())) { 
    148                                 throw new FLARException("サイズ不一致(" + this._bin_raster.getSize() + ":" + i_raster.getSize()); 
     226                        if (!this._bin_raster.getSize().isEqualSizeO(i_raster.getSize())) { 
     227                                if (this._sizeCheckEnabled )  
     228                                        throw new FLARException("サイズ不一致(" + this._bin_raster.getSize() + ":" + i_raster.getSize()); 
     229                                else { 
     230                                        //2値画像バッファを作る 
     231                                        this._bin_raster = new FLARRaster_BitmapData(i_raster.getSize().w, i_raster.getSize().h); 
     232                                } 
    149233                        } 
    150234 
     
    168252                        // Square毎に、一致するコードを決定していく 
    169253                        var square:FLARSquare;  //検査対象のSquareを格納しておく 
    170                         var code_index:int;             // 
    171                         var confidence:Number;  //マーカパターンとSquareの一致度 
    172                         var direction:int;              //マーカの向き 
    173                         var i2:int;                             // 
    174                         var c2:Number;                  //一致度2 
    175                          
     254                        //結果を保存しておくオブジェクト 
     255                        var result:CubeMarkerMatchResultTemporaryHolder = new CubeMarkerMatchResultTemporaryHolder(); 
     256                        var maxMarkerDirection:String; 
     257                        var detectFlagTemp:uint = this._detectFlag; 
     258 
    176259                        //Squareリストを走査する 
    177260                        for (var i:int = 0; i < number_of_square; i++) { 
     
    190273                                } 
    191274                                // コードと順番に比較していく 
     275 
     276                                //1.まずはTOPから 
     277                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.TOP]) { 
     278                                        _match_patt.evaluate(this._marker.top); 
     279                                        result.topConf = _match_patt.getConfidence(); 
     280                                        result.topDir = _match_patt.getDirection(); 
     281                                        result.topSq = square; 
     282                                        if (result.top > this._immidiateEndConfidence) { 
     283                                                //即時終了条件1に一致 
     284                                                return this.createDetectedResult 
     285                                                        (result.topConf, result.topDir, square, CubeMarkerDirection.TOP); 
     286                                        } 
     287                                } 
    192288                                 
     289                                //2.次はFRONT 
     290                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.FRONT]) { 
     291                                        _match_patt.evaluate(this._marker.front); 
     292                                        result.frontConf = _match_patt.getConfidence(); 
     293                                        result.frontDir = _match_patt.getDirection(); 
     294                                        result.frontSq = square; 
     295                                        if (result.frontConf > this._immidiateEndConfidence) { 
     296                                                //即時終了条件1に一致 
     297                                                return this.createDetectedResult 
     298                                                        (result.frontConf, result.frontDir, square, CubeMarkerDirection.FRONT); 
     299                                        } 
     300                                } 
    193301                                 
    194                                 //まずはTOPから 
    195                                 _match_patt.evaluate(this._marker.top); 
    196                                 confidence = _match_patt.getConfidence(); 
    197                                 direction = _match_patt.getDirection(); 
    198                                 trace("top_confidence:", confidence); 
    199                                 if (confidence > 0.9) { 
    200                                         //終了条件1に一致 
     302                                //3.次はBACK 
     303                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.BACK]) { 
     304                                        _match_patt.evaluate(this._marker.back); 
     305                                        result.backConf = _match_patt.getConfidence(); 
     306                                        result.backDir = _match_patt.getDirection(); 
     307                                        result.backSq = square; 
     308                                        if (result.backConf > this._immidiateEndConfidence) { 
     309                                                //即時終了条件1に一致 
     310                                                return this.createDetectedResult 
     311                                                        (result.backConf, result.backDir, square, CubeMarkerDirection.BACK); 
     312                                        } 
     313                                } 
     314 
     315                                //4.次はLEFT 
     316                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.LEFT]) { 
     317                                        _match_patt.evaluate(this._marker.left); 
     318                                        result.leftConf = _match_patt.getConfidence(); 
     319                                        result.leftDir = _match_patt.getDirection(); 
     320                                        result.leftSq = square; 
     321                                        if (result.leftConf > this._immidiateEndConfidence) { 
     322                                                //即時終了条件1に一致 
     323                                                return this.createDetectedResult 
     324                                                        (result.leftConf, result.leftDir, square, CubeMarkerDirection.LEFT); 
     325                                        } 
     326                                } 
     327 
     328                                //5.次はRIGHT 
     329                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.RIGHT]) { 
     330                                        _match_patt.evaluate(this._marker.right); 
     331                                        result.rightConf = _match_patt.getConfidence(); 
     332                                        result.rightDir = _match_patt.getDirection(); 
     333                                        result.rightSq = square; 
     334                                        if (result.rightConf > this._immidiateEndConfidence) { 
     335                                                //即時終了条件1に一致 
     336                                                return this.createDetectedResult 
     337                                                        (result.rightConf, result.rightDir, square, CubeMarkerDirection.RIGHT); 
     338                                        } 
     339                                } 
     340 
     341                                //6.最後はBOTTOM 
     342                                if (detectFlagTemp & DETECT_FLAGS[CubeMarkerDirection.BOTTOM]) { 
     343                                        _match_patt.evaluate(this._marker.bottom); 
     344                                        result.bottomConf = _match_patt.getConfidence(); 
     345                                        result.bottomDir = _match_patt.getDirection(); 
     346                                        result.bottomSq = square; 
     347                                        if (result.bottomConf > this._immidiateEndConfidence) { 
     348                                                //即時終了条件1に一致 
     349                                                return this.createDetectedResult 
     350                                                        (result.bottomConf, result.bottomDir, square, CubeMarkerDirection.BOTTOM); 
     351                                        } 
     352                                } 
     353                                 
     354                                //一番一致度の高かった面を調査 
     355                                result.checkMaxConfidenceDirection(); 
     356                                //最高一致度が終了条件に達しているかを調査 
     357                                if (result.currentMaxConfidence > this._endConfidence) { 
     358                                        //終了条件1に合致したため、終了 
     359                                        return this.createDetectedResult(result.currentMaxConfidence,  
     360                                                                                                        result.currentMaxConfidenceDirection,  
     361                                                                                                        square, 
     362                                                                                                        result.currentMaxConfidenceMarkerDirection); 
     363                                } else if (detectFlagTemp & DETECT_FLAGS[result.currentMaxConfidenceMarkerDirection]) { 
     364                                        //最高一致したマーカは、次は評価しない 
     365                                        detectFlagTemp ^= DETECT_FLAGS[result.currentMaxConfidenceMarkerDirection]; 
     366                                } 
     367                                 
     368                        } 
     369                        return this.createDetectedResult(result.currentMaxConfidence,  
     370                                                                                        result.currentMaxConfidenceDirection,  
     371                                                                                        square, 
     372                                                                                        result.currentMaxConfidenceMarkerDirection); 
     373                        //return number_of_square; 
     374                } 
     375 
     376                private function createDetectedResult(confidence:Number,direction:int square:FLARSquare,markerDirection:String):Object  
     377                { 
    201378                                        var ret:Object = new Object(); 
    202                                         ret.direction = direction
    203                                         ret.confidence = confidence
     379                                        ret.direction = confidence
     380                                        ret.confidence = rdirection
    204381                                        ret.square = square; 
    205                                         ret.markerDirection = CubeMarkerDirection.TOP
     382                                        ret.markerDirection = markerDirection
    206383                                        return ret; 
    207                                 } 
    208                                 for (i2 = 1; i2 < this._number_of_code; i2++) { 
    209                                         if (this._codeResult[i2]) continue; 
    210                                         // コードと比較する 
    211                                         _match_patt.evaluate(_codes[i2]); 
    212                                         c2 = _match_patt.getConfidence(); 
    213                                         trace(i, i2, c2,"(",square.label.area,")"); 
    214                                         if (confidence > c2) { 
    215                                                 continue; 
    216                                         } 
    217                                         // より一致するARCodeの情報を保存 
    218                                         code_index = i2; 
    219                                         direction = _match_patt.getDirection(); 
    220                                         confidence = c2; 
    221                                 } 
    222                                 //if (confidence > 0.8) trace("GREAT!!",i, code_index, confidence); 
    223                                 // i番目のパターン情報を保存する。 
    224                                 var result:FLARMultiMarkerDetectorResult = this._result_holder.result_array[i]; 
    225                                 result._codeId = code_index; 
    226                                 result._confidence = confidence; 
    227                                 result._direction = direction; 
    228                                 result._square = square; 
    229                                 this._codeResult[code_index] = true; 
    230                                 if (confidence > 0.8) return result; 
    231                         } 
    232                         return null; 
    233                         //return number_of_square; 
    234                 } 
    235  
     384                } 
    236385                /** 
    237386                 * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。 
     
    314463        } 
    315464} 
     465import org.libspark.flartoolkit.core.FLARSquare; 
     466/** 
     467 * キューブ型マーカの探索結果を一時的にためておくホルダ 
     468 */ 
     469class CubeMarkerMatchResultTemporaryHolder { 
     470 
     471        public var topConf:Number; 
     472        public var bottomConf:Number; 
     473        public var frontConf:Number; 
     474        public var backConf:Number; 
     475        public var leftConf:Number; 
     476        public var rightConf:Number; 
     477 
     478        public var topDir:int; 
     479        public var bottomDir:int; 
     480        public var frontDir:int; 
     481        public var backDir:int; 
     482        public var leftDir:int; 
     483        public var rightDir:int; 
     484 
     485        public var topSq:FLARSquare; 
     486        public var bottomSq:FLARSquare; 
     487        public var frontSq:FLARSquare; 
     488        public var backSq:FLARSquare; 
     489        public var leftSq:FLARSquare; 
     490        public var rightSq:FLARSquare; 
     491 
     492        private var _currentMaxConfidenceMarkerDirection:String; 
     493        private var _currentMaxConfidence:Number; 
     494        private var _currentMaxConfidenceDirection:int; 
     495        private var _currentMaxConfidenceSquare:FLARSquare; 
     496 
     497        /** 
     498         * 現在保持している一致度の中で、最も一致度が高い面を返す 
     499         * @return 
     500         */ 
     501        public function checkMaxConfidenceDirection():Object 
     502        { 
     503                _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.TOP; 
     504                _currentMaxConfidence = topConf; 
     505                _currentMaxConfidenceDirection = topDir; 
     506                _currentMaxConfidenceSquare = topSq; 
     507                 
     508                if (bottomConf > maxConf) { 
     509                        _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.BOTTOM; 
     510                        _currentMaxConfidence = bottomConf; 
     511                        _currentMaxConfidenceDirection = bottomDir; 
     512                        _currentMaxConfidenceSquare = bottomSq; 
     513                } 
     514                if (frontConf > maxConf) { 
     515                        _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.FRONT; 
     516                        _currentMaxConfidence = frontConf; 
     517                        _currentMaxConfidenceDirection = frontDir; 
     518                        _currentMaxConfidenceSquare = frontSq; 
     519                } 
     520                if (backConf > maxConf) { 
     521                        _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.BACK; 
     522                        _currentMaxConfidence = backConf; 
     523                        _currentMaxConfidenceDirection = backDir; 
     524                        _currentMaxConfidenceSquare = backSq; 
     525                } 
     526                if (leftConf > maxConf) { 
     527                        _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.LEFT; 
     528                        _currentMaxConfidence = leftConf; 
     529                        _currentMaxConfidenceDirection = leftDir; 
     530                        _currentMaxConfidenceSquare = leftSq; 
     531                } 
     532                if (rightConf > maxConf) { 
     533                        _currentMaxConfidenceMarkerDirection = CubeMarkerDirection.RIGHT; 
     534                        _currentMaxConfidence = rightConf; 
     535                        _currentMaxConfidenceDirection = rightDir; 
     536                        _currentMaxConfidenceSquare = rightSq; 
     537                } 
     538        } 
     539         
     540        /** 
     541         * 保持している情報を全て消去する。 
     542         */ 
     543        public function reset():void 
     544        { 
     545                topConf = 0; 
     546                bottomConf = 0; 
     547                frontConf = 0; 
     548                backConf = 0; 
     549                leftConf = 0; 
     550                rightConf = 0; 
     551 
     552                topDir = 0; 
     553                bottomDir = 0; 
     554                frontDir = 0; 
     555                backDir = 0; 
     556                leftDir = 0; 
     557                rightDir = 0; 
     558        } 
     559        /** 
     560         * 現在、最も一致度の高いマーカの面 
     561         */ 
     562        public function get currentMaxConfidenceMarkerDirection():String { return _currentMaxConfidenceMarkerDirection; } 
     563         
     564        public function get currentMaxConfidence():Number { return _currentMaxConfidence; } 
     565         
     566        public function get currentMaxConfidence():Number { return _currentMaxConfidence; } 
     567         
     568        public function get currentMaxConfidenceDirection():int { return _currentMaxConfidenceDirection; } 
     569         
     570        public function get currentMaxConfidenceSquare():FLARSquare { return _currentMaxConfidenceSquare; } 
     571}