色阶为位图的颜色分布的图。做过图形处理的应该很熟悉。
比如下图:上面为原图,下面为色阶。
得到方法:
- 灰色化。把各个通道求平均。用ColorMatrixFilter即可。
- 用threshold来得到颜色分布。threshold的返回值为匹配的像素的个数。
代码:
package {
import flash.display.*;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
public class TestBitmap extends Sprite {
[Embed(source="023.jpg")]
private var SampleImage:Class;
public function TestBitmap() {
var bmd:BitmapData = Bitmap(addChild(new SampleImage())).bitmapData;
var s:Sprite = new Sprite();
createHistogram(bmd,s);
addChild(s).y = bmd.height + 10;
}
//生成色阶
private function createHistogram(bmd:BitmapData, s:Sprite):void {
//灰度画
var cmf:ColorMatrixFilter = new ColorMatrixFilter(
[1 / 3, 1 / 3, 1 / 3, 0, 0,
1 / 3, 1 / 3, 1 / 3, 0, 0,
1 / 3, 1 / 3, 1 / 3, 0, 0]
);
var bmd2:BitmapData = bmd.clone();
bmd2.applyFilter(bmd2, bmd2.rect, new Point(), cmf);
//用threshold来得到颜色分布
var values:Array = [];
for(var i:int = 0; i < 0x256; i++) {
values[i] = bmd2.threshold(bmd2, bmd2.rect, new Point(), "==", i, 0, 0xff, false);
}
bmd2.dispose();
//画色阶
var max:int = bmd.width * bmd.height / 50;
s.graphics.lineStyle(1);
for(i = 0; i < 256; i++) {
s.graphics.moveTo(i, 100);
s.graphics.lineTo(i, Math.max(0, 100 - values[i] / max * 100));
}
}
}
}

![[Google]](http://www.google.com/logos/Logo_25wht.gif)
![[Synchronous Space]](http://weihe924stephen.googlepages.com/syncspace-banner.jpg)
![[Creative Commons License]](http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png)
![[Add to Google]](http://buttons.googlesyndication.com/fusion/add.gif)
![[Aggregated by MXNA]](http://weblogs.macromedia.com/mxna/images/mxna88x31.gif)
![[Flex.org]](http://www.flex.org/images/flexorg.gif)
![Validate my Atom 1.0 feed [Valid Atom 1.0]](http://validator.w3.org/feed/images/valid-atom.png)
![Validate my RSS feed [Valid RSS]](http://resource.googlecode.com/files/valid-rss.png)
![[I heart FeedBurner]](http://www.feedburner.com/fb/images/pub/i_heart_fb.gif)
没有评论:
发表评论