|
-
Oct 19th, 2005, 06:08 PM
#1
Thread Starter
Frenzied Member
reading the color of pixels
how would i read the pixels in a picture box and store there colors in a string like
green,black,blue,pink
or something like that or how would i read just the first pixel of a picture i can do the rest.
-
Oct 19th, 2005, 06:35 PM
#2
Re: reading the color of pixels
You can read a pixel by using the Point method of the picturebox, eg:
MsgBox Picture1.Point(1,1)
Note however that this only returns the colour number, it will not return words as you want - you will have to convert the number somehow.
I think I posted an example of a method to do this, so I'll have a little search!
-
Oct 19th, 2005, 06:38 PM
#3
Re: reading the color of pixels
Posibly by using the GetPixel API:
The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.
VB4-32,5,6
Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
VB.NET
System.Drawing.Bitmap.GetPixel
Operating Systems Supported
Requires Windows NT 3.1 or later; Requires Windows 95 or later
Library
Gdi32
Parameter Information
· hdc
Identifies the device context.
· nXPos
Specifies the logical x-coordinate of the pixel to be examined.
· nYPos
Specifies the logical y-coordinate of the pixel to be examined.
Return Values
If the function succeeds, the return value is an RGB value. If the pixel is outside of the current clipping region, the return value is CLR_INVALID.
-
Oct 19th, 2005, 06:39 PM
#4
Re: reading the color of pixels
 Originally Posted by si_the_geek
You can read a pixel by using the Point method of the picturebox, eg:
MsgBox Picture1.Point(1,1)
Kewl
-
Oct 19th, 2005, 06:43 PM
#5
Thread Starter
Frenzied Member
Re: reading the color of pixels
 Originally Posted by si_the_geek
You can read a pixel by using the Point method of the picturebox, eg:
MsgBox Picture1.Point(1,1)
Note however that this only returns the colour number, it will not return words as you want - you will have to convert the number somehow.
I think I posted an example of a method to do this, so I'll have a little search!
how would i tell wat color it is?
-
Oct 19th, 2005, 06:50 PM
#6
Re: reading the color of pixels
-
Oct 19th, 2005, 07:03 PM
#7
Re: reading the color of pixels
I'm sure I've done this before, but couldn't find it - so here it is again!
This works by converting the number you get (a long) into separate R,G,B values, and then 'estimating' what the colour is - as there is such a range of values this is about the best you will be able to do.
VB Code:
Private Function ColourLongToWords(ByVal lColour As Long) As String
'Converts an RGB colour number to a string equivalent
'By Si_the_geek, VBForums
'ensure value is within range for colours
lColour = lColour And &HFFFFFF
'convert to separate RGB values
Dim iRed As Integer, iGreen As Integer, iBlue As Integer
iRed = lColour And &HFF
iGreen = (lColour \ &H100) And &HFF
iBlue = lColour \ &H10000
'"guess" the colour based on these values
Dim sColourName As String
Select Case iRed
Case Is > 170 'lots of red
Select Case iGreen
Case Is > 170 'lots of green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "White"
Case Is > 85: sColourName = "Bright Yellow"
Case Else: sColourName = "Yellow"
End Select
Case Is > 85 'medium green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Pink"
Case Is > 85: sColourName = "Magenta"
Case Else: sColourName = "Orange"
End Select
Case Else 'little green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Purple"
Case Is > 85: sColourName = "Dark Pink"
Case Else: sColourName = "Red"
End Select
End Select
Case Is > 85 'medium red
Select Case iGreen
Case Is > 170 'lots of green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Cyan"
Case Is > 85: sColourName = "Green"
Case Else: sColourName = "Bright Green"
End Select
Case Is > 85 'medium green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Dark Blue"
Case Is > 85: sColourName = "Dark Grey"
Case Else: sColourName = "Dark Green"
End Select
Case Else 'little green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Dark Blue"
Case Is > 85: sColourName = "Purple"
Case Else: sColourName = "Dark Red"
End Select
End Select
Case Else 'little red
Select Case iGreen
Case Is > 170 'lots of green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Cyan"
Case Is > 85: sColourName = "Green"
Case Else: sColourName = "Bright Green"
End Select
Case Is > 85 'medium green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Blue"
Case Is > 85: sColourName = "Dark Cyan"
Case Else: sColourName = "Dark Green"
End Select
Case Else 'little green
Select Case iBlue 'blue: lots/medium/little
Case Is > 170: sColourName = "Bright Blue"
Case Is > 85: sColourName = "Dark Blue"
Case Else: sColourName = "Black"
End Select
End Select
End Select
ColourLongToWords = sColourName
End Function
Note that I may not have chosen colour names that you like, but it is easy enough to change them!
Last edited by si_the_geek; Oct 20th, 2005 at 10:36 AM.
-
Oct 19th, 2005, 07:13 PM
#8
Thread Starter
Frenzied Member
Re: reading the color of pixels
also how would i get the pixels by pixels for a picture?
-
Oct 19th, 2005, 07:21 PM
#9
Re: reading the color of pixels
Do you mean read each pixel in a picturebox?
-
Oct 19th, 2005, 07:34 PM
#10
Thread Starter
Frenzied Member
Re: reading the color of pixels
 Originally Posted by si_the_geek
