2013-10-24 29 views
0

我一直在試圖將我從相機輸入中捕獲的位圖轉換爲矢量,然後將矢量點轉換爲數組供稍後使用。將位圖轉換爲as3中的矢量

但是我在Flash中找不到任何這樣的功能,所以我一直在嘗試使用由Corey O'Neil創建的'矢量化包'。這似乎工作,但未能刪除我的位圖上的白色背景(我用一條線捕捉紙張的圖片)。即使手動執行此操作並使背景透明仍然不會產生任何結果。

這裏是我的代碼:

public class DocumentRich extends MovieClip{ 

    public var initBox2D:Main; 
    var cam:Camera = Camera.getCamera(); 
    var vid:Video = new Video(420, 300); 
    var myVector:Vectorize; 
    public function DocumentRich() 
    { 
     if(stage){ 
      initBox2D = new Main(); 
      addChild(initBox2D); 
      //addChild(new movieMonitor()); 
     } 

     addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true); 
     StartAnim(); 


    } 

    function StartAnim():void 
    { 
     //SET UP WORLD 
     initBox2D.isDebugMode(false); 
     initBox2D.mouseDragEnabled(true); 
     Main.world.SetGravity(new b2Vec2(0,0)); 


     cam.setQuality(0, 100); 
     cam.setMode(420, 300, 30, true); 
     trace(cam.height); 
     vid.attachCamera(cam); 
     addChild(vid); 
     addChild(go); 
    } 

    function clickHandler(m:MouseEvent){ 
     trace(m.target.name); 
     switch(m.target.name){ 
      case 'go': 
       goButtonFun(); 
      break; 

     } 
    } 

    function goButtonFun():void{ 
     var screenShot:BitmapData = new BitmapData(cam.width, cam.height); 
     var screenShot_image:Bitmap=new Bitmap(screenShot); 
     screenShot.draw(vid) ; 
     ReduceColors.reduceColors(screenShot, 0, true, false); 
     screenShot.colorTransform(new Rectangle(0, 0, screenShot.width, screenShot.height), new ColorTransform(2,2,2,1)); 

     ///// --------- MAY NOT NEED THIS ----------- //////////// 
     for(var i:int = 0; i<screenShot.width; i++) 
     { 
      for(var j:int = 0; j<screenShot.height; j++) 
      { 
       if(screenShot.getPixel(i,j) == 0xffffff) 
       { 
        var transparent:uint = 0x00000000; 
        screenShot.setPixel32(i, j, transparent); 
       } 
      } 
     } 

     myVector = new Vectorize(screenShot_image, 23, 2, 1, 3, 0xFFFFFF); 
     myVector.addEventListener(Vectorize.INIT, traceStatus); 
     myVector.addEventListener(Vectorize.DESPECKLE_START, traceStatus); 
     myVector.addEventListener(Vectorize.DESPECKLE_COMPLETE, traceStatus); 
     myVector.addEventListener(Vectorize.GROUPING_START, traceStatus); 
     myVector.addEventListener(Vectorize.GROUPING_COMPLETE, traceStatus); 
     myVector.addEventListener(Vectorize.EDGING_START, traceStatus); 
     myVector.addEventListener(Vectorize.EDGING_COMPLETE, traceStatus); 

     myVector.addEventListener(Vectorize.DESPECKLE_PROGRESS, traceProgress); 
     myVector.addEventListener(Vectorize.GROUPING_PROGRESS, traceProgress); 
     myVector.addEventListener(Vectorize.EDGING_PROGRESS, traceProgress); 

     myVector.addEventListener(Vectorize.COMPLETE, showVector); 
     myVector.vectorize(); 

     //addChild(screenShot_image); 
     addChild(go); 
     removeChild(vid); 
     cam = null; 
     vid.attachCamera(null); 
     vid = null; 
    } 

    function traceStatus(event:Event):void 
    { 
     trace(event.type); 
    } 
    function traceProgress(event:Event):void 
    { 
     var progress:int = event.target.percentDone * 100; 
     trace(event.type + ": " + progress + "%"); 
    } 
    function showVector(event:Event):void 
    { 
     trace(event.type); 
     addChild(myVector); 
    } 

} 

有什麼建議?

謝謝。

UPDATE: 對不起任何混亂,基本上我想的方式跟蹤一透明的位圖,並獲取在該形狀的點的陣列,所述位圖。像素數據陣列太大了...我想要一個矩形4點,不管它的大小...

+1

你說你用矢量化一張紙上的圖片,但它的顏色值不太可能是#ffffff。圖書館是否有寬容設置? – prototypical

+0

我將這些圖像轉換爲黑白圖像: 'ReduceColors.reduceColors(screenShot,0,true,false); (新的矩形(0,0,screenShot.width,screenShot.height),新的ColorTransform(2,2,2,1));' –

+0

你是否驗證過這兩行產生的圖像只是黑與白 ?不只是在視覺上,但你有沒有證實他們在bitmapData中的顏色值是你期望他們是什麼? – prototypical

回答

0

顯然您正在尋找BitmapData.getVector()。它可以從Flash播放器10中獲得,所以您應該可以隨時使用它。

編輯:當我重新讀你的問題後,我知道你想要你的位圖被分析成一個向量 - 而且一個向量不是一個像素數組,而是一個特定行的開始和結束。那麼是的,如果您有能力調用threshold(),以便線條的像素將是一種顏色,並且可以調用另一種顏色的背景,則可以調用getColorBoundsRect()並查詢矩形的所有四個角。具有線條顏色的那些是您的起始和結束座標,將這些存儲起來。

+0

但是這需要輸入一個矩形區域參數?我不知道頁面上哪一行會出現這種情況,所以我不能指定矩形的位置...除非可以在沒有參數的情況下使用它,並且忽略透明像素? –

+0

對此進行試驗似乎給出了許多無意義的觀點。該數組中有超過300,000個數字... –

+0

嗯,給它一個矩形意味着你想要導出的圖像的哪一部分作爲矢量。你顯然想用正確的座標來解析圖像中的一條線,以便開始和結束 - 我不確定是否有這樣的功能。如果你的直線是直線,你可以嘗試調用'getColorBoundsRect()',那麼其中一對相反的角度將會爲你提供開始和結束座標。否則,你必須解析一條線,不知何故更棘手。 – Vesper