Jump to content

Find all the pixels of a particular color in the screen.


Recommended Posts

Hi everyone,

 

I am trying to find the location of all the pixels of a particular on the screen. In order for me to do that, I move the cursor to the color that I am selecting in ms-paint. Once I have done that, I draw a line on the screen and hope to catch the location of all the pixels but the script doesnt do anything once it gets to that point.

 

Looking for some help here.

 

#include <MsgBoxConstants.au3>

Local $aArray[0]
Sleep(5000)

Local $aPos = MouseGetPos()

Local $iColor = PixelGetColor($aPos[0], $aPos[1])

MsgBox($MB_SYSTEMMODAL, "", "The decimal color is: " & $iColor)
;MsgBox($MB_SYSTEMMODAL, "", "The hex color is: " & Hex($iColor, 6))

Sleep(5000)

Local $aCoord = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor)
If Not @error Then
    MsgBox($MB_SYSTEMMODAL, "", "X and Y are: " & $aCoord[0] & "," & $aCoord[1])
 EndIf


;MouseMove($aCoord[0], $aCoord[1], 0)


For $x = 0 To @DesktopWidth Step 1
   For $y = 0 To @DesktopHeight Step 1
      If PixelGetColor($x,$y) = $iColor Then
         ;Add $x and $y to Array using _ArrayAdd() (or whatever you prefer)
         MouseMove($x, $y, 10)
         _ArrayAdd($aArray, $x & "-" & $y)
      EndIf
   Next
Next
_ArrayDisplay($aArray, "Pixels")

 

Link to comment
Share on other sites

Your script is working for me (you need to add an #include tho).  Not the most efficient, but it is working...

ps.  on Win10, it is terribly slow, maybe that is why you think nothing is happening...

Edited by Nine
Link to comment
Share on other sites

Is there a faster way to do this then? I need to run this on an ongoing basis to see if a new line has graphed on the screen.

 

Also what is the #include that I am missing. <Array.au3>?

 

edit: I added ConsoleWrite($x & "-" & $y & @LF) to the loop and its very very slow. Dont think this solution will work for my need.

Edited by rm65453
additional detail.
Link to comment
Share on other sites

Link to comment
Share on other sites

No its not. I basically wanted to detect a zig zag line on a forex chart and find out the slope of the last drawn line. if its a positive or negative slope basically.

 

What I was thinking was if I have the location of all the pixels on the screens that I can go from right to left to find the last pixel and then find the relative position of the pixel before that and detect the slop that way.

Edited by rm65453
Link to comment
Share on other sites

The default PixelGetColor is slow. To speed it up, take a look on those:

With this, you can make in memory screenshot and traverse it pixel by pixel muuuch faster than with PixelGetColor. Also works if the window you monitor is in background!

or here:

or here:

 

Edited by MaximusCZ
Link to comment
Share on other sites

Ok, I see.  Since it is a totally static picture, I would take a screen capture of your window (_ScreenCapture_CaptureWnd) .  Then use GDI+ to convert the bitmap into a scan memory (_GDIPlus_BitmapLockBits).  And finally use that space in memory to locate pixel as you want to.  The example of _GDIPlus_BitmapLockBits is close to what I am suggesting. 

You may also look at the previously mentioned UDF, there is probably some work that have already been done around this area.  I made my own UDF (unpublished).  It is not very complicated, but if you can use a UDF, it can serve as a basis for your own project.

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <String.au3> ;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <FastFind.au3>
#Include <WinAPI.au3>
Opt("WinTitleMatchMode", 2)

Local $aArray[0]
Local $x
Local $y
Local $x2
Local $y2
Local $aCoords[0]
Sleep(10000)

Local $aPos = MouseGetPos()

Local $iColor = PixelGetColor($aPos[0], $aPos[1])
;Local $iColor = 9785202
;Local $iColor = 15539236
MsgBox($MB_SYSTEMMODAL, "", "The decimal color is: " & $iColor & ". Monitoring zigzag lines.")
;MsgBox($MB_SYSTEMMODAL, "", "The hex color is: " & Hex($iColor, 6))

Local $hGUI = GUICreate("Signal Monitoring", 100, 100)
 Local $idButton = GUICtrlCreateButton("Monitoring", 10, 10, 80, 80)
GUISetState(@SW_SHOW, $hGUI)
Sleep(1000)
While 1
Sleep (10000)
$x = 0
$y = 0
$x2 = 0
$y2 = 0

FFSnapShot(0, 0, 0, 0)

;$aCoords = FFNearestSpot(4, 5 , @DesktopHeight, 0, 10, $iColor, False)
$aCoords = FFNearestPixel(@DesktopHeight, 0, $iColor, False)

If UBound($aCoords) = 0 Then
   GUICtrlSetData( $idButton, "Monitoring")
Else
;_ArrayDisplay($aCoords, "Coordinates")
$x = $aCoords[0]
$y = $aCoords[1]
;MouseMove($x, $y, 10)
$newx = $x - 20
$aCoords = FFNearestPixel($newx, $y, $iColor, False)
;$aCoords = FFNearestSpot(4, 5 , $newx, $y, 10, $iColor, False)
;_ArrayDisplay($aCoords, "Coordinates")
;$aCoords = FFNearestPixel($newx, $y, $iColor, False)

$x2 = $aCoords[0]
$y2 = $aCoords[1]
;MouseMove($x2, $y2, 10)
;MsgBox(0,"Coordinates", $x & "-" & $y & "--" & $x2 & "-" & $y2)

If $y > $y2 Then
    GUICtrlSetData( $idButton, "Down")
 Else
    GUICtrlSetData( $idButton, "Up")
EndIf
EndIf
WEnd

This one is working but for some reason is unreliable and I cannot figure out why it works until I have 2 or 3 lines but then stops working.

 

 

edit: seems like it works well if the lines are in the left half of the screens but not so well as it scrolls towards the right.

Edited by rm65453
more details.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...