AS3 - Find the most abundant pixel colour in BitmapData

元气小坏坏 提交于 2019-12-11 05:56:07

问题


Basically, I want to get the most common ARGB value that appears in a BitmapData. That is, I want to know which exact pixel colour is the most abundant in the image. I tried going through every pixel of the image and counting whenever a colour that already exists comes up, but that's way too slow, even with relatively small images. Does anybody know a faster method for this, maybe using the BitmapData.histogram() function or something?

Ideally the process should be near instantaneous for images around at least 1000x1000 pixels.


回答1:


Run through bitmapData.getVector() with a Dictionary to hold numbers, then sort that Dictionary's key-value pairs by value and get the key of maximum.

var v:Vector.<uint>=yourBitmapData.getVector(yourBitmapData.rect);
var d:Dictionary=new Dictionary();
for (var i:int=v.length-1; i>=0;i--) {
    if (d[v[i]]) d[v[i]]++; else d[v[i]]=1;
}
var maxkey:String=v[0].toString();
var maxval:int=0;
for (var k:String in d) {
    if (d[k]>maxval) {
        maxval=d[k];
        maxkey=k;
    }
}
return parseInt(maxkey); // or just maxkey



回答2:


I haven't worked with shaders at all, but I think you might be able to get faster results. Looping through pixels is faster at the shader level.

I'd try by creating essentially the same loop in the shader, and paint the entire resulting bitmap with the most used colour and sample that (unless you can get a variable directly out of the shader)

this should be significantly faster



来源:https://stackoverflow.com/questions/14388164/as3-find-the-most-abundant-pixel-colour-in-bitmapdata

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