`

actionscript 3.0 图像处理(通道篇)

阅读更多

问题:

1、将图片的R,G,B通道分别分离出来,以RGB模式,灰度模式表示

2、将图片以灰度模式表示

 

源码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import mx.core.UIComponent;
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.BitmapDataChannel;
   private var bm:Bitmap;
            private var bmd:BitmapData;
            
    private function image_complete(evt:Event):void 
    {
                /* Create Bitmap from Image content. */
                bm = img.content as Bitmap;

                /* Create new BitmapData object. */
                bmd = new BitmapData(img.contentWidth, img.contentHeight);

                /* Draw Bitmap into BitmapData. */
                bmd.draw(bm.bitmapData); 
                
                process(imgr,getRedChannelData(bmd)); 
                process(imgg,getGreenChannelData(bmd));
                process(imgb,getBlueChannelData(bmd));
                
                process(gimgr,getRedChannelGrayScaleData(bmd));
                process(gimgg,getGreenChannelGrayScaleData(bmd));
                process(gimgb,getBlueChannelGrayScaleData(bmd));
                
                process(gimg,getGrayscaleData(bmd));                
            }
   
   //用位图数据填充Image控件
   private function process(image:Image,bitmapData:BitmapData):void
   {
             image.graphics.beginBitmapFill(bitmapData);
    image.graphics.drawRect(0,0,bitmapData.width,bitmapData.height);
    image.graphics.endFill();
   }
   
            private function image_mouseMove(evt:MouseEvent):void {
                /* Get the pixel currently under the mouse cursor. */
                //var color:int = bmd.getPixel(evt.localX, evt.localY);

                /* Convert the color value from a number to Hex string. */
                //var colorStr:String = color.toString(16).toUpperCase();

                /* Set the swatch Box instance's backgroundColor style to the color under the mouse cursor. */
                //swatch.setStyle("backgroundColor", color);

                /* Make sure colorStr is at least 6 characters. */
                //colorStr = "000000" + colorStr;

                /* Make sure colorStr is at MOST 6 characters. */
                //lbl.text = "#" + colorStr.substr(colorStr.length - 6);
            }
            
            //获取红色通道中每个像素的值
            private function getRedChannelData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               data.setPixel(i,j,getRed(source.getPixel(i,j)) << 16);
              }
             }
             return data;
            }
            
            //获取红色通道灰度模式下每个像素的值
            private function getRedChannelGrayScaleData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               var red:uint = getRed(source.getPixel(i,j));
               data.setPixel(i,j,mergeRGB(red,red,red));
              }
             }
             return data;
            }
            
            private function getGreenChannelData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               data.setPixel(i,j,getGreen(source.getPixel(i,j)) << 8);
              }
             }
             return data;
            }
            
            private function getGreenChannelGrayScaleData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               var green:uint = getGreen(source.getPixel(i,j));
               data.setPixel(i,j,mergeRGB(green,green,green));
              }
             }
             return data;
            }
            
            private function getBlueChannelData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               data.setPixel(i,j,getBlue(source.getPixel(i,j)));
              }
             }
             return data;
            }
            
            private function getBlueChannelGrayScaleData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               var blue:uint = getBlue(source.getPixel(i,j));
               data.setPixel(i,j,mergeRGB(blue,blue,blue));
              }
             }
             return data;
            }
            
            //获取灰度模式下每个像素的值
            private function getGrayscaleData(source:BitmapData):BitmapData
            {
             var data:BitmapData = new BitmapData(source.width,source.height);
             for(var i:Number = 1;i<source.width;i++)
             {
              for(var j:Number =1 ;j<source.height;j++)
              {
               var pixel:uint = source.getPixel(i,j);
               var r:uint = getRed(pixel);
               var g:uint = getGreen(pixel);
               var b:uint = getBlue(pixel);
               var gray:uint = rgb2gray(r,g,b);            
               data.setPixel(i,j,mergeRGB(gray,gray,gray));
              }
             }
             return data;
            }
            
            //获取红色通道中该像素的值
            private function getRed(rgb:uint):uint
            {
             if(!isHex24(rgb))
             {
              throw new Error("rgb value is not correct!");
             }
             var mask:uint = 0xff0000;
             var red:uint = (rgb & mask) >> 16;
             
             return red;
            }
            
            //当该像素色彩值包含alpha通道的值时,获取红色通道中该像素的值
            private function getRed32(argb:uint):uint
            {
             if(!isHex32(argb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0x00ff0000;
             var red:uint = (argb & mask) >> 16;
             
             return red;
            }
            
            private function getGreen(rgb:uint):uint
            {
             if(!isHex24(rgb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0x00ff00;
             var green:uint = (rgb & mask) >> 8;
             
             return green;
            }
            
            private function getGreen32(argb:uint):uint
            {
             if(!isHex32(argb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0x0000ff00;
             var green:uint = (argb & mask) >> 8;
             
             return green;
            }
            
            private function getBlue(rgb:uint):uint
            {
             if(!isHex24(rgb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0x0000ff;
             var blue:uint = (rgb & mask);
             
             return blue;
            }
            
            private function getBlue32(argb:uint):uint
            {
             if(!isHex32(argb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0x000000ff;
             var blue:uint = (argb & mask);
             
             return blue;
            }
            
            //获取该像素的alpha值
            private function getAlpha(argb:uint):uint
            {
             if(!isHex32(argb))
             {
              throw new Error("rgb value is not correct!");
             }
             
             var mask:uint = 0xff000000;
             var alpha:uint = (argb & mask) >>> 24;
             
             return alpha;
            }
            
            private function mergeRGB(red:uint,green:uint,blue:uint):uint
            {
             return (red<<16)|(green<<8)|blue;
            }
            
            private function mergeARGB(alpha:uint,red:uint,green:uint,blue:uint):uint
            {
             return (alpha << 24)|(red<<16)|(green<<8)|blue;
            }
            
            //判断是否为32位16进制数
            private function isHex32(hex:uint):Boolean
            {
             return hex.toString(16).length == 8;
            }
            //判断是否为24位16进制数
            private function isHex24(hex:uint):Boolean
            {
             return hex.toString(16).length == 6;
            }
            
            //网上摘录的算法,用于将RGB图像转换为灰度图像,按像素转换。
            private function rgb2gray(R:Number,G:Number,B:Number):Number
            {
             var x:Number;
             var y:Number;
             var z:Number;
             var gray:Number;
             
             var r:Number = R/255.0;
             var g:Number = G/255.0;
             var b:Number = B/255.0;
             
             r = Math.pow((r+0.055)/1.055,2.4);
             g = Math.pow((g+0.055)/1.055,2.4);
             b = Math.pow((b+0.055)/1.055,2.4);
             
             y = r*0.222+g*0.717+b*0.061;
             x = y*0.964;
             z = y * 0.825;
             
             gray = 3.134 * x -1.617*y -0.490*z;
             gray = Math.pow(gray,1/2.4)*1.055-0.055;
             gray = gray*255;
             
             //gray = (1-(gray/255))*100;           
             
             return gray;
             
             //return R*0.3+G*0.59+B*0.11;
            }
  ]]>
 </mx:Script>
 <mx:Zoom id="zoom" />
 <mx:VBox id="container" height="100%" width="100%">
  <mx:Label text="origin"/> 
  <mx:Image id="img" source="images/test.gif" completeEffect="{zoom}" complete="image_complete(event);" mouseMove="image_mouseMove(event)"/>
  
  <mx:Label text="channel"/>  
  <mx:HBox>
   <mx:VBox>
    <mx:Label text="red"/>
    <mx:Image id="imgr" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
   
   <mx:VBox>
    <mx:Label text="green"/>
    <mx:Image id="imgg" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
   
   <mx:VBox>
    <mx:Label text="blue"/>
    <mx:Image id="imgb" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
   
  </mx:HBox>
    
  <mx:Label text="grayscale channel"/>
  <mx:HBox>
   <mx:VBox>
    <mx:Label text="red"/>
    <mx:Image id="gimgr" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
   
   <mx:VBox>
    <mx:Label text="green"/>
    <mx:Image id="gimgg" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
   
   <mx:VBox>
    <mx:Label text="blue"/>
    <mx:Image id="gimgb" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
   </mx:VBox>
  </mx:HBox>
  
  <mx:Label text="grayscale mode"/>
  <mx:Image id="gimg" width="{img.width}" height="{img.height}" mouseMove="image_mouseMove(event)"/>
  
  <mx:HBox>
   <mx:Box id="swatch" width="{lbl.height}" height="{lbl.height}"/>
   <mx:Label id="lbl" width="100"/>
  </mx:HBox>
 </mx:VBox> 
</mx:Application>

 

 

 

欢迎关注微信公众号——计算机视觉:

分享到:
评论

相关推荐

    本地播放器(ActionScript 3.0实现).rar

    - 事件驱动:ActionScript 3.0采用事件驱动模型,即通过监听和处理事件来实现程序的交互性。例如,播放、暂停、停止等操作都需要响应用户或系统的特定事件。 - 数据类型:AS3.0支持基本数据类型(如Number、String...

    Flash中利用ActionScript3.0实现图像的扭曲效果

    总之,Flash CS3结合ActionScript 3.0的BitmapData类和DisplacementMapFilter滤镜为设计师提供了一个强大、灵活的图像处理平台,使其可以创造出各种视觉冲击力强的动画效果。即使在当前网络技术的多样化竞争中,掌握...

    tx.rar_图像

    压缩包内的"图像控制"可能包含多个ACTIONSCRIPT文件,每个文件对应一个特定的图像处理功能,如透明度控制的脚本、大小变化的函数或颜色转换的类。通过查看和学习这些源代码,开发者不仅可以掌握基本的图像处理技术,...

    flex_Actionscript3

    处理图像文件,包括加载和操作位图数据。 ##### 4.6 载入或嵌入内容 - **载入内容**:动态地加载资源到舞台上。 - **嵌入内容**:将资源直接嵌入到SWF文件中。 #### 五、基本运动 ##### 5.1 速度 - **向量和速度*...

    as3.0 ColorTransform类改变剪辑的颜色。组件ColorPicker的应用

    总的来说,`ColorTransform`和`ColorPicker`是AS3.0中强大的颜色处理工具,它们在Flash开发中扮演着不可或缺的角色,为用户界面设计和动态内容创建提供了极大的灵活性。通过深入理解和熟练运用这两个概念,开发者...

    [ActionScript.3] 常用英文单词大全20120726

    【ActionScript 3.0 (AS3)】是Adobe Flash平台上的编程语言,它基于ECMAScript的一个方言,主要用于创建互动式网页内容、游戏和动画。以下是一些AS3中的重要概念和常用英文单词的解释: 1. **AAS (ActionScript)**...

    BMPDecoder.rar

    标题"BMPDecoder.rar"指的是一个专门用于处理BMP图像文件的解码类,适用于ActionScript 3.0(AS3)编程环境。BMP(Bitmap)是一种常见的位图格式,广泛用于存储数字图像。在AS3中,处理这种格式的图像通常涉及到二...

    [Flash as3.0动画教程]文字版

    - **AS3.0简介**: ActionScript 3.0 (AS3.0) 是Flash平台的主要脚本语言之一,用于开发交互式内容、游戏和复杂的应用程序。 **2.2 关于ActionScript版本** - **发展**: AS3.0是在AS2.0的基础上进行了重大改进,提高...

    获取舞台某一个点的颜色值

    获取舞台上的某一点的颜色值是编程中的常见任务,这有助于进行像素级的操作,比如图像处理、碰撞检测或者实现特定的视觉效果。本篇文章将深入探讨如何获取舞台某一个点的颜色值,并以十六进制形式表示。 首先,我们...

    MakingThingsMove中文版

    通过对《MakingThingsMove》中文版的内容分析,我们可以看到这本书从基础到高级,系统地介绍了使用ActionScript 3.0进行动画编程的知识体系。无论是对于初学者还是有一定基础的学习者来说,这本书都是一个非常好的...

    gskinner的碰撞检测实现

    一旦创建,我们就可以使用draw()方法将显示对象绘制到BitmapData上,或者直接访问其像素数据来执行各种图像处理任务,比如颜色操作、滤镜应用,当然也包括碰撞检测。 gskinner的碰撞检测实现主要利用了BitmapData的...

    网页动画设计实习报告.pdf

    报告中可能提到了ActionScript 3.0的相关知识,这是Flash版本9之后的主要编程语言,用于更高级的动画控制和交互设计。 3. Alpha通道:在报告中也可能涉及到Alpha通道的应用。Alpha通道用于定义Flash动画中图像的...

    优化Adobe Flash Platform的性能

    - **提高ActionScript 3.0性能**:利用高级编程技巧提高脚本执行效率。 - **加快渲染速度**:优化图形和视频渲染过程,提升用户体验。 - **优化网络交互**:合理安排网络请求,减少延迟和错误。 - **使用音频和视频*...

Global site tag (gtag.js) - Google Analytics