VB Pixel Counting Program

Discussion in 'OT Technology' started by peterthesmart, Dec 17, 2006.

  1. peterthesmart

    peterthesmart New Member

    Joined:
    Aug 9, 2004
    Messages:
    76
    Likes Received:
    0
    Location:
    Minnesota
    A friend and I are doing a science project it is on the percentage of bacteria growing we grew them in a dish and took a photo of them. We then made the photo so that it is a monochrome photo with black as no bacteria and white as bacteria.

    It is very difficult to find a accrete percentage the manual way so we want to use a computer to help us. We decided to use the pixels of the photo.

    In visual basic I wrote the following program:

    Code:
    opendio.ShowDialog() [COLOR=#007f00]'open file
    [/COLOR]        Label1.Text = opendio.FileName [COLOR=#007f00]'print path on label
    [/COLOR]        Picture.Image = Image.FromFile(opendio.FileName) [COLOR=#007f00]'show image in box
    [/COLOR]        [COLOR=#00007f]Dim[/COLOR] photo [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]New[/COLOR] Bitmap(opendio.FileName) [COLOR=#007f00]'save image as varable
    [/COLOR]
            [COLOR=#00007f]Dim[/COLOR] count [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR] [COLOR=#007f00]' number of white pixels
    [/COLOR]        [COLOR=#00007f]Dim[/COLOR] y [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR] [COLOR=#007f00]'y pixel
    [/COLOR]        [COLOR=#00007f]Dim[/COLOR] x [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR] [COLOR=#007f00]'x pixel
    [/COLOR]        [COLOR=#00007f]Dim[/COLOR] photomaxX [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR] = photo.Size.Width [COLOR=#007f00]'how many pixels on X axes
    [/COLOR]        [COLOR=#00007f]Dim[/COLOR] photomaxY [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR] = photo.Size.Height [COLOR=#007f00]'how many pixels on Y axes
    [/COLOR]        [COLOR=#00007f]For[/COLOR] y = 0 [COLOR=#00007f]To[/COLOR] photomaxY - 1 [COLOR=#007f00]'go though the y axis
    [/COLOR]            [COLOR=#00007f]For[/COLOR] x = 0 [COLOR=#00007f]To[/COLOR] photomaxX - 1 [COLOR=#007f00]'go though the x axis
    [/COLOR]                [COLOR=#00007f]If[/COLOR] photo.GetPixel(x, y).Equals(Color.White) [COLOR=#00007f]Then[/COLOR] [COLOR=#007f00]'if the pixel color is white
    [/COLOR]                    count = count + 1 [COLOR=#007f00]'add one
    [/COLOR]                [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]
                [COLOR=#00007f]Next[/COLOR]
            [COLOR=#00007f]Next[/COLOR]
    
            [COLOR=#00007f]MsgBox[/COLOR](count / (photomaxX * photomaxY)) [COLOR=#007f00]' show percentage in decmal form
    [/COLOR]



    the issue is it always comes up as 0% no matter what picture it is. they are monocrome bitmaps that we are using
     
  2. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    what is the colour depth of the bitmap? depending on your source you might be better off defining a range of colour to accept as white.
     
  3. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Yeah - I would first print out what each pixel is defined as to ensure you have the two values right.
     
  4. Doneranator

    Doneranator New Member

    Joined:
    Nov 16, 2006
    Messages:
    60
    Likes Received:
    0
    it also looks like you loops are set up wrong. They are both increasing by one at the same time. it should go row by row or col by col and count the opposite. ie( x1,y1,,x1,y2,x1,y3......x1,ymax, then x2..... until both are maxed out.) you needa nested loop there. Hope this helps!
     
  5. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    it is nested - it's just formated weird.
     
  6. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    My guess is that you'll find that there will be different shades of white/grey, I'd just check for everything not black (assuming your pixel color value printout says black is really..black).
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Count all pixels with an average brightness < 128 as black, and all pixels with an average brightness >= 128 as white. It will give you the same result as what you were hoping for, minus all the WTFing.
     
  8. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    I've always wondered how to do this in a simple program. Now I know. :eek5:
     

Share This Page