Do you mean read each pixel in a picturebox?
well wat im trying to do is make a program to do this
load a picture box
read each pixel from it 1 by 1 by 1 and if the pixel is white it will add a " " to the textbox and if its black it adds a ".". but i cant get the checking the pixels to work
-
Oct 19th, 2005, 07:50 PM
#11
Re: reading the color of pixels
you could have mentioned that before I wrote the Function above!
All you need to do is create two nested loops (one for vertical and one for horizontal), and read each pixel using Point. You can then use a much shorter version of my function (or just use it as-is) to check whether the value is one of the colours you want.
VB Code:
Dim lX as Long, lY as Long
Dim lPixelColour as Long
With Picture1
For lX = 1 to .Picture.Width
For lY = 1 to .Picture.Height
lPixelColour = .Point(lX,lY)
Select Case ColourLongToWords(lPixelColour)
Case "Black"
'do something
Case "White
'do something else
End Select
Next lY
Next lX
End With
Note that this is untested, I'm not sure if the loops (or the parameters to Point) are correct.
-
Oct 19th, 2005, 07:55 PM
#12
Thread Starter
Frenzied Member
Re: reading the color of pixels
 Originally Posted by si_the_geek
 you could have mentioned that before I wrote the Function above!
All you need to do is create two nested loops (one for vertical and one for horizontal), and read each pixel using Point. You can then use a much shorter version of my function (or just use it as-is) to check whether the value is one of the colours you want.
VB Code:
Dim lX as Long, lY as Long
Dim lPixelColour as Long
With Picture1
For lX = 1 to .Picture.Width
For lY = 1 to .Picture.Height
lPixelColour = .Point(lX,lY)
Select Case ColourLongToWords(lPixelColour)
Case "Black"
'do something
Case "White
'do something else
End Select
Next lY
Next lX
End With
Note that this is untested, I'm not sure if the loops (or the parameters to Point) are correct.
wat would i use in ur function to find if its black or white?
-
Oct 19th, 2005, 08:06 PM
#13
Re: reading the color of pixels
Those parts are already there. If you don't understand which parts to remove, it's probably best to leave it as-is.
Note that there were a couple of typo's in the function, which I have now corrected.
-
Oct 19th, 2005, 08:33 PM
#14
Thread Starter
Frenzied Member
Re: reading the color of pixels
 Originally Posted by si_the_geek
Those parts are already there. If you don't understand which parts to remove, it's probably best to leave it as-is.
Note that there were a couple of typo's in the function, which I have now corrected.
it didnt work i got an error in the function it was overflow
iGreen = (lColour And &HFFFF) \ &H100
-
Oct 19th, 2005, 08:34 PM
#15
Re: reading the color of pixels
 Originally Posted by high6
....how would i read just the first pixel of a picture i can do the rest.
 Originally Posted by high6
....also how would i get the pixels by pixels for a picture?.
C'mon some effort on your part is required... (Sorry Si)
-
Oct 19th, 2005, 08:48 PM
#16
Re: reading the color of pixels
Minor change, from this:
iGreen = (lColour And &HFFFF) \ &H100
To this:
iGreen = (lColour And &HFF00) Mod &HFF
I have only one thing to say Bruce, and that is:
-
Oct 19th, 2005, 09:02 PM
#17
Thread Starter
Frenzied Member
Re: reading the color of pixels
well its late so im going to bed but i tryed and wat happened was it just wrote "." like 20 times across in 4 rows and that isnt wat the picture was can anoyone help?
-
Oct 20th, 2005, 08:39 AM
#18
Re: reading the color of pixels
-
Oct 20th, 2005, 10:30 AM
#19
Re: reading the color of pixels
The code for green is still wrong. Should be
VB Code:
iGreen = (lColour \ &H100) And &HFF
-
Oct 20th, 2005, 10:31 AM
#20
Re: reading the color of pixels
it just wrote "." like 20 times across in 4 rows and that isnt wat the picture was
This is probably because you need to adjust the thresholds in the function to catch the color you are trying to read. Can you upload a sample picture so we could try it?
-
Oct 20th, 2005, 10:38 AM
#21
Re: reading the color of pixels
 Originally Posted by moeur
The code for green is still wrong. Should be
VB Code:
iGreen = (lColour \ &H100) And &HFF
Indeed - I just checked again and my previous correction worked for several values of Blue, but not all. Now fixed above!
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
|