Finding a pixel in a window

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Finding a pixel in a window

Post by SFSxOI »

1. How would you find a single pixel of a particular color in a window and return the coordinates of that pixel if you know the pixel color but don't know the coordinates of the pixel in the window?

2. After you find the pixel and its coordinates, how would you track that particular pixel if it moved in the window without having to find it again?
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

Post by theteapot »

1. See code below. Plot() is slow at times, and it is hard to get lots of colours quickly, so it may take a while depending on your computer. In the code, I've drawn a pixel, found it, and waited for the user to close the window. You will notice that the higher the values of #Width and #Height you give, the longer it takes to find it. Roughly, Plot() will take 1 ms per call.

2. You could only do that if you knew precisely how far it was moving, and find it by looking in a circle.

Code: Select all

#Width     = 400
#Height    = 400
OpenWindow(1, 10, 10, #Width, #Height, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "Window Test")

;;;;;Draw the pixel somewhere.
StartDrawing(WindowOutput())
 pX = Random(#Width)
 pY = Random(#Height)
 Plot(pX, pY, 255) ;The true pixel is red
 Debug "The pixel has been drawn at (" + Str(pX) + ", " + Str(pY) + ")."
 ;;;Dud pixels...
 Plot(Random(#Width), Random(#Height), 0)
 Plot(Random(#Width), Random(#Height), 0)
 Plot(Random(#Width), Random(#Height), 0)
StopDrawing()

;;;;Find the pixel.
Timer = ElapsedMilliseconds()
StartDrawing(WindowOutput())
 For x = 1 To WindowWidth()
  For y = 1 To WindowHeight()
   If Point(x, y) = 255
    Debug "The point is at (" + Str(x) + ", " + Str(y) + ")."
    Debug "It took " + Str(ElapsedMilliseconds() - Timer) + " milliseconds to find."
   EndIf
  Next
 Next
StopDrawing()

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow

CloseWindow(1)
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

2.
Pixels don't move period. If it appears to you as if the pixel is moving, it is because it exchanges colour with a neighbour pixel.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

yes, you are correct of course :) ...pixels dont move - they appear to move. I said it the wrong way, what I intended to say was what you just said kinda, i guess my question should have been "how to track when it exchanges color with a neighbour pixel". Anyway, the code you posted above i'm going to try it out a little later today, and intrestingly enough you happen to have used the very color i'm interested in - red.

Thank you

Trond wrote:2.
Pixels don't move period. If it appears to you as if the pixel is moving, it is because it exchanges colour with a neighbour pixel.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

After you've found it, you don't have to check the whole area again, you can check in a rectangle around where it last was, the sides of the rectangle should be twice the maximal speed of the pixel.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

What does "twice the maximal speed of the pixel" mean? I'm not sure i understand, i think i understand but i'm not sure as i may be thinking of it in a different aspect.

Trond wrote:After you've found it, you don't have to check the whole area again, you can check in a rectangle around where it last was, the sides of the rectangle should be twice the maximal speed of the pixel.
Post Reply