dede
dede

Reputation: 805

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)

Upvotes: 3

Views: 1232

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

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;
}

Upvotes: 5

Related Questions