Changeset 17212 for lang/actionscript
- Timestamp:
- 08/07/08 20:54:15 (4 months ago)
- Location:
- lang/actionscript/ashaardetect/trunk/cv
- Files:
-
- 2 added
- 4 modified
-
ColorUtil.as (added)
-
HaarCascade.as (modified) (12 diffs)
-
HaarCascade.mas (modified) (12 diffs)
-
SumImage.as (modified) (4 diffs)
-
U.as (modified) (3 diffs)
-
VideoDetector.as (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/actionscript/ashaardetect/trunk/cv/HaarCascade.as
r17172 r17212 4 4 import flash.display.*; 5 5 import flash.geom.*; 6 public class HaarCascade6 public class HaarCascade 7 7 { 8 8 private var width:int, height:int; … … 14 14 private var mSumW:int; 15 15 private var mSkipOverlap:Boolean = true; 16 private var mYieldContext:DetectContext = new DetectContext(); 16 17 17 18 function HaarCascade() 18 19 { 20 19 21 } 20 22 … … 34 36 35 37 mStages.push(s); 38 } 39 40 public function generateHueSum(himg:BitmapData):void 41 { 42 mYieldContext.Hsumimage = new SumImage(himg, false); 36 43 } 37 44 … … 51 58 } 52 59 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"; 58 61 private static const NPASS:uint = 2; 59 62 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 62 75 var resw:uint, resh:uint; 63 76 var rect:Rectangle = new Rectangle(); 64 77 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; 68 82 var pass:uint = 0; 69 83 … … 75 89 var winWidth:uint; 76 90 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 ) 82 99 { 83 initCascade(factor, sums );84 var ystep:Number = MAX(2,factor);100 initCascade(factor, sums, Hsum); 101 var ystep:Number = ((2> factor) ? 2 : factor); 85 102 winWidth = factor * width + 0.5; 86 103 winHeight = factor * height + 0.5; … … 90 107 91 108 var stop_height:uint = (srcHeight - winHeight) / ystep; 92 93 ++mask_index;94 109 var result:int; 95 110 96 111 var iy:uint = 0, iyp:uint = 0; 97 for(pass = 0; pass < NPASS; pass++)112 for(pass = mYieldContext.pass; pass < NPASS; pass++) 98 113 { 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++) 100 116 { 117 if (--limit == 0) 118 { saveContext(_iy, pass, resrects1, factor, mask_index, sums); return YIELDED; } 119 else 120 mYieldContext._iy = 0; 121 101 122 var _ix:uint, _xstep:uint = 1; 102 iy = _iy*ystep + 0.5;123 iy = int((_iy*ystep) + 0.5); 103 124 iyp = iy * mSumW; 104 125 … … 111 132 { 112 133 _xstep = 2; 134 135 if (Hsum) { 136 if ((Hsum.calc_esum(ix, iyp) / Number(Hsum.eqArea)) < 0.4) 137 continue; 138 } 139 113 140 result = cvRunHaarClassifierCascade(ix, iyp, 0, sums, 2); 114 141 … … 131 158 resrects1 = []; 132 159 resrects1.push([ix, iy, winWidth, winHeight]); 133 160 /* 134 161 if (winWidth > 8) { 135 162 resw = winWidth; … … 140 167 rect.height = resh; 141 168 bmp.fillRect(rect, INVALID_MASK); 142 } 169 }*/ 143 170 } 144 171 else … … 151 178 } 152 179 } 180 if (scale_factor == 0) 181 break; 153 182 } 183 mYieldContext.enabled = false; 184 154 185 return resrects1; 155 186 } 156 187 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 158 200 { 159 201 var equ_rect_width:int = int(((width-2)*scale) + 0.5); … … 161 203 mWeightScale = 1.0/Number(equ_rect_width*equ_rect_height); // cascade->inv_window_area 162 204 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 163 208 // correction_ratio = mWeightScale; 164 209 for each(var stg:ClassifierStage in mStages) … … 272 317 } 273 318 } 319 320 class 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 4 4 import flash.display.*; 5 5 import flash.geom.*; 6 #define cvRound(fval) int((fval) + 0.5) 7 6 8 public class HaarCascade 7 9 { … … 14 16 private var mSumW:int; 15 17 private var mSkipOverlap:Boolean = true; 18 private var mYieldContext:DetectContext = new DetectContext(); 16 19 17 20 function HaarCascade() 18 21 { 22 19 23 } 20 24 … … 34 38 35 39 mStages.push(s); 40 } 41 42 public function generateHueSum(himg:BitmapData):void 43 { 44 mYieldContext.Hsumimage = new SumImage(himg, false); 36 45 } 37 46 … … 51 60 } 52 61 53 private static function MAX(a:Number, b:Number):Number54 { 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"; 58 67 private static const NPASS:uint = 2; 59 68 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 62 81 var resw:uint, resh:uint; 63 82 var rect:Rectangle = new Rectangle(); 64 83 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; 68 88 var pass:uint = 0; 69 89 … … 75 95 var winWidth:uint; 76 96 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 ) 82 105 { 83 initCascade(factor, sums );106 initCascade(factor, sums, Hsum); 84 107 var ystep:Number = MAX(2, factor); 85 108 winWidth = factor * width + 0.5; … … 90 113 91 114 var stop_height:uint = (srcHeight - winHeight) / ystep; 92 93 ++mask_index;94 115 var result:int; 95 116 96 117 var iy:uint = 0, iyp:uint = 0; 97 for(pass = 0; pass < NPASS; pass++)118 for(pass = mYieldContext.pass; pass < NPASS; pass++) 98 119 { 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++) 100 122 { 123 if (--limit == 0) 124 YIELD(_iy, pass, resrects1, factor, mask_index, sums) 125 else 126 mYieldContext._iy = 0; 127 101 128 var _ix:uint, _xstep:uint = 1; 102 iy = _iy*ystep + 0.5;129 iy = cvRound(_iy*ystep); 103 130 iyp = iy * mSumW; 104 131 … … 111 138 { 112 139 _xstep = 2; 140 141 if (Hsum) { 142 if ((Hsum.calc_esum(ix, iyp) / Number(Hsum.eqArea)) < 0.4) 143 continue; 144 } 145 113 146 result = cvRunHaarClassifierCascade(ix, iyp, 0, sums, 2); 114 147 … … 131 164 resrects1 = []; 132 165 resrects1.push([ix, iy, winWidth, winHeight]); 133 166 /* 134 167 if (winWidth > 8) { 135 168 resw = winWidth; … … 140 173 rect.height = resh; 141 174 bmp.fillRect(rect, INVALID_MASK); 142 } 175 }*/ 143 176 } 144 177 else … … 151 184 } 152 185 } 186 if (scale_factor == 0) 187 break; 153 188 } 189 mYieldContext.enabled = false; 190 154 191 return resrects1; 155 192 } 156 193 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 160 206 { 161 207 var equ_rect_width:int = cvRound((width-2)*scale); … … 163 209 mWeightScale = 1.0/Number(equ_rect_width*equ_rect_height); // cascade->inv_window_area 164 210 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 165 214 // correction_ratio = mWeightScale; 166 215 for each(var stg:ClassifierStage in mStages) … … 271 320 } 272 321 } 322 323 class 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 9 9 10 10 private var mEqWinSize:CvSize = new CvSize(); 11 private var mEqArea:int; 11 12 private var mEqX:int; 12 13 private var mEqY:int; … … 18 19 private var mData:Array; 19 20 private var mSQData:Array; 20 function SumImage(b:BitmapData )21 function SumImage(b:BitmapData, make_sq:Boolean = true) 21 22 { 22 23 mWidth = b.width + 1; 23 24 mHeight = b.height + 1; 24 25 25 mData = new Array(mWidth*mHeight); 26 mSQData = new Array(mWidth*mHeight); 26 mData = new Array(mWidth*mHeight); 27 27 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++) 30 33 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); 32 40 } 33 34 sum(b, mData, mSQData, b.width, b.height);41 else 42 sum_nosq(b, mData, b.width, b.height); 35 43 } 36 44 … … 44 52 mEqWinSize.width = ww; 45 53 mEqWinSize.height = wh; 54 mEqArea = ww*wh; 46 55 47 56 mQBase1 = y*mWidth + x; 48 57 mQBase2 = mQBase1 + ww; 49 58 mQBaseH = wh * mWidth; 59 } 60 61 public function get eqArea():int 62 { 63 return mEqArea; 50 64 } 51 65 /* … … 138 152 } 139 153 } 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 140 177 } 141 178 } -
lang/actionscript/ashaardetect/trunk/cv/U.as
r17159 r17212 34 34 } 35 35 36 private static var _rc:Rectangle = new Rectangle(); 37
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)