| 1 |
// |
|---|
| 2 |
// Project Marilena |
|---|
| 3 |
// Object Detection in Actionscript3 |
|---|
| 4 |
// based on OpenCV (Open Computer Vision Library) Object Detection |
|---|
| 5 |
// |
|---|
| 6 |
// Copyright (C) 2008, Masakazu OHTSUKA (mash), all rights reserved. |
|---|
| 7 |
// contact o.masakazu(at)gmail.com |
|---|
| 8 |
// |
|---|
| 9 |
// Redistribution and use in source and binary forms, with or without modification, |
|---|
| 10 |
// are permitted provided that the following conditions are met: |
|---|
| 11 |
// |
|---|
| 12 |
// * Redistribution's of source code must retain the above copyright notice, |
|---|
| 13 |
// this list of conditions and the following disclaimer. |
|---|
| 14 |
// |
|---|
| 15 |
// * Redistribution's in binary form must reproduce the above copyright notice, |
|---|
| 16 |
// this list of conditions and the following disclaimer in the documentation |
|---|
| 17 |
// and/or other materials provided with the distribution. |
|---|
| 18 |
// |
|---|
| 19 |
// This software is provided by the copyright holders and contributors "as is" and |
|---|
| 20 |
// any express or implied warranties, including, but not limited to, the implied |
|---|
| 21 |
// warranties of merchantability and fitness for a particular purpose are disclaimed. |
|---|
| 22 |
// In no event shall the Intel Corporation or contributors be liable for any direct, |
|---|
| 23 |
// indirect, incidental, special, exemplary, or consequential damages |
|---|
| 24 |
// (including, but not limited to, procurement of substitute goods or services; |
|---|
| 25 |
// loss of use, data, or profits; or business interruption) however caused |
|---|
| 26 |
// and on any theory of liability, whether in contract, strict liability, |
|---|
| 27 |
// or tort (including negligence or otherwise) arising in any way out of |
|---|
| 28 |
// the use of this software, even if advised of the possibility of such damage. |
|---|
| 29 |
// |
|---|
| 30 |
package jp.maaash.ObjectDetection |
|---|
| 31 |
{ |
|---|
| 32 |
import flash.display.BitmapData; |
|---|
| 33 |
|
|---|
| 34 |
public class TargetImage{ |
|---|
| 35 |
private var debug :Boolean; |
|---|
| 36 |
public var bd :BitmapData; |
|---|
| 37 |
public var _ii :Array; // IntegralImage |
|---|
| 38 |
public var _ii2 :Array; // IntegralImage of squared pixels |
|---|
| 39 |
public var iiw :int; |
|---|
| 40 |
public var iih :int; |
|---|
| 41 |
|
|---|
| 42 |
public function TargetImage( d :Boolean = true ){ |
|---|
| 43 |
debug = d; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
public function set bitmapData(b:BitmapData):void{ |
|---|
| 47 |
bd = b; |
|---|
| 48 |
|
|---|
| 49 |
if( (b.width+1)!=iiw || (b.height+1)!=iih ){ |
|---|
| 50 |
_ii = new Array; |
|---|
| 51 |
_ii2 = new Array; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
// build IntegralImages |
|---|
| 55 |
// IntegralImage is 1 size larger than image |
|---|
| 56 |
// all 0 for the 1st row,column |
|---|
| 57 |
iiw = bd.width +1; |
|---|
| 58 |
iih = bd.height+1; |
|---|
| 59 |
var singleII :Number = 0; |
|---|
| 60 |
var singleII2 :Number = 0; |
|---|
| 61 |
for( var j:int=0; j<iih; j++ ){ |
|---|
| 62 |
for( var i:int=0; i<iiw; i++ ){ |
|---|
| 63 |
if( i==0 || j==0 ){ |
|---|
| 64 |
//_ii.push(0); |
|---|
| 65 |
//_ii2.push(0); |
|---|
| 66 |
_ii[ j*iiw+i ] = 0; |
|---|
| 67 |
_ii2[ j*iiw+i ] = 0; |
|---|
| 68 |
continue; |
|---|
| 69 |
} |
|---|
| 70 |
var pix :Number = bd.getPixel(i-1,j-1)>>16; |
|---|
| 71 |
singleII = _ii[iiw*(j-1)+i] + _ii[iiw*j+i-1] + pix - _ii[iiw*(j-1)+i-1]; |
|---|
| 72 |
singleII2 = _ii2[iiw*(j-1)+i] + _ii2[iiw*j+i-1] + pix*pix - _ii2[iiw*(j-1)+i-1]; |
|---|
| 73 |
//_ii.push(singleII); |
|---|
| 74 |
//_ii2.push(singleII2); |
|---|
| 75 |
_ii[ j*iiw+i ] = singleII; |
|---|
| 76 |
_ii2[ j*iiw+i ] = singleII2; |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
public function getSum(x:int,y:int,w:int,h:int):Number{ |
|---|
| 82 |
var y_iiw :Number = y * iiw; |
|---|
| 83 |
var yh_iiw :Number = (y+h) * iiw; |
|---|
| 84 |
return _ii[y_iiw + x ] + |
|---|
| 85 |
_ii[yh_iiw + x + w] - |
|---|
| 86 |
_ii[yh_iiw + x ] - |
|---|
| 87 |
_ii[y_iiw + x + w]; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
// sum of squared pixel |
|---|
| 91 |
public function getSum2(x:int,y:int,w:int,h:int):Number{ |
|---|
| 92 |
var y_iiw :Number = y * iiw; |
|---|
| 93 |
var yh_iiw :Number = (y+h) * iiw; |
|---|
| 94 |
return _ii2[y_iiw + x ] + |
|---|
| 95 |
_ii2[yh_iiw + x + w] - |
|---|
| 96 |
_ii2[yh_iiw + x ] - |
|---|
| 97 |
_ii2[y_iiw + x + w]; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
public function getII(x:int,y:int):Number{ |
|---|
| 101 |
return _ii[y*iiw+x]; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
public function getII2(x:int,y:int):Number{ |
|---|
| 105 |
return _ii2[y*iiw+x]; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
public function get width():int{ |
|---|
| 109 |
return bd.width; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
public function get height():int{ |
|---|
| 113 |
return bd.height; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
private function logger(... args):void{ |
|---|
| 117 |
if(!debug){ return; } |
|---|
| 118 |
log(["[TargetImage]"+args.shift()].concat(args)); |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|