比较两个位图在ActionScript 3 [英] Compare two Bitmaps in ActionScript 3

查看:217
本文介绍了比较两个位图在ActionScript 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类似的BitmapData,我想对它们进行比较,并得​​到多少百分比是他们相似。 (我想我应该使用比较()和的BitmapData的阈值()方法,但不知道怎么做)(或者简单地使用getPixel和比较每个像素的像素,但我不知道这是否是性能好)

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)

推荐答案

下面是一个使用比较和getVector,假设两个位图数据对象是相同的宽度和高度的一个简单的方法:

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

这篇关于比较两个位图在ActionScript 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