问题
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