compare two images and check equality

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 16:03:31

问题


do you know any source or info about comparing images within as3/flash?

I want to compare two images and check if the images are the same or not.

Check this example: http://imageshack.us/photo/my-images/825/imagecompare.jpg/

Any clues? Thank you in advance!


回答1:


In addition to the duplicate answers,

I believe you can also use BitmapData.compare()

An example taken from the link, consider the following two BitmapData objects:

 var bmd1:BitmapData = new BitmapData(50, 50, true, 0xFFFF8800);
 var bmd2:BitmapData = new BitmapData(50, 50, true, 0xCCCC6600);
 var diffBmpData:BitmapData = bmd1.compare(bmd2) as BitmapData;
 trace ("0x" + diffBmpData.getPixel(0,0).toString(16); // 0x332200

Code Sample (for Percentage Difference) :

Don't how correct the results are, this is what I brewed up for a percentage :

var bmd1:BitmapData = new BitmapData(225, 225);
bmd1.draw(mc1);
var bmd2:BitmapData = new BitmapData(225, 225);
bmd2.draw(mc2);

var diffBmpData:BitmapData = bmd1.compare(bmd2) as BitmapData;

var diff:int = 0;
var total:int = 225 * 225;

for(var i=0; i<225; i++)
    for(var j=0; j<225; j++)
        diff += (diffBmpData.getPixel(i,j) != 0)? 1 : 0; 

info.text = Math.round((diff / total * 100)).toString();

where : info is a TextBox, mc1 & mc2 are two movieclips on stage.

I think you can make it better by comparing individual values (i.e how much different a pixel is) rather than a boolean is-pixel-similar match.


Result: (White space around the round image would be included)




回答2:


Using BitmapData.compare() will return 0 if the pixel values are identical.

trace(bmd1.compare(bmd2)); // 0



回答3:


In addition to loxxy & xLite's answers, if you need to know how to get the BitmapData from an image file:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

loader.load( new URLRequest ( "http://www.fake.url.path/image.jpg" ) );

function onLoaded(e:Event):void {
    var bmp:Bitmap = loader.content as Bitmap;
    var bitmapData:BitmapData = bmp.data; 
    //bitmapData.Compare(...)
}

Also see loading image by Loader.loadBytes(byteArray)



来源:https://stackoverflow.com/questions/13401773/compare-two-images-and-check-equality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!