Results 1 to 16 of 16

Thread: Colors [Solved]

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Resolved Colors [Solved]

    Hello, 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 fallowing program

    VB Code:
    1. opendio.ShowDialog() 'open file
    2.         Label1.Text = opendio.FileName 'print path on label
    3.         Picture.Image = Image.FromFile(opendio.FileName) 'show image in box
    4.         Dim photo As New Bitmap(opendio.FileName) 'save image as varable
    5.  
    6.         Dim count As Integer ' number of white pixels
    7.         Dim y As Integer 'y pixel
    8.         Dim x As Integer 'x pixel
    9.         Dim photomaxX As Integer = photo.Size.Width 'how many pixels on X axes
    10.         Dim photomaxY As Integer = photo.Size.Height 'how many pixels on Y axes
    11.         For y = 0 To photomaxY - 1 'go though the y axis
    12.             For x = 0 To photomaxX - 1 'go though the x axis
    13.                 If photo.GetPixel(x, y).Equals(Color.White) Then 'if the pixel color is white
    14.                     count = count + 1 'add one
    15.                 End If
    16.             Next
    17.         Next
    18.  
    19.         MsgBox(count / (photomaxX * photomaxY)) ' show percentage in decmal form

    the issue is it always comes up as 0% no matter what picture it is. they are monocrome bitmaps that we are using
    Last edited by tonyrueb; Dec 18th, 2006 at 12:05 AM. Reason: Solved -- Thanks!

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Colors

    You should tweak the comparison a bit more. I found at least 2 ways to do it but most certainly there are more.
    VB Code:
    1. If photo.GetPixel(x, y).ToArgb.Equals(Color.White.ToArgb) Then
    2.                     count = count + 1 'add one
    3.                 End If
    4.  
    5. 'Or                
    6.                 If photo.GetPixel(x, y).R = 255 AndAlso photo.GetPixel(x, y).G = 255 AndAlso photo.GetPixel(x, y).B = 255 Then
    7.                     count = count + 1 'add one
    8.                 End If

    -----------
    EDIT: The Argb value for white is -1, so 'If photo.GetPixel(x, y).ToArgb = -1 Then' not only works but should be faster as well.
    Last edited by Half; Dec 16th, 2006 at 11:07 PM.
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Re: Colors

    thank you very much, i am relitvly new to programing, so can you please explane what the ToArgb command does thank you

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Colors

    A description of every type and member is at your fingertips in the MSDN library:

    http://msdn2.microsoft.com/en-us/lib...or.toargb.aspx

    Having said that, there's no need to turn two Colors to ARGB components to compare them. Just compare the two Colors:
    VB Code:
    1. If photo.GetPixel(x, y) = Color.White Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Re: Colors

    ok thank you for your help! one more question however. i would like to make it so it will turn all the bactera cells (white pixles) to red or blue pixels. i used the photo.setcolor command but it dosent seem to work, im only guessing here.

  6. #6
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Colors

    That would be the SetPixel method.
    VB Code:
    1. For y = 0 To photomaxY - 1 'go though the y axis
    2.             For x = 0 To photomaxX - 1 'go though the x axis
    3.  
    4.                 If photo.GetPixel(x, y).ToArgb = -1 Then
    5.                     photo.SetPixel(x, y, Color.Red)
    6.                 End If
    7.             Next
    8.         Next
    9.  
    10.         'reload the changed picture in the picturebox
    11.         Picture.Image = photo
    You are changing the picture, but not the image contained in the picture box. That is why you must reload the changed picture in the picturebox again.

    ToArgb returns an Integer describing a color. It may seem useless, but if you decide to use a custom color in your programs, there is no way to use it by it's name as it won't have one. There can be 1000s shades of red and to use those, you must be able to address them uniquely. There is more than one way, i.e. you may use a bunch of integers containing the Red, Green and Blue values. To/From Argb uses only one integer describing all the values - I find it easier to work with.
    VB 2005, Win Xp Pro sp2

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Re: Colors

    that would be why it dident work i never reloaded it

  8. #8
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Colors

    There is most certainly a way to change pixels in a picture loaded in a picturebox but each redrawing will slow down the program. From what I've seen, it is actually desirable to make all the changes behind the scenes and redraw everything in one pass in the end.
    VB 2005, Win Xp Pro sp2

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Colors

    OR just refresh the picture box aftere editing the image
    VB Code:
    1. Picture.Refresh()

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Re: Colors

    the progam still dose not work it gives me this error on the line that changes colors:

    An unhandled exception of type 'System.Exception' occurred in system.drawing.dll

    Additional information: SetPixel is not supported for images with indexed pixel formats.

    could this be because the immage is imported as grayscale (we had to change it from monocrome to grayscale becase it picked up all the area outside of the dish, now we just make the area outside a gray color here is the whole program so far, any other tips would also be good

    VB Code:
    1. 'open file
    2.         opendio.ShowDialog() 'open file
    3.         Label1.Text = opendio.FileName 'print path on label
    4.         Picture.Image = Image.FromFile(opendio.FileName) 'show image in box
    5.         'initalize varables
    6.         Dim photo As New Bitmap(opendio.FileName) 'save image as varable
    7.         Dim white As Integer = Color.White.ToArgb
    8.         Dim totalmesure As Integer 'total amount of area mesuring
    9.         Dim pixel As Integer
    10.         Dim black As Integer = Color.Black.ToArgb
    11.         Dim countwhite As Integer ' number of white pixels
    12.         Dim countgrayb As Integer ' the number of gray and black pixels
    13.         Dim countgray As Integer 'number of gray
    14.         Dim countblack As Integer ' number of black
    15.         Dim totalpix As Integer 'total pixels
    16.         Dim y As Integer 'y pixel
    17.         Dim x As Integer 'x pixel
    18.         Dim photomaxX As Integer = photo.Size.Width 'how many pixels on X axes
    19.         Dim photomaxY As Integer = photo.Size.Height 'how many pixels on Y axes
    20.  
    21.         For y = 0 To photomaxY - 1 'go though the y axis
    22.             For x = 0 To photomaxX - 1 'go though the x axis
    23.                 If photo.GetPixel(x, y).ToArgb = white Then 'test for white pixel
    24.                     countwhite = countwhite + 1 'add one
    25.                 End If
    26.                 ProgressBar1.Maximum = photomaxX * photomaxY
    27.                 pixel = pixel + 1 'add one for every pixel tested
    28.                 ProgressBar1.Value = pixel 'move bar
    29.  
    30.             Next
    31.         Next
    32.  
    33.         'find gray and black
    34.         For y = 0 To photomaxY - 1 'go though the y axis
    35.             For x = 0 To photomaxX - 1 'go though the x axis
    36.                 If photo.GetPixel(x, y).ToArgb <> white Then 'test for non white pixel
    37.  
    38.                     countgrayb = countgrayb + 1 'add one
    39.                 End If
    40.                 pixel = 0 'reset
    41.                 pixel = pixel + 1 'add one for every pixel tested
    42.                 ProgressBar1.Value = pixel
    43.  
    44.  
    45.             Next
    46.         Next
    47.         'find black
    48.         For y = 0 To photomaxY - 1 'go though the y axis
    49.             For x = 0 To photomaxX - 1 'go though the x axis
    50.                 If photo.GetPixel(x, y).ToArgb = black Then 'test for black pixel
    51.                     countblack = countblack + 1 'add one
    52.                 End If
    53.                 pixel = 0 'reset
    54.                 pixel = pixel + 1 'add one pixle
    55.                 ProgressBar1.Value = pixel
    56.  
    57.  
    58.             Next
    59.         Next
    60.  
    61.         For y = 0 To photomaxY - 1 'go though the y axis
    62.             For x = 0 To photomaxX - 1 'go though the x axis
    63.                 If photo.GetPixel(x, y).ToArgb = white Then 'test for white pixel
    64.                     photo.SetPixel(x, y, Color.Blue)
    65.                 End If
    66.             Next
    67.         Next
    68.         Picture.Image = photo
    69.  
    70.         'calulate data
    71.         countgray = countgrayb - countblack 'finds number of gray pixels
    72.         totalpix = photomaxX * photomaxY 'finds total pixels
    73.         totalmesure = totalpix - countgray 'finds how much pixels are in the area we want to mesure
    74.  
    75.         MsgBox("total bactera pixels: " & countwhite & vbNewLine & _
    76.         "Total null pixles: " & countblack & vbNewLine & _
    77.         "Total Area: " & totalmesure & vbNewLine & _
    78.         "Percent: " & (countwhite / totalmesure) * 100) ' show all data
    79.     End Sub

  11. #11
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Colors

    Is this some other format than bmp? Compressions sometimes use pixel indexing to reduce size.
    VB 2005, Win Xp Pro sp2

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Colors

    Try this
    VB Code:
    1. Dim img As New Bitmap(opendio.FileName)
    2.         For x As Integer = 0 To img.Size.Width - 1
    3.             For y As Integer = 0 To img.Size.Height - 1
    4.                 If img.GetPixel(x, y) = Color.White Then
    5.                     countwhite += 1
    6.                 End If
    7.             Next
    8.         Next
    to get the white pixel count. Eite**

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    46

    Re: Colors

    i know as a fact from past issues that
    VB Code:
    1. if img.GetPixel(x, y) = Color.White Then
    does not work you must use .equals and even then it dosent register the pixels for some odd reson, but thanks anyways

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Colors

    Use this function. It works!
    VB Code:
    1. 'Gets the pixel count of the color
    2.     Private Function GetPixelCount(ByVal img As Bitmap, ByVal col As Color) As Integer
    3.         Dim count As Integer = 0
    4.         For x As Integer = 0 To img.Size.Width - 1
    5.             For y As Integer = 0 To img.Size.Height - 1
    6.                 If img.GetPixel(x, y).ToArgb = col.ToArgb Then
    7.                     count += 1
    8.                 End If
    9.             Next
    10.         Next
    11.         Return count
    12.     End Function
    13.  
    14.     'Changes the image color to a new color
    15.     Private Function ChangeImageColor(ByVal img As Bitmap, ByVal currentColor As Color, ByVal newColor As Color) As Bitmap
    16.         For x As Integer = 0 To img.Size.Width - 1
    17.             For y As Integer = 0 To img.Size.Height - 1
    18.                 If img.GetPixel(x, y).ToArgb = currentColor.ToArgb Then
    19.                     img.SetPixel(x, y, newColor)
    20.                 End If
    21.             Next
    22.         Next
    23.         Return img
    24.     End Function

  15. #15
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Colors

    Ok I was wrong about the pixel indexing, it is not only about compression. Anywayz, you will have to modify your code slightly as indexed images can't be manipulated easily when raw. The following will create a non pixel indexed copy of the picture. You then use that copy for pixel manipulation. Rename Photo to photo2 but only here:
    VB Code:
    1. Dim photo2 As New Bitmap(opendio.FileName)
    Then immediately after 'Dim x As Integer 'x pixel' add the following:
    VB Code:
    1. Dim photo As New Bitmap(photo2.Width, photo2.Height) ', System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
    2.         Dim painter As Graphics = Graphics.FromImage(photo)
    3.         painter.DrawImage(photo2, 0, 0)
    4.         'now you have the "unindexed" original picture, its name is photo
    Your code should be working now.
    Last edited by Half; Dec 17th, 2006 at 06:06 PM. Reason: Tags cleanup
    VB 2005, Win Xp Pro sp2

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Colors

    Quote Originally Posted by tonyrueb
    i know as a fact from past issues that
    VB Code:
    1. if img.GetPixel(x, y) = Color.White Then
    does not work you must use .equals and even then it dosent register the pixels for some odd reson, but thanks anyways
    The "=" operator is not defined for the Color type in .NET 1.x. It is in .NET 2.0. Another reason that everyone should be specifying their version when posting.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width