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>

 

 

 

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

展开阅读全文

没有更多推荐了,返回首页