0
votes

I have been trying to convert a bitmap I've captured from a camera input into a vector to then convert the vectors points into an array for use later.

However I can not find any such functionality within Flash and so I've been trying to use 'Vectorization Package' created by Corey O'Neil. This seems to work but fails to remove the white background on my bitmap (I am capturing a picture of paper with a line on it). Even manually doing this and making the background transparent still yields no results.

Here is my code:

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);
    }

}

Any suggestions?

Thanks.

UPDATE: Sorry for any confusion, basically I would like a way to trace a transparent bitmap, and get an array of the points of the shape in said bitmap. An array of pixel data is simply too large... I'd like 4 points for a rectangle, regardless of it's size...

1
You say you are vectorizing a picture of a paper with a line on it, yet it's not likely that it's color value is #ffffff. Does the library have a tolerance setting ?prototypical
I am converting the image into black and white using these lines: ReduceColors.reduceColors(screenShot, 0, true, false); screenShot.colorTransform(new Rectangle(0, 0, screenShot.width, screenShot.height), new ColorTransform(2,2,2,1) );Craig Wheatley
Have you verified that these two lines results in an image with just black and white ? Not just visually, but have you verified that they color values in the bitmapData are what you expect them to be ?prototypical

1 Answers

0
votes

Apparently you are looking for BitmapData.getVector(). It's available from Flash player 10, so you should have it at your disposal.

EDIT: After I've reread your question, I understand you want your bitmap to be parsed into a vector - and a vector isn't an array of pixels, but instead a start and end of a certain line. Then yes, if you are capable of calling a threshold() so that the line's pixels will be of one color, and the background of another, you then can call getColorBoundsRect() and query all 4 corners of the rectangle. The ones which have the color of the line are your start and end coordinates, store these.