|
-
Dec 16th, 2006, 10:37 PM
#1
Thread Starter
Member
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:
opendio.ShowDialog() 'open file
Label1.Text = opendio.FileName 'print path on label
Picture.Image = Image.FromFile(opendio.FileName) 'show image in box
Dim photo As New Bitmap(opendio.FileName) 'save image as varable
Dim count As Integer ' number of white pixels
Dim y As Integer 'y pixel
Dim x As Integer 'x pixel
Dim photomaxX As Integer = photo.Size.Width 'how many pixels on X axes
Dim photomaxY As Integer = photo.Size.Height 'how many pixels on Y axes
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).Equals(Color.White) Then 'if the pixel color is white
count = count + 1 'add one
End If
Next
Next
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!
-
Dec 16th, 2006, 11:02 PM
#2
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:
If photo.GetPixel(x, y).ToArgb.Equals(Color.White.ToArgb) Then
count = count + 1 'add one
End If
'Or
If photo.GetPixel(x, y).R = 255 AndAlso photo.GetPixel(x, y).G = 255 AndAlso photo.GetPixel(x, y).B = 255 Then
count = count + 1 'add one
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
-
Dec 17th, 2006, 12:50 AM
#3
Thread Starter
Member
Re: Colors
thank you very much, i am relitvly new to programing, so can you please explane what the ToArgb command does thank you
-
Dec 17th, 2006, 06:42 AM
#4
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:
If photo.GetPixel(x, y) = Color.White Then
-
Dec 17th, 2006, 10:28 AM
#5
Thread Starter
Member
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.
-
Dec 17th, 2006, 11:04 AM
#6
Re: Colors
That would be the SetPixel method.
VB Code:
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).ToArgb = -1 Then
photo.SetPixel(x, y, Color.Red)
End If
Next
Next
'reload the changed picture in the picturebox
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.
-
Dec 17th, 2006, 04:18 PM
#7
Thread Starter
Member
Re: Colors
that would be why it dident work i never reloaded it
-
Dec 17th, 2006, 04:38 PM
#8
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.
-
Dec 17th, 2006, 04:40 PM
#9
Re: Colors
OR just refresh the picture box aftere editing the image
-
Dec 17th, 2006, 04:45 PM
#10
Thread Starter
Member
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:
'open file
opendio.ShowDialog() 'open file
Label1.Text = opendio.FileName 'print path on label
Picture.Image = Image.FromFile(opendio.FileName) 'show image in box
'initalize varables
Dim photo As New Bitmap(opendio.FileName) 'save image as varable
Dim white As Integer = Color.White.ToArgb
Dim totalmesure As Integer 'total amount of area mesuring
Dim pixel As Integer
Dim black As Integer = Color.Black.ToArgb
Dim countwhite As Integer ' number of white pixels
Dim countgrayb As Integer ' the number of gray and black pixels
Dim countgray As Integer 'number of gray
Dim countblack As Integer ' number of black
Dim totalpix As Integer 'total pixels
Dim y As Integer 'y pixel
Dim x As Integer 'x pixel
Dim photomaxX As Integer = photo.Size.Width 'how many pixels on X axes
Dim photomaxY As Integer = photo.Size.Height 'how many pixels on Y axes
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).ToArgb = white Then 'test for white pixel
countwhite = countwhite + 1 'add one
End If
ProgressBar1.Maximum = photomaxX * photomaxY
pixel = pixel + 1 'add one for every pixel tested
ProgressBar1.Value = pixel 'move bar
Next
Next
'find gray and black
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).ToArgb <> white Then 'test for non white pixel
countgrayb = countgrayb + 1 'add one
End If
pixel = 0 'reset
pixel = pixel + 1 'add one for every pixel tested
ProgressBar1.Value = pixel
Next
Next
'find black
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).ToArgb = black Then 'test for black pixel
countblack = countblack + 1 'add one
End If
pixel = 0 'reset
pixel = pixel + 1 'add one pixle
ProgressBar1.Value = pixel
Next
Next
For y = 0 To photomaxY - 1 'go though the y axis
For x = 0 To photomaxX - 1 'go though the x axis
If photo.GetPixel(x, y).ToArgb = white Then 'test for white pixel
photo.SetPixel(x, y, Color.Blue)
End If
Next
Next
Picture.Image = photo
'calulate data
countgray = countgrayb - countblack 'finds number of gray pixels
totalpix = photomaxX * photomaxY 'finds total pixels
totalmesure = totalpix - countgray 'finds how much pixels are in the area we want to mesure
MsgBox("total bactera pixels: " & countwhite & vbNewLine & _
"Total null pixles: " & countblack & vbNewLine & _
"Total Area: " & totalmesure & vbNewLine & _
"Percent: " & (countwhite / totalmesure) * 100) ' show all data
End Sub
-
Dec 17th, 2006, 04:51 PM
#11
Re: Colors
Is this some other format than bmp? Compressions sometimes use pixel indexing to reduce size.
-
Dec 17th, 2006, 04:54 PM
#12
Re: Colors
Try this
VB Code:
Dim img As New Bitmap(opendio.FileName)
For x As Integer = 0 To img.Size.Width - 1
For y As Integer = 0 To img.Size.Height - 1
If img.GetPixel(x, y) = Color.White Then
countwhite += 1
End If
Next
Next
to get the white pixel count. Eite**
Last edited by VBDT; Dec 17th, 2006 at 04:59 PM.
-
Dec 17th, 2006, 04:59 PM
#13
Thread Starter
Member
Re: Colors
i know as a fact from past issues that
VB Code:
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
-
Dec 17th, 2006, 05:12 PM
#14
Re: Colors
Use this function. It works!
VB Code:
'Gets the pixel count of the color
Private Function GetPixelCount(ByVal img As Bitmap, ByVal col As Color) As Integer
Dim count As Integer = 0
For x As Integer = 0 To img.Size.Width - 1
For y As Integer = 0 To img.Size.Height - 1
If img.GetPixel(x, y).ToArgb = col.ToArgb Then
count += 1
End If
Next
Next
Return count
End Function
'Changes the image color to a new color
Private Function ChangeImageColor(ByVal img As Bitmap, ByVal currentColor As Color, ByVal newColor As Color) As Bitmap
For x As Integer = 0 To img.Size.Width - 1
For y As Integer = 0 To img.Size.Height - 1
If img.GetPixel(x, y).ToArgb = currentColor.ToArgb Then
img.SetPixel(x, y, newColor)
End If
Next
Next
Return img
End Function
Last edited by VBDT; Dec 17th, 2006 at 06:31 PM.
-
Dec 17th, 2006, 06:02 PM
#15
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:
Dim photo2 As New Bitmap(opendio.FileName)
Then immediately after 'Dim x As Integer 'x pixel' add the following:
VB Code:
Dim photo As New Bitmap(photo2.Width, photo2.Height) ', System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
Dim painter As Graphics = Graphics.FromImage(photo)
painter.DrawImage(photo2, 0, 0)
'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
-
Dec 17th, 2006, 06:08 PM
#16
Re: Colors
 Originally Posted by tonyrueb
i know as a fact from past issues that
VB Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|