2012-08-22 7 views
0

많은 사용되지 않은 공간이있는 동영상 클립을 비트 맵으로 변환했지만 변환 중에 비트 맵에 많은 흰색 픽셀이 추가되어 배경이 제대로 표시되지 않을 수 있습니다.비트 맵에서 흰색 픽셀 제거

내 질문은 가장 똑똑한 방법을 배경을 제거하는 방법입니다 ... 모든 단일 흰색 픽셀을 제거하고 어떻게 그렇게합니까? 가능한 경우 동영상 클립에서 알파 맵을 생성 하시겠습니까? 또는 내가 알지 못하는 다른 방법은 있습니까?

사전 =)

답변

2

은 다음 코드를 참조하십시오. 이 코드는 픽셀이 흰색 인 경우 투명 색상으로 변환합니다.

for(var i:int = 0; i<myBitmapData.width; i++) 
{ 
    for(var j:int = 0; j<myBitmapData.height; j++) 
    { 
     if(myBitmapData.getPixel(i,j) == 0xffffff) 
     { 
      var transparent:uint = 0x00000000; 
      myBitmapData.setPixel32(i, j, transparent); 
     } 
    } 
} 

다음 코드를 확인했습니다. 테스트를 시작하기 전에 색상을 빨강 또는 파랑 또는 녹색으로 변경해야합니다. 당신의 BitmapData 객체를 생성 알파 당신이의 BitmapData를 반복하고 @의 BitmapData의 대답에서 각 픽셀을 설정하지 않아도이 방법은 0입니다입니다 ARGB 색상으로 채우기를 사용할 때

import flash.display.Sprite; 
import flash.events.Event; 
import flash.display.BitmapData; 
import flash.display.Bitmap; 
import flash.events.MouseEvent; 

var brush:Sprite =new Sprite(); 
brush.graphics.beginFill(0xffffff); 
brush.graphics.drawRect(0,0,100,100); 
brush.graphics.endFill(); 
//addChild(brush); 

var myBitmap:Bitmap = new Bitmap(convertToBitmap(brush)); 
var bmd:BitmapData = myBitmap.bitmapData; 
addChild(myBitmap); 

for(var i:int = 0; i<bmd.width; i++) 
{ 
    for(var j:int = 0; j<bmd.height; j++) 
    { 
     if(bmd.getPixel(i,j) == 0xffffff) 
     { 
      var transparent_color:uint = 0x00000000; 
      bmd.setPixel32(i, j, transparent_color); 
     } 
    } 
} 

function convertToBitmap(clip:DisplayObject):BitmapData 
{ 
    var bounds:Rectangle = clip.getBounds(clip); 
    var bitmap:BitmapData = new BitmapData(int(bounds.width + 0.5), int(bounds.height + 0.5), true, 0); 
    bitmap.draw(clip, new Matrix(1,0,0,1,-bounds.x,-bounds.y)); 
    return bitmap; 
} 
+0

들으, 나는 생각하지만 코딩하는 방법을 몰랐어요 이잖아,하지만 당신은 정말이 가장 빠른/가장 좋은 방법이라고 생각합니까? – tschery

+0

내가 편집 한 게시물. 체크 아웃하십시오. 이'bmd.setPixel32 (...)'를 체크했다. –

1

또 다른 방법은 .

var bitmapData:BitmapData = new BitmapData(100, 100, true, 0x00000000); 

여기이 효과를 보여주는 샘플 응용 프로그램입니다 :

package 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.display.Graphics; 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 

    public class TestAS3 extends Sprite 
    { 
     private var drawingObject:Sprite = new Sprite(); 
     private var bitmap:Bitmap = new Bitmap(); 
     private var backgroundObject:Sprite = new Sprite(); 

     public function TestAS3() 
     { 
      super(); 
      stage.scaleMode=StageScaleMode.NO_SCALE; 
      stage.align=StageAlign.TOP_LEFT; 

      // draw something on the background, so we can verify the 
      // bitmap has transparency 
      var g:Graphics = backgroundObject.graphics; 
      g.beginFill(0xFF0000); 
      g.drawRect(0,0,200,200); 
      g.endFill(); 
      addChild(backgroundObject); 

      // draw a black circle so we can make a bitmap out of it 
      g = drawingObject.graphics; 
      g.beginFill(0); 
      g.drawCircle(100,100,50); 
      g.endFill(); 

      // create the bitmap data 
      // note use an ARGB color (here color is black, alpha is 0) 
      // to default the BitmapData to a transparent fill 
      var bmd:BitmapData = new BitmapData(200,200,true,0x00000000); 
      bmd.draw(drawingObject); 
      bitmap.bitmapData=bmd; 
      addChild(bitmap); 
     } 
    } 
} 
+0

thanks man =) 나는 completly를 이해하지 못했다. 그러나 그것은 유사하게 보인다, 그 다음 분과 함께 놀 것이다! 도와 줘서 고맙다! – tschery

+1

정말 간단합니다 (단 한 줄만), 방금 바보 같은 예를 추가했습니다. BitmapData를 만들 때 기본 값 대신 투명하게 채 웁니다. –