Get dominant or average color in getPixels ByteArray

久未见 提交于 2020-01-07 06:43:51

问题


So, I've got this code:

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

var bmd:BitmapData = new BitmapData(10, 10, true);
var seed:int = int(Math.random() * int.MAX_VALUE);
bmd.noise(seed);

var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
var pixels:ByteArray = bmd.getPixels(bounds);

Is there a way to get, efficiently and quickly, the dominant color and/or the average color in pixels ByteArray.

noise is used for the example here. I'll be having something else drawn on that BitmapData instead.

I found some ways to extract the average color palate from a BitmapData, but this isn't enough for me since I want the average from a rect over that image.

Thanks in advance!


回答1:


The average color is easy, just resize the bitmapData by draw()ing it in to a 1x1 bitmap, flash will average the color in the copy process.

var m: Matrix = new Matrix();
m.scale( 1 / bmd.width, 1 / bmd.height);
var averageColorBmd:BitmapData = new BitmapData(1, 1);
averageColorBmd.draw(bmd, m);
var averageColor:uint = averageColorBmd.getPixel(0,0);

Something like that!




回答2:


FYI this actually does not average or pick the dominant color.

doing it this way is relying on flash to shrink the image down to one pixel and then decide which color to use. I'm not sure how this color is chosen, but through tests it is NOT the average NOR is it the dominant color.



来源:https://stackoverflow.com/questions/2037151/get-dominant-or-average-color-in-getpixels-bytearray

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