• Welcome to the PopMalt Forums! Whether you're new to forums or a veteran, welcome to our humble home on the web! We're a 20-year old forum community with thousands of discussions on entertainment, lifestyle, leisure, and more.

    Our rules are simple. Be nice and don't spam. Registration is free, so what are you waiting for? Join today!.

pixel location vb.net

toocool

Registered Member
how do i detect the location of a pixel of a certain colour?

e.g. how do i detect the x, y coordinates of a red pixel?


I DO NOT WANT TO FIND THE COLOUR OF A PIXEL AT A GIVEN X, Y LOCATION!!!!!! i need to do the opposite, which is find the location of a red pixel


RULES:

1. code MUST be in vb.net 2008 NOT 2005 (2010 may be acceptable,
preferrably 2008)
2. vb.net 6.0 is also sortof acceptable, but again prefferably vb.net 2008
3. do not provide code that is malicious, or gives me the colour of a given
pixel




EDITED:

i have figured out the code, except i keep getting a 'null reference exception was unhandled' error pointing to the line in the code marked with '<--ERR


CODE:


Code:
Dim pic = cam.CurrentCamera.GetCurrentImage

        PictureBox1.Image = pic

        Dim match As Boolean = False
        Dim x, y As Integer

        For y = 0 To My.Computer.Screen.WorkingArea.Height
            For x = 0 To My.Computer.Screen.WorkingArea.Width

                Do Until match = True
                    If pic.GetPixel(x, y).R > 100 And pic.GetPixel(x, y).R <= 255 '<--ERR Then
                        If pic.GetPixel(x, y).G > 100 And pic.GetPixel(x, y).G <= 255 Then
                            If pic.GetPixel(x, y).B > 100 And pic.GetPixel(x, y).B <= 255 Then
                                match = True
                                Dim pos As Point
                                pos.X = x
                                pos.Y = y
                                SetCursorPos(pos.X, pos.Y)
                            End If
                        End If
                    End If

                Loop
            Next
        Next
 
Last edited:

Smelnick

Creeping On You
V.I.P.
Holy function nest batman.

Took me a few minutes to figure out what you were doing. I can see your problem though. You have WAY too many things nested inside of each other. Loops inside ifs inside for/nexts. The error you're getting usually means somethings overflowing.

You're problem is your Do until match = true.

I'm not totally sure what you're finding the red pixel for. I'm sort of thinking you're trying to go through a grid of pixels square by square until you find the first red pixel? or are you trying to find the location of ALL the red pixels?

The problem with your DO loop is that you have it looping that nested IF. If the pixel you're checking isn't a red pixel, it's going to keep looping forever. It's an infinite loop.

I can see what you're trying to do though. You want the looping to stop once it finds a red pixel and move your cursor there.

Here's a better way to do it. (the syntax won't be correct, i'm just using psuedocode to explain myself)

Scrap the for loops. Instead use a single Do loop

Code:
                Do Until match = True OR endOfCheck = True
                    If pic.GetPixel(x, y).R > 100 And pic.GetPixel(x, y).R <= 255 '<--ERR Then
                        If pic.GetPixel(x, y).G > 100 And pic.GetPixel(x, y).G <= 255 Then
                            If pic.GetPixel(x, y).B > 100 And pic.GetPixel(x, y).B <= 255 Then
                                match = True
                                Dim pos As Point
                                pos.X = x
                                pos.Y = y
                                SetCursorPos(pos.X, pos.Y)
                            End If
                        End If
                    End If

                Loop
                If x+1 > My.Computer.Screen.WorkingArea.Width  Then
                     x = 0
                     y = y + 1
                Else
                     x = x + 1
                End If

                If x + 1 > My.Computer.Screen.WorkingArea.Width AND y + 1 > My.Computer.Screen.WorkingArea.Height Then
                endOfCheck = true
                End If

Try something like that. Generally, For loops are for when you know exactly when you want the loop to end. Do until loops are best for situations like this when you don't know where you want the loop to end.

Don't forget to dim the variables I added.
 

toocool

Registered Member
you got a point, but i'll first try scraping the 'do until match = true' loop and keeping the for, since i think it needs those for loops to check the screen. failing that, i'll try your idea.

it certainly makes sence that the error is in the do loop
------
or it could just be that it tries to execute the pic.getpixel code before the webcam gets a chance to load, and therefore pic (which represents whatever the webcam is capturing) has no image associated with it. i'll try setting a delay before the for loops start their execution. i'll do that before i try your pseudocode (i'm just troubleshooting using ideas that are most likely to work first. not saying you provided a useless tip, just saying i think the webcam doesn't load fast enough)


EDITED:

fixed the 'null reference exception was unhandled' error. however, i now get a 'argument out of range exception was unhandled'

the details for this error are: Parameter must be positive and < Width. Parameter name: x

this means there is a problem with x
------
forgot to supply the code:

Code:
Dim pic = cam.CurrentCamera.GetCurrentImage

        PictureBox1.Image = pic

        Dim match As Boolean = False
        Dim x, y As Integer

        If pic IsNot Nothing Then
            For y = 0 To My.Computer.Screen.WorkingArea.Height - 1
                For x = 0 To My.Computer.Screen.WorkingArea.Width - 1

                    If pic.GetPixel(x, y).R > 100 And pic.GetPixel(x, y).R <= 255 Then
                        If pic.GetPixel(x, y).G > 100 And pic.GetPixel(x, y).G <= 255 Then
                            If pic.GetPixel(x, y).B > 100 And pic.GetPixel(x, y).B <= 255 Then
                                Dim pos As Point
                                pos.X = x
                                pos.Y = y
                                SetCursorPos(pos.X, pos.Y)
                            End If
                        End If
                    End If

                Next
            Next
        End If
 
Last edited:
Thread starter Similar threads Forum Replies Date
Mirage Movies & TV 8
toocool Computers 1
Boredie Technology 1
RATTIE Pets 10
Mirage Computers 3
Top