Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Bitmaps in ActionScript 3

I have two similar BitmapData and I want to compare them and get what percentage are they similar. (I suppose i should use compare() and threshold() method of BitmapData but don't know how) (or maybe simply use getPixel and compare pixel per pixel but I don't know if it is good for performance)

like image 226
dede Avatar asked Jan 28 '26 11:01

dede


1 Answers

Here is a simple approach using compare and getVector, assuming the two bitmap data objects are the same width and height :

var percentDifference:Number = getBitmapDifference(bitmapData1, bitmapData2);

private function getBitmapDifference(bitmapData1:BitmapData, bitmapData2:BitmapData):Number 
{
    var bmpDataDif:BitmapData = bitmapData1.compare(bitmapData2) as BitmapData;
    if(!bmpDataDif)
        return 0;
    var differentPixelCount:int = 0;

    var pixelVector:Vector.<uint> =  bmpDataDif.getVector(bmpDataDif.rect);
    var pixelCount:int = pixelVector.length;

    for (var i:int = 0; i < pixelCount; i++) 
    {
        if (pixelVector[i] != 0)
            differentPixelCount ++;
    }

    return (differentPixelCount / pixelCount)*100;
}
like image 92
Barış Uşaklı Avatar answered Jan 31 '26 04:01

Barış Uşaklı