PixelSearch search area always finds colour

Get help with using AutoHotkey and its commands and hotkeys
Rlentless
Posts: 11
Joined: 19 Apr 2018, 06:16

PixelSearch search area always finds colour

06 Nov 2018, 09:45

I'm trying to get the PixelSearch to find this 8d8e8d RGB colour. I used 0,0,A_ScreenWidth,A_ScreenHeight so that it searches the entire active window, but my problem is that it always finds it even if the colour is not present. So I decided to mark a specific small region instead of 0,0 I wrote 500,200,1000,600. I opened up a fully black image on full screen just to be sure that it covers the above region. I run the pixel but even if the image is fully black and I'm searching for white pixels it always comes out as 'Found the colour'. I really don't know what to do in this situation :(

Code: Select all


Numpad1::

    PixelSearch,ax,ay,500,200,1000,600,8d8e8d,RGB, Fast  

    if ErrorLevel =2
        msgbox, Not working
    if ErrorLevel =1
        msgbox, Did not find the colour
    if ErrorLevel =0
        msgbox, Found the colour



return

ESC::ExitApp
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: PixelSearch search area always finds colour

06 Nov 2018, 10:04

Sometimes designating a Coordmode can fix issues like this. For example before the PixelSearch command try: CoordMode, Pixel, Screen
For more info please see the documentation.
for direct help, tweet at me: https://twitter.com/CW_DTech
Rlentless
Posts: 11
Joined: 19 Apr 2018, 06:16

Re: PixelSearch search area always finds colour

06 Nov 2018, 17:48

TLM wrote:
06 Nov 2018, 10:04
Sometimes designating a Coordmode can fix issues like this. For example before the PixelSearch command try: CoordMode, Pixel, Screen
For more info please see the documentation.
Thank you for the answer. I also tried using CoordMode with Pixel, Screen, but the result is the same. No matter what is on the screen it always finds the colour.
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: PixelSearch search area always finds colour

06 Nov 2018, 18:27

I honestly rarely use pixel commands. I have run into problems detecting correct colors when AERO and or DWM is running.
What's your underlying goal? Is there perhaps a more reliable way for you to achieve it? Lemmi know..
for direct help, tweet at me: https://twitter.com/CW_DTech
Rlentless
Posts: 11
Joined: 19 Apr 2018, 06:16

Re: PixelSearch search area always finds colour

07 Nov 2018, 08:41

TLM wrote:
06 Nov 2018, 18:27
I honestly rarely use pixel commands. I have run into problems detecting correct colors when AERO and or DWM is running.
What's your underlying goal? Is there perhaps a more reliable way for you to achieve it? Lemmi know..
I'm trying to detect a change in a small area of the screen that once detected uses a combination of keys or mouse clicks. I wanted to detect a change because it makes more sense than detecting an image, with detecting a change it can work with anything, white pixels changing to black, anything popping up in the selected region and so on... I tried working around that with ImageSearch because I can't get the PixelSearch to work, as for now it works pretty well, but I ran into a different problem.

