PixelGetColor taking 3 seconds for 64 searches?

Ask gaming related questions (AHK v1.1 and older)
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

PixelGetColor taking 3 seconds for 64 searches?

19 Jan 2017, 13:53

From reading some threads (https://autohotkey.com/board/topic/1168 ... ing-wrong/), this sounds like an unusually long time. Other people were doing 2000 reads in fractions of a second. Anyone know why this could be? Using Win7 x64, AHK v1.1.24.01, SciTE4AutoHotkey Version 3.0.06.01. I've watched my CPU resources during the search and they stay roughly at 20-30% (which is what they are before I start the search) The game I'm searching is running in windowed mode.

Code: Select all

; Coords within the window for the bottom left of the board (0,0).
global SG_ORIGIN_X=290
global SG_ORIGIN_Y=769
; Offset to measure within the square.
global SG_OFFSET_X=43		; from the left
global SG_OFFSET_Y=72		; from the top
; Scans Grid into array for evaluation later
scanGrid(newBoard, gemColorsHex, gemColorsRGB)
{
	startTime := A_TickCount
	updateStatusBox("Scanning grid...")
	; Loop X then Y: get a full column from the bottom then move to the next
	Loop, 8
	{
		x := A_Index
		px := xCoordToPixel(A_Index)
		Loop, 8
		{
			y := A_Index
			py := yCoordToPixel(A_Index)
			PixelGetColor, sg_color, %px%, %py%, RGB		; gets pixel color as an RGB Hex number
			gemColorsHex[x, y] := sg_color
		}
	}
	updateStatusBox("Grid scan took " (A_TickCount - startTime))
}
yCoordToPixel(y)
{
	return Round( SG_ORIGIN_Y - (y-1) * SG_SIZE_Y - SG_OFFSET_Y )
}

; Convert a board square x coord to a screen x coord.
xCoordToPixel(x)
{
	return Round ((x-1) * SG_SIZE_X + SG_ORIGIN_X + SG_OFFSET_X )
}
updateStatusBox(newStatus)
{
	Gui, GoW_Helper: Default
	GuiControlGet, contents,, StatusText
	GuiControl,, StatusText, %  newStatus "`n" contents
}
Last edited by Entropy42 on 19 Jan 2017, 14:44, edited 1 time in total.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: PixelGetColor taking 3 seconds for 64 searches?

19 Jan 2017, 14:19

After using ahk for a couple of years I have found out that
  • Using PixelGetColor, ImageSearch or PixelSearch on a remote desktop application, is way slower compared to using same/similar code in a windows program that runs on your local computer.
  • If searching inside a box (like yours 8x8), you want to use PixelSearch, , , , , , , , Fast because it is substantially faster. Also on a remote desktop application.
  • In your particular code (even if assuming that is a part of a larger script), you don't exit the loop even if the color is found at the first pixel in the search rectangle. I just point it out in case the intention is to find one pixel with matching color and not to actually count the number of matching pixels - but I can't know that since you have reduced the code that much.
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: PixelGetColor taking 3 seconds for 64 searches?

19 Jan 2017, 14:42

Thanks for the suggestions, but I am not running it on a remote desktop. I'm just looking at 64 specific pixels inside a 1243x776 game window. I just mentioned that it was running in windowed mode, rather than fullscreen, because I have read in the past that PixelGetColor can fail in fullscreen.

My real code actually just saves the color value to an array:
gemColorsHex[x, y] := sg_color
So, I'm not actually looking for a specific color somewhere on the screen, I'm just trying to read out the color value at 64 specific locations. I then have other code later that interprets those colors and figures out information from what color I found in each location. But I've run the code above, with no other functions, and it takes nearly 3 seconds. For reference the coordinate conversion functions are simple, I'll add them to OP for completeness:
yCoordToPixel(y)
{
return Round( SG_ORIGIN_Y - (y-1) * SG_SIZE_Y - SG_OFFSET_Y )
}

; Convert a board square x coord to a screen x coord.
xCoordToPixel(x)
{
return Round ((x-1) * SG_SIZE_X + SG_ORIGIN_X + SG_OFFSET_X )
}
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: PixelGetColor taking 3 seconds for 64 searches?

19 Jan 2017, 15:33

Haven't tried myself, but I recommend you to take a look at CGDipSnapShot posted by evilC here:
https://autohotkey.com/boards/viewtopic.php?t=5682
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: PixelGetColor taking 3 seconds for 64 searches?

23 Jan 2017, 15:49

Thank you very much for the suggestion! Using CGDipSnapShot instead of PixelGetColor, my search time went from 3 sec to 100 ms.
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: PixelGetColor taking 3 seconds for 64 searches?

23 Jan 2017, 23:35

Unfortunately it seems like it also has a memory leak? I watched in Windows Explorer and every iteration of my loop that makes a new snapshot and reads out the 64 values was increasing my AHK memory usage by about 4 MB, and never going back down until I restart the script.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Duker and 47 guests