3
votes

I am working on an app to detect "damage amount" of paper money.
The app will assess the user's input image, and confirm if they have rights to a replacement note.

The output is : if 30% of lost of money object, can allow changing at the bank.

I have idea, we make capture with camera of money and the damage background is white so, in AS3 can count how percent of white color. If less then 30% so is good to change on the Bank.

Any idea (calculation) of how to count percentage of white colour in image?. Thanks.

-- EDIT --

i have the code @VC.One, may you make correction? Thanks.


    import flash.media.Camera;
    import flash.media.Video;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    var camMoney: Camera = Camera.getCamera();
    camMoney.setQuality(0, 100);
    camMoney.setMode(480, 300, 30, true);
    var vidMoney: Video = new Video(480, 300)
    vidMoney.x = 0;
    vidMoney.y = 10;
    addChild(vidMoney);
    vidMoney.attachCamera(camMoney);

    var moneyBD: BitmapData = new BitmapData(vidMoney.width, vidMoney.height);

    var moneyBM: Bitmap = new Bitmap(moneyBD);
    moneyBM.x = 0;
    moneyBM.y = 376;
    addChild(moneyBM);

    var circle: Sprite = new Sprite();
    circle.graphics.lineStyle(2, 0xFFFFFF);
    circle.graphics.beginFill(0x000000);
    circle.graphics.drawCircle(225, 340, 20);
    circle.graphics.endFill();
    addChild(circle);
    circle.buttonMode = true;
    circle.addEventListener(MouseEvent.CLICK, capture);
    function capture(e: MouseEvent): void {
        moneyBD.draw(vidMoney);
    }

    var circleRect: Sprite = new Sprite();
    circleRect.graphics.lineStyle(2, 0xFFFFFF);
    circleRect.graphics.beginFill(0xFF0000);
    circleRect.graphics.drawRoundRect(260, 320, 60, 40, 30);
    circleRect.graphics.endFill();
    addChild(circleRect);
    circleRect.buttonMode = true;
    circleRect.addEventListener(MouseEvent.CLICK, analisa);
    function analisa(e: MouseEvent): void {
        var xi: int;
        var yi: int;
        var pixel: uint;
        var color: uint;
        var pointsWhite: Array = [];
        for (xi = 0; xi = 2) {
            var lengthOfWhiteSpace: int = pointGroup[pointGroup.length - 1].x - pointGroup[0].x;
            var t: TextField = new TextField();
            t.x = pointGroup[0].x;
            t.y = pointGroup[0].y;
            addChild(t);
        }
    }

2
thanks @Jezzamon. i try it now.Asrul
Have you worked with pixels before? In that Bitmpdata API link, you might want to check histogram or even just use getPixel (which tells you colour from a tested pixel position). Use a For loop to scan each pixel and test if it matches white (0xFFFFF) so logic like this : if white == myBMPdata.getPixel( i, j ) then { so add +1 to totalWhitePixels };. When the scan is finished compare your total image pixels vs total white pixels, use math to calculate percentage.VC.One
thanks @VC.One for your edited and the answer. I work now. Thnk you for a briliant solutionAsrul
@VC.One can you correct my AS3:Asrul

2 Answers

2
votes

The percentage formula is : amountFound * 100 / amountTotal

For example if you have $200 put somewhere but only found $50. Doing 50 * 100 / 200 tells that you found 25%. If you found another $50 (now $100), it tells you that you got 50% of your $200.

With this basic formula, you can use it like this for your Analisa function...

function analisa(e:MouseEvent = null) : void 
{
    var xi: int=0; var yi: int=0; var pixel: uint=0;

    var totalPixels_image : int = 0;
    var totalPixels_white : int = 0;
    var PercentWhitePixels : Number = 0;

    //# get total pixels
    totalPixels_image = bmpData.width * bmpData.height;

    //# get total white pixels
    for (yi = 0; yi < bmpData.height; yi++) 
    {
        for (xi = 0; xi < bmpData.width; xi++) 
        {
            pixel = bmpData.getPixel( xi, yi );

            if ( pixel == 0xFFFFFF)
            {
                totalPixels_white += 1; //++;
            }
        }
    }

    PercentWhitePixels = ( (totalPixels_white * 100) / totalPixels_image);

    //# get results 
    trace("=== RESULTS =================================");
    trace("Image Width  : " + bmpData.width );
    trace("Image Height : " + bmpData.height );
    trace("total Pixels in Image  : " + totalPixels_image);
    trace("total Pixels of White  : " + totalPixels_white);
    trace("Percentage of White    : " + PercentWhitePixels) + "%";

}

For me the result for your image was 0.1895.... But from looking with my eyes I think a number around 5% to 10% looks more correct (not this calculated 0.18%). If you multiply result by 100 it will become 18.95% but now that seems too much.

I think an extra or different calculation is needed, I will update when I think of something later but this Answer should help you start..

PS consider checking about :

1) Percentage: Difference vs Error.
2) I think Percentage Difference might help you.

0
votes

The OP, new to SOF, accidentally added additional question info in as an answer. I moved it into an edit under the OP because I do not have rights to delete this "answer".