After 'else if(ErrorLevel=0)' I'm using 'sleep', but no matter what value I assign to sleep, it ignores it and acts immediately. There are other sleep values coming up later, but they work fine, it's just the first 'sleep' that gets ignored. Is there a different way than 'sleep' for a program to delay the actions after detecting an image? I also assigned Num1 to stop the program, but it stopped working suddenly the next day and now pressing num1 does not stop the program :(

Here's the code

Code: Select all

SetWorkingDir, (DIRECTORY)
CoordMode,Mouse,screen
CoordMode,Pixel,screen
Gui,2:+AlwaysOnTop
Gui,2:Add,Button,x10 y20 w200 h20 gFind_image,Find image
Gui,2:Add,Button,x10 w200 h20 gGui_1,Load / Reload Image
Gui,2:Add,Text,x10 ,Press Numpad 1 to stop
Gui,2:Show,x1000 y200
return
2GuiClose:
    ExitApp

Find_Image:


    Stop:=0
    While(Stop=0) 786, 33 1096, 70
        {
                ;ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ImageFile, n*5
                ImageSearch, Loc_X, Loc_Y, 786, 33, 1096, 73, *12 (DIRECTORY) 
                if(ErrorLevel=2)
                    {
                        msgbox,ErrorLevel = 2'nCos sie zjebalo ;Error2
                    }
                else if (ErrorLevel=1)
                    {
                        TrayTip,,Nie widze rybki :(,2 ; Can't see the image
                    }
                else if(ErrorLevel=0) ;Change from mouse to keypress
                    {
                            sleep 1500
                        SendInput, {2 down}
                            
                            sleep 500
                        SendInput, {2 up}
                            
                      
                            sleep 6000
                        
                        
                        SendInput, {1 down}
                        
                            sleep 500
                        SendInput, {1 up}
                        
                            sleep 6000
                        SendInput, {2 down}
                        
                            sleep 500
                        SendInput, {2 up}
                        
                        
                        
                        
                        
                     
                        ;MouseMove, Loc_X+(posw//2) ,Loc_Y+(posh//2) ; USE LATER 
                        ;tx:=Loc_X+(posw//2)
                        ;ty:=Loc_Y+(posh//2)
                        ;click,%tx% %ty%
                    }
        }
        return
    
GuiContextMenu:
    Gui,1:Destroy
    return

Move_Window:
    Random,new_x,0,A_ScreenWidth-posw
    Random,new_y,0,A_screenHeight-posh
    Gui,1:show,x%New_x% y%new_y%
    return

Gui_1()
    {
        global
        Gui,1:Destroy
        Gui,1:Margin,0,0
        Gui,1:+AlwaysOnTop -Caption +Owner2
        Gui,1:Add,Picture,x0 y0 vClip_Picture gMove_Window, (DIRECTORY)
        GuiControlGet,Pos,1:Pos,Clip_Picture
        Gui,1:Show, AutoSize
        
     }
       
Numpad1::
    Stop:=1
    return
    
      



*ESC::ExitApp
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: PixelSearch search area always finds colour

09 Nov 2018, 09:14

sorry for the delay, any progress? this could be your problem While(Stop=0) 786, 33 1096, 70
for direct help, tweet at me: https://twitter.com/CW_DTech
Rlentless
Posts: 11
Joined: 19 Apr 2018, 06:16

Re: PixelSearch search area always finds colour

09 Nov 2018, 09:40

TLM wrote:
09 Nov 2018, 09:14
sorry for the delay, any progress? this could be your problem While(Stop=0) 786, 33 1096, 70
I gave up on the PixelSearch, decided to stick with ImageSearch and so far it works great, I keep on tweaking the script, changing/adding things so I keep on running into different problems haha. As for now I am using this and my problem is that it only works once instead of looping itself. I can't seem to figure out how to make this loop so that if keeps on looking for the first Find_Image and when if errorlevel =0 once it finds the image it proceeds to 2Find_Image ->

EDIT: As for the above problem with the first sleep value being ignored, I simply added another sleep with the same value and it picks up on the second one which is fine.

Code: Select all

SetWorkingDir, C:\Users\User 1\Desktop\folder\image.png
CoordMode,Mouse,screen
CoordMode,Pixel,screen
Gui,2:+AlwaysOnTop
Gui,2:Add,Button,x10 y20 w200 h20 gFind_image,  Find Image
Gui,2:Add,Button,x10 y70 w200 h20 g2Find_image,  Find Second Image
;Gui,2:Add,Button,x10 y50 w200 h20 gLowienie, 2. Maybe Add Later
Gui,2:Add,Button,x10 y90 w200 h40 gGui_1, Load New Image
Gui,2:Add,Text,x10 ,Press Numpad 1 to stop
Gui,2:Show,x1000 y200
return
2GuiClose:
    ExitApp




Find_Image:
        

    Stop:=0
    While(Stop=0) 
        {
                ;ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ImageFile, n*5
                ImageSearch, Loc_X, Loc_Y, 786, 33, 1096, 73, *12 C:\Users\User 1\Desktop\folder\image.png 
                if(ErrorLevel=2)
                    {
                        msgbox,ErrorLevel = 2'nCos sie zjebalo ;Error2
                    }
                else if (ErrorLevel=1)
                    {
                        ;TrayTip,,can't see the image :(,2 ; Can't see the image
                        
                    }
                else if(ErrorLevel=0) ; unnecessary comment
                    {
                       
    
                        sleep 2000
                        SendInput, {3 down}
                            Sleep 400
                        SendInput, {3 up}
                        SendInput, {2 down}
                            
                            sleep 500
                        SendInput, {2 up}
                            
                      
                            sleep 5000
                            

                        2Find_Image: ; This is inside the 'else if(ErrorLevel=0) so that it only acts if the first ImageSearch is successful 
        
                            Stop:=0
                            While Stop:=0 ;1726, 650, 1888, 940 coord comment
                            
                            
                                {
                                
                                    ImageSearch, ImgRybyX, ImgRybyY, 0, 0, A_ScreenWidth, A_ScreenHeight, *5 C:\Users\User 1\Desktop\folder\image2.png 
                                        if(ErrorLevel=2)
                                            {
                                                msgbox,ErrorLevel = 2'nCos sie zjebalo ;Error2 2 inside the 'else if'
                                            }
                                        else if (ErrorLevel=1)
                                            {
                                                TrayTip,,can't see the image inside 'else if' :(,2 ; Can't see the image
                                            }
                                        else if(ErrorLevel=0) ; unnecessary comment 
                                            {
                                                MouseMove, right, ImgRybyX ,ImgRybyY ; USE LATER 
                                                tx:=ImgRybyX
                                                ty:=ImgRybyY
                                                Mouseclick, right, ImgRybyX ,ImgRybyY, %tx% %ty%
                                                
                                                    sleep 2000
                                                    SendInput, {2 down}
                                                    sleep 500
                                                    SendInput, {2 up}
                                                    
                                            }
                                }
                    return
                    }
        }
        return                             
                         
                        ;MouseMove, Loc_X+(posw//2) ,Loc_Y+(posh//2) ; USE LATER 
                        ;tx:=Loc_X+(posw//2)
                        ;ty:=Loc_Y+(posh//2)
                        ;click,%tx% %ty%
    
GuiContextMenu:
    Gui,1:Destroy
    return

Move_Window:
    Random,new_x,0,A_ScreenWidth-posw
    Random,new_y,0,A_screenHeight-posh
    Gui,1:show,x%New_x% y%new_y%
    return

Gui_1()
    {
        global
        Gui,1:Destroy
        Gui,1:Margin,0,0
        Gui,1:+AlwaysOnTop -Caption +Owner2
        Gui,1:Add,Picture,x0 y0 vClip_Picture gMove_Window, C:\Users\User 1\Desktop\folder\imageee.png 
        GuiControlGet,Pos,1:Pos,Clip_Picture
        Gui,1:Show, AutoSize
        
     }
       
Numpad1::
    Stop:=1
    return
    
      
*ESC::ExitApp
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: PixelSearch search area always finds colour

09 Nov 2018, 09:51

Rlentless wrote:
09 Nov 2018, 09:40
....so far it works great
Ok sounds good.

FYI, your while loop was incorrect
for direct help, tweet at me: https://twitter.com/CW_DTech
User avatar
Blackholyman
Posts: 1281
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: PixelSearch search area always finds colour

09 Nov 2018, 13:09

The number you are searching for is the wrong format

It needs to be a hex or decimal number, so you are missing 0x to make your color id a hexidecimal number
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: PixelSearch search area always finds colour

09 Nov 2018, 22:05

https://autohotkey.com/board/topic/3897 ... ix-search/.

Instead of searching for a particular pixel,it searches for the pixel in the context of nearby pixels,It's my goto for pixel related stuff...& It's magnitudes faster than image search, & more reliable than plain pixel search.

Code: Select all

;___________________________________________
;GetMatrix(StartX,StartY,Dimension)
;FindMatrix(ScanStartX,ScanEndX,ScanStartY,ScanEndY,Dimension,Matrix)
;___________________________________________

;Tip : For faster searching, make sure the first pixel of matrix is
;      not abundant on the screen

start:

;example of 9x9 matrix

SetBatchLines, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

SplashImage,, W185 H50 B1, Press F4 to grab Matrix at mouse position,, 
KeyWait, F4, D
MouseGetPos, mX, mY
SplashImage, Off
Matrix := GetMatrix(mX,mY,32)
TrayTip, MATRIX, MATRIX: %matrix%
Result := _FindMatrix(1,A_ScreenWidth,1,A_ScreenHeight,32,Matrix)
ToolTip, %Result%

StringSplit, Result, Result, %A_Space%

Loop, 5
{
	SplashImage,, X%Result1% Y%Result2% W3 H3 B1 CWRed, .
	Sleep, 500
	SplashImage, Off
	Sleep, 500
}
Goto, start
ExitApp



;___________________________________________

GetMatrix(StartX,StartY,Dimension)
{
	Loop, %Dimension%
	{
		Loop, %Dimension%
		{
			PixelGetColor, Pix, %StartX%, %StartY%
			Matrix = %Matrix%|%Pix%
			StartX ++
		}
		
		StartY ++
		StartX -= %Dimension%
	}
	StringTrimLeft, Matrix, Matrix, 1
	Return %Matrix%
}
;___________________________________________

FindMatrix(ScanStartX,ScanEndX,ScanStartY,ScanEndY,Dimension,Matrix)
{
	StringSplit, Pix, Matrix, |
	Length := ScanEndY - ScanStartY
	
	;loop for per line of screenY
	Loop, %Length%
	{
		StartX = %ScanStartX%
		EndX = %ScanEndX%

		StartY := ScanStartY + A_Index - 1
		EndY := ScanStartY + A_Index - 1
	
		;loop for searching inside a line
		Loop
		{
			count = 0
			MatchFound = 1
			
			;change variance and 'Fast' if not suited
			PixelSearch, pX, pY, %StartX%, %StartY%, %EndX%, %EndY%, %Pix1%, 0, Fast
			IfNotEqual, ERRORLEVEL, 0, Break
			sX = %pX%
			sY = %pY%
			
			;loop for matching
			Loop, %Dimension%
			{
				Loop, %Dimension%
				{
					Count ++
					PixelGetColor, CurrPix, %pX%, %pY%
					
					IfNotEqual, Pix%Count%, %CurrPix%
						MatchFound = 0
					
					IfEqual, MatchFound, 0, Break
					pX ++
				}
				IfEqual, MatchFound, 0, Break
				pY ++
				pX -= %Dimension%
			}
			
			;match found!
			IfEqual, MatchFound, 1
			{
				Result = %sX% %sY%
				Return Result
			}
			
			StartX ++
			
			DiffX := EndX - StartX
			IfLess, DiffX, %Dimension%, Break
		}
		StartY ++
		EndY ++
	}
	Return 0
}
;___________________________________________






/*
;The GetMatrix() function was very useful for me. I made a change so that it is able to search rectangular areas as well and wanted to share it in case anyone has a use for it.

GetMatrix(StartX, startY, DimensionX, DimensionY)
{
	Loop, %DimensionY%
	{
		Loop, %DimensionX%
		{
			PixelGetColor, Pix, %StartX%, %StartY%
			Matrix = %Matrix%|%Pix%
			StartX ++
		}
		StartY ++
		StartX -= %DimensionX%
	}
	StringTrimLeft, Matrix, Matrix, 1
	Return %Matrix%
}
*/


/*
I'd identified an error (optimization really) that was causing some issues when the first pixel was not unique on the screen. After the 2 for loops that check the individual pixels, I updated StartX to the last X value returned by PixelSearch (plus one so it increments the loop).

With the original implementation I found PixelSearch was repeatedly returning the same pixel until the increment eventually caught up; which could be very wasteful in some cases :p.

For some people who are having trouble getting this to work on rectangular areas try the change below as well; I had one strange case where the function returned false negatives because the outer loop (length) was ending for some reason before the "line search" loop found the correct pixel. 
*/
_FindMatrix(ScanStartX,ScanEndX,ScanStartY,ScanEndY,Dimension,Matrix)
{
	StringSplit, Pix, Matrix, |
	Length := ScanEndY - ScanStartY
	pX = 0
	pY = 0
	
	;loop for per line of screenY
	Loop, %Length%
	{
		StartX = %ScanStartX%
		EndX = %ScanEndX%

		StartY := ScanStartY + A_Index - 1
		EndY := ScanStartY + A_Index - 1
	
		;loop for searching inside a line
		Loop
		{
			count = 0
			MatchFound = 1
			
			;change variance and 'Fast' if not suited
			PixelSearch, pX, pY, %StartX%, %StartY%, %EndX%, %EndY%, %Pix1%, 0, Fast 
			IfNotEqual, ERRORLEVEL, 0, Break
			sX = %pX%
			sY = %pY%
			
			;loop for matching
			Loop, %Dimension%
			{
				Loop, %Dimension%
				{
					Count ++
					PixelGetColor, CurrPix, %pX%, %pY%
					IfNotEqual, Pix%Count%, %CurrPix%
						MatchFound = 0
					
					IfEqual, MatchFound, 0, Break
					pX ++
				}
				IfEqual, MatchFound, 0, Break
				pY ++
				pX -= %Dimension%
			}
			
			;match found!
			IfEqual, MatchFound, 1
			{
				Result = %sX% %sY%
				Return Result
			}
			
			StartX := px + 1
			
			DiffX := EndX - StartX
			IfLess, DiffX, %Dimension%, Break
		}
		StartY ++
		EndY ++
	}
	Return 0
}
live ? long & prosper : regards

Return to “Ask For Help”

Who is online

Users browsing this forum: Bort P, guest3456, hoppfrosch and 205 guests