Changeset 17212 for lang/actionscript

Show
Ignore:
Timestamp:
08/07/08 20:54:15 (4 months ago)
Author:
gyuque
Message:

detection from video

Location:
lang/actionscript/ashaardetect/trunk/cv
Files:
2 added
4 modified

Legend:

Unmodified
Added
Removed
  • lang/actionscript/ashaardetect/trunk/cv/HaarCascade.as

    r17172 r17212  
    44        import flash.display.*; 
    55        import flash.geom.*; 
    6         public class HaarCascade 
     6                public class HaarCascade 
    77        { 
    88                private var width:int, height:int; 
     
    1414                private var mSumW:int; 
    1515                private var mSkipOverlap:Boolean = true; 
     16                private var mYieldContext:DetectContext = new DetectContext(); 
    1617 
    1718                function HaarCascade() 
    1819                { 
     20 
    1921                } 
    2022 
     
    3436 
    3537                        mStages.push(s); 
     38                } 
     39 
     40                public function generateHueSum(himg:BitmapData):void 
     41                { 
     42                        mYieldContext.Hsumimage = new SumImage(himg, false); 
    3643                } 
    3744 
     
    5158                } 
    5259 
    53                 private static function MAX(a:Number, b:Number):Number 
    54                 { 
    55                         return (a>b) ? a : b; 
    56                 } 
    57  
     60                                                public static const YIELDED:String = "YIELDED"; 
    5861                private static const NPASS:uint = 2; 
    5962                private static const INVALID_MASK:uint = 0xffffff; 
    60                 public function detect(bmp:BitmapData, scale_factor:Number, min_width:int = 0, min_height:int = 0):Array 
    61                 { 
     63                public function detect(bmp:BitmapData, scale_factor:Number, min_width:int = 0, min_height:int = 0, limit:int = -1):Object 
     64                { 
     65                        if (!mYieldContext.enabled) { 
     66                                mYieldContext._iy  = 0; 
     67                                mYieldContext.pass = 0; 
     68                                mYieldContext.res  = null; 
     69                                mYieldContext.factor = 1; 
     70                                mYieldContext.sumimage = new SumImage(bmp); 
     71                                mYieldContext.Hsumimage = null; 
     72                                mYieldContext.mask_index = 1; 
     73                        } 
     74 
    6275                        var resw:uint, resh:uint; 
    6376                        var rect:Rectangle = new Rectangle(); 
    6477 
    65                         var resrects1:Array = null; 
    66                         var sums:SumImage = new SumImage(bmp); 
    67                         var factor:Number = 1; 
     78                        var resrects1:Array = mYieldContext.res; 
     79                        var sums:SumImage = mYieldContext.sumimage; 
     80                        var Hsum:SumImage = mYieldContext.Hsumimage; 
     81                        var factor:Number = mYieldContext.factor; 
    6882                        var pass:uint = 0; 
    6983 
     
    7589                        var winWidth:uint; 
    7690                        var winHeight:uint; 
    77                         var mask_index:uint = 0; 
    78  
    79                         for( factor = 1; factor*Number(width)  < (srcWidth  - 10) && 
    80                                                          factor*Number(height) < (srcHeight - 10); 
    81                                                                 factor *= scale_factor ) 
     91                        var mask_index:uint = mYieldContext.mask_index; 
     92 
     93                        if (scale_factor == 0) 
     94                                factor = Number(min_width) / Number(width); 
     95 
     96                        for( ; factor*Number(width)  < (srcWidth  - 10) && 
     97                                 factor*Number(height) < (srcHeight - 10); 
     98                                factor *= scale_factor, ++mask_index ) 
    8299                        { 
    83                                 initCascade(factor, sums); 
    84                                 var ystep:Number = MAX(2, factor); 
     100                                initCascade(factor, sums, Hsum); 
     101                                var ystep:Number = ((2> factor) ? 2 : factor); 
    85102                                winWidth  = factor * width  + 0.5; 
    86103                                winHeight = factor * height + 0.5; 
     
    90107 
    91108                                var stop_height:uint = (srcHeight - winHeight) / ystep; 
    92  
    93                                 ++mask_index; 
    94109                                var result:int; 
    95110 
    96111                                var iy:uint = 0, iyp:uint = 0; 
    97                                 for(pass = 0; pass < NPASS; pass++) 
     112                                for(pass = mYieldContext.pass; pass < NPASS; pass++) 
    98113                                { 
    99                                         for(var _iy:uint = 0; _iy < stop_height; _iy++) 
     114                                        mYieldContext.pass = 0; 
     115                                        for(var _iy:uint = mYieldContext._iy; _iy < stop_height; _iy++) 
    100116                                        { 
     117                                                if (--limit == 0) 
     118                                                        { saveContext(_iy,  pass,  resrects1,  factor,  mask_index,  sums); return YIELDED; } 
     119                                                else 
     120                                                        mYieldContext._iy = 0; 
     121 
    101122                                                var _ix:uint, _xstep:uint = 1; 
    102                                                 iy = _iy*ystep + 0.5; 
     123                                                iy = int((_iy*ystep) + 0.5); 
    103124                                                iyp = iy * mSumW; 
    104125 
     
    111132                                                        { 
    112133                                                                _xstep = 2; 
     134 
     135                                                                if (Hsum) { 
     136                                                                        if ((Hsum.calc_esum(ix, iyp) / Number(Hsum.eqArea)) < 0.4) 
     137                                                                                continue; 
     138                                                                } 
     139 
    113140                                                                result = cvRunHaarClassifierCascade(ix, iyp, 0, sums, 2); 
    114141 
     
    131158                                                                                resrects1 = []; 
    132159                                                                        resrects1.push([ix, iy, winWidth, winHeight]); 
    133  
     160/* 
    134161                                                                        if (winWidth > 8) { 
    135162                                                                                resw = winWidth; 
     
    140167                                                                                rect.height = resh; 
    141168                                                                                bmp.fillRect(rect, INVALID_MASK); 
    142                                                                         } 
     169                                                                        }*/ 
    143170                                                                } 
    144171                                                                else 
     
    151178                                        } 
    152179                                } 
     180                                if (scale_factor == 0) 
     181                                        break; 
    153182                        } 
     183                        mYieldContext.enabled = false; 
     184 
    154185                        return resrects1; 
    155186                } 
    156187 
    157                                 private function initCascade(scale:Number, sums:SumImage):void 
     188                private function saveContext(_iy:uint, pass:uint, res:Array, factor:Number, mask_index:uint, sums:SumImage):void 
     189                { 
     190                        mYieldContext.enabled = true; 
     191                        mYieldContext._iy = _iy; 
     192                        mYieldContext.pass = pass; 
     193                        mYieldContext.res  = res; 
     194                        mYieldContext.factor = factor; 
     195                        mYieldContext.mask_index = mask_index; 
     196                        mYieldContext.sumimage = sums; 
     197                } 
     198 
     199                private function initCascade(scale:Number, sums:SumImage, Hsum:SumImage):void 
    158200                { 
    159201                        var equ_rect_width:int  = int(((width-2)*scale) + 0.5); 
     
    161203                        mWeightScale = 1.0/Number(equ_rect_width*equ_rect_height); // cascade->inv_window_area 
    162204                        sums.setEqWindowRect(int((scale) + 0.5), int((scale) + 0.5), equ_rect_width, equ_rect_height); 
     205                        if (Hsum) 
     206                                Hsum.setEqWindowRect(int((scale) + 0.5), int((scale) + 0.5), equ_rect_width, equ_rect_height); 
     207 
    163208//                      correction_ratio = mWeightScale; 
    164209                        for each(var stg:ClassifierStage in mStages) 
     
    272317        } 
    273318} 
     319 
     320class DetectContext 
     321{ 
     322        import cv.*; 
     323        public var enabled:Boolean = false; 
     324        public var _iy:Number; 
     325        public var factor:Number; 
     326        public var pass:uint; 
     327        public var res:Array; 
     328        public var mask_index:uint; 
     329        public var sumimage:SumImage; 
     330        public var Hsumimage:SumImage; 
     331} 
  • lang/actionscript/ashaardetect/trunk/cv/HaarCascade.mas

    r17172 r17212  
    44        import flash.display.*; 
    55        import flash.geom.*; 
     6        #define cvRound(fval) int((fval) + 0.5) 
     7 
    68        public class HaarCascade 
    79        { 
     
    1416                private var mSumW:int; 
    1517                private var mSkipOverlap:Boolean = true; 
     18                private var mYieldContext:DetectContext = new DetectContext(); 
    1619 
    1720                function HaarCascade() 
    1821                { 
     22 
    1923                } 
    2024 
     
    3438 
    3539                        mStages.push(s); 
     40                } 
     41 
     42                public function generateHueSum(himg:BitmapData):void 
     43                { 
     44                        mYieldContext.Hsumimage = new SumImage(himg, false); 
    3645                } 
    3746 
     
    5160                } 
    5261 
    53                 private static function MAX(a:Number, b:Number):Number 
    54                 { 
    55                         return (a>b) ? a : b; 
    56                 } 
    57  
     62                #define MAX(a, b) ((a>b) ? a : b) 
     63 
     64                #define YIELD(_con_iy, _con_pass, _con_res, _con_factor, _con_maskindex, _con_sum) { saveContext(_con_iy, _con_pass, _con_res, _con_factor, _con_maskindex, _con_sum); return YIELDED; } 
     65 
     66                public static const YIELDED:String = "YIELDED"; 
    5867                private static const NPASS:uint = 2; 
    5968                private static const INVALID_MASK:uint = 0xffffff; 
    60                 public function detect(bmp:BitmapData, scale_factor:Number, min_width:int = 0, min_height:int = 0):Array 
    61                 { 
     69                public function detect(bmp:BitmapData, scale_factor:Number, min_width:int = 0, min_height:int = 0, limit:int = -1):Object 
     70                { 
     71                        if (!mYieldContext.enabled) { 
     72                                mYieldContext._iy  = 0; 
     73                                mYieldContext.pass = 0; 
     74                                mYieldContext.res  = null; 
     75                                mYieldContext.factor = 1; 
     76                                mYieldContext.sumimage = new SumImage(bmp); 
     77                                mYieldContext.Hsumimage = null; 
     78                                mYieldContext.mask_index = 1; 
     79                        } 
     80 
    6281                        var resw:uint, resh:uint; 
    6382                        var rect:Rectangle = new Rectangle(); 
    6483 
    65                         var resrects1:Array = null; 
    66                         var sums:SumImage = new SumImage(bmp); 
    67                         var factor:Number = 1; 
     84                        var resrects1:Array = mYieldContext.res; 
     85                        var sums:SumImage = mYieldContext.sumimage; 
     86                        var Hsum:SumImage = mYieldContext.Hsumimage; 
     87                        var factor:Number = mYieldContext.factor; 
    6888                        var pass:uint = 0; 
    6989 
     
    7595                        var winWidth:uint; 
    7696                        var winHeight:uint; 
    77                         var mask_index:uint = 0; 
    78  
    79                         for( factor = 1; factor*Number(width)  < (srcWidth  - 10) && 
    80                                                          factor*Number(height) < (srcHeight - 10); 
    81                                                                 factor *= scale_factor ) 
     97                        var mask_index:uint = mYieldContext.mask_index; 
     98 
     99                        if (scale_factor == 0) 
     100                                factor = Number(min_width) / Number(width); 
     101 
     102                        for( ; factor*Number(width)  < (srcWidth  - 10) && 
     103                                 factor*Number(height) < (srcHeight - 10); 
     104                                factor *= scale_factor, ++mask_index ) 
    82105                        { 
    83                                 initCascade(factor, sums); 
     106                                initCascade(factor, sums, Hsum); 
    84107                                var ystep:Number = MAX(2, factor); 
    85108                                winWidth  = factor * width  + 0.5; 
     
    90113 
    91114                                var stop_height:uint = (srcHeight - winHeight) / ystep; 
    92  
    93                                 ++mask_index; 
    94115                                var result:int; 
    95116 
    96117                                var iy:uint = 0, iyp:uint = 0; 
    97                                 for(pass = 0; pass < NPASS; pass++) 
     118                                for(pass = mYieldContext.pass; pass < NPASS; pass++) 
    98119                                { 
    99                                         for(var _iy:uint = 0; _iy < stop_height; _iy++) 
     120                                        mYieldContext.pass = 0; 
     121                                        for(var _iy:uint = mYieldContext._iy; _iy < stop_height; _iy++) 
    100122                                        { 
     123                                                if (--limit == 0) 
     124                                                        YIELD(_iy, pass, resrects1, factor, mask_index, sums) 
     125                                                else 
     126                                                        mYieldContext._iy = 0; 
     127 
    101128                                                var _ix:uint, _xstep:uint = 1; 
    102                                                 iy = _iy*ystep + 0.5; 
     129                                                iy = cvRound(_iy*ystep); 
    103130                                                iyp = iy * mSumW; 
    104131 
     
    111138                                                        { 
    112139                                                                _xstep = 2; 
     140 
     141                                                                if (Hsum) { 
     142                                                                        if ((Hsum.calc_esum(ix, iyp) / Number(Hsum.eqArea)) < 0.4) 
     143                                                                                continue; 
     144                                                                } 
     145 
    113146                                                                result = cvRunHaarClassifierCascade(ix, iyp, 0, sums, 2); 
    114147 
     
    131164                                                                                resrects1 = []; 
    132165                                                                        resrects1.push([ix, iy, winWidth, winHeight]); 
    133  
     166/* 
    134167                                                                        if (winWidth > 8) { 
    135168                                                                                resw = winWidth; 
     
    140173                                                                                rect.height = resh; 
    141174                                                                                bmp.fillRect(rect, INVALID_MASK); 
    142                                                                         } 
     175                                                                        }*/ 
    143176                                                                } 
    144177                                                                else 
     
    151184                                        } 
    152185                                } 
     186                                if (scale_factor == 0) 
     187                                        break; 
    153188                        } 
     189                        mYieldContext.enabled = false; 
     190 
    154191                        return resrects1; 
    155192                } 
    156193 
    157                 #define cvRound(fval) int((fval) + 0.5) 
    158  
    159                 private function initCascade(scale:Number, sums:SumImage):void 
     194                private function saveContext(_iy:uint, pass:uint, res:Array, factor:Number, mask_index:uint, sums:SumImage):void 
     195                { 
     196                        mYieldContext.enabled = true; 
     197                        mYieldContext._iy = _iy; 
     198                        mYieldContext.pass = pass; 
     199                        mYieldContext.res  = res; 
     200                        mYieldContext.factor = factor; 
     201                        mYieldContext.mask_index = mask_index; 
     202                        mYieldContext.sumimage = sums; 
     203                } 
     204 
     205                private function initCascade(scale:Number, sums:SumImage, Hsum:SumImage):void 
    160206                { 
    161207                        var equ_rect_width:int  = cvRound((width-2)*scale); 
     
    163209                        mWeightScale = 1.0/Number(equ_rect_width*equ_rect_height); // cascade->inv_window_area 
    164210                        sums.setEqWindowRect(cvRound(scale), cvRound(scale), equ_rect_width, equ_rect_height); 
     211                        if (Hsum) 
     212                                Hsum.setEqWindowRect(cvRound(scale), cvRound(scale), equ_rect_width, equ_rect_height); 
     213 
    165214//                      correction_ratio = mWeightScale; 
    166215                        for each(var stg:ClassifierStage in mStages) 
     
    271320        } 
    272321} 
     322 
     323class DetectContext 
     324{ 
     325        import cv.*; 
     326        public var enabled:Boolean = false; 
     327        public var _iy:Number; 
     328        public var factor:Number; 
     329        public var pass:uint; 
     330        public var res:Array; 
     331        public var mask_index:uint; 
     332        public var sumimage:SumImage; 
     333        public var Hsumimage:SumImage; 
     334} 
  • lang/actionscript/ashaardetect/trunk/cv/SumImage.as

    r17159 r17212  
    99 
    1010                private var mEqWinSize:CvSize = new CvSize(); 
     11                private var mEqArea:int; 
    1112                private var mEqX:int; 
    1213                private var mEqY:int; 
     
    1819                private var mData:Array; 
    1920                private var mSQData:Array; 
    20                 function SumImage(b:BitmapData) 
     21                function SumImage(b:BitmapData, make_sq:Boolean = true) 
    2122                { 
    2223                        mWidth  = b.width  + 1; 
    2324                        mHeight = b.height + 1; 
    2425 
    25                         mData        = new Array(mWidth*mHeight); 
    26                         mSQData      = new Array(mWidth*mHeight); 
     26                        mData = new Array(mWidth*mHeight); 
    2727 
    28                         for (var i:int = 0;i < mWidth;i++) 
    29                         { 
     28                        if (make_sq) 
     29                                mSQData = new Array(mWidth*mHeight); 
     30 
     31                        var i:int; 
     32                        for (i = 0;i < mWidth;i++) 
    3033                                mData[i] = 0; 
    31                                 mSQData[i] = 0; 
     34 
     35                        if (make_sq) { 
     36                                for (i = 0;i < mWidth;i++) 
     37                                        mSQData[i] = 0; 
     38         
     39                                sum(b, mData, mSQData, b.width, b.height); 
    3240                        } 
    33  
    34                         sum(b, mData, mSQData, b.width, b.height); 
     41                        else 
     42                                sum_nosq(b, mData, b.width, b.height); 
    3543                } 
    3644 
     
    4452                        mEqWinSize.width  = ww; 
    4553                        mEqWinSize.height = wh; 
     54                        mEqArea = ww*wh; 
    4655 
    4756                        mQBase1 = y*mWidth + x; 
    4857                        mQBase2 = mQBase1 + ww; 
    4958                        mQBaseH = wh * mWidth; 
     59                } 
     60 
     61                public function get eqArea():int 
     62                { 
     63                        return mEqArea; 
    5064                } 
    5165/* 
     
    138152                        } 
    139153                } 
     154 
     155                private function sum_nosq(src:BitmapData, ba:Array, swidth:int, sheight:int):void 
     156                { 
     157                        var x:int, y:int; 
     158                        var s:uint; 
     159                        var it:uint, t:uint; 
     160 
     161                        var curpos:int = mWidth; 
     162                        for (y = 0;y < sheight;y++) 
     163                        { 
     164                                ba[curpos++] = 0; 
     165 
     166                                s = 0; 
     167                                for (x = 0;x < swidth;x++) 
     168                                { 
     169                                        t = it = src.getPixel(x, y) & 0x0000ff; 
     170                                        s += t; 
     171                                        t  = ba[curpos - mWidth]  + s; 
     172                                        ba[curpos++] = t; 
     173                                } 
     174                        } 
     175                } 
     176 
    140177        } 
    141178} 
  • lang/actionscript/ashaardetect/trunk/cv/U.as

    r17159 r17212  
    3434                } 
    3535 
     36                private static var _rc:Rectangle = new Rectangle(); 
     37