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)
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With