Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

Ask gaming related questions (AHK v1.1 and older)
cristalgrip
Posts: 36
Joined: 19 Feb 2023, 15:52

Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

03 Nov 2023, 10:48

Hello everyone,

I've been working on a script to automate a Candy Crush-like game, and I've hit a roadblock that I'm struggling to overcome. In the game, I need to build a matrix of the map based on different images, each with varying shades and characteristics.

To achieve this, I've been using the ImageSearch function in AutoHotkey. My approach has been to find the smallest "haystack" and "needle" possible for each element, all of which are around 10x10 pixels (the search area is slightly larger, just 1 pixel bigger than the image). While ImageSearch works, it's quite slow, and speed is crucial in a game like Candy Crush.

I've discovered that Gdip_ImageSearch is significantly faster than ImageSearch, but it's not producing the desired results. Even with the same parameters, Gdip_ImageSearch finds fewer images than ImageSearch.

Here's a snippet of my code for reference:

Code: Select all

fn_single_box_check(num_riga, num_col) {
	; ...
	; Coordinates and dimensions for the image search area
	coord_hat_x1 := 556
	coord_hat_y1 := 286
	pix_hat_x := 6
	pix_hat_y := 6
	X1 := coord_hat_x1 + (65 * index_col) - 1
	Y1 := coord_hat_y1 + (65 * index_riga) - 1
	X2 := X1 + pix_hat_x + 1
	Y2 := Y1 + pix_hat_y + 1

	; Using Gdip_ImageSearch
	im_hat := Gdip_CreateBitmapFromFile("C:\Users\" . utente . "\Desktop\hat.png")
	pBitMap := GDIP_BitmapFromScreen()
	search_im_res := Gdip_ImageSearch(pBitMap, im_hat, OutputVar, X1, Y1, X2, Y2, 18, Trans:="", SearchDirection:=1, Instances:=1, LineDelim:="`n", CoordDelim:=",")

	if (search_im_res > 0) {
		; Do something when image is found
	}

	; Using ImageSearch
	ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, *18 C:\Users\%utente%\Desktop\hat.png
	if (ErrorLevel = 0) {
		; Do something when image is found
	}
	; ...
}

I'm open to any suggestions or optimizations to make my image search more efficient while maintaining accuracy. Any help would be greatly appreciated. Thank you!
sakamoto
Posts: 7
Joined: 03 Oct 2022, 19:57

Re: Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

06 Nov 2023, 14:59

did you try using FindText function?
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

06 Nov 2023, 17:14

Your function parameters num_riga, num_col are never used?
The vars: index_col and index_riga are never assigned a value?

Conclusion: you have naming missmatches.
cristalgrip
Posts: 36
Joined: 19 Feb 2023, 15:52

Re: Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

13 Nov 2023, 12:36

sakamoto wrote:
06 Nov 2023, 14:59
did you try using FindText function?
I'm not sure how this can be helpful. :)
Xtra wrote: Your function parameters num_riga, num_col are never used?
The vars: index_col and index_riga are never assigned a value?

Conclusion: you have naming missmatches.
Yes and no. In the provided example, part of the code has been omitted, and your observation is somewhat correct. However, if that were the issue, no images would be found.
Furthermore, the issue arises from the disparity between the two functions that share the same variables.
The actual problem lies entirely within the Gdip_ImageSearch function or how it behaves in my system, of course.
Here the omitted code:

Code: Select all

	index_col:=num_col-1
	index_riga:=num_riga-1
[/code]
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

14 Nov 2023, 01:21

If the vars: index_col and index_riga are not set to global the function will not be able to see their values and will read as empty. This is due to functions having local scope by default.
cristalgrip
Posts: 36
Joined: 19 Feb 2023, 15:52

Re: Issue with Gdip_ImageSearch Not Finding All Images Detected by ImageSearch

18 Nov 2023, 13:44

Why they need to be global?

Code: Select all

fn_single_box_check(num_riga, num_col) {
	; ...
	; Coordinates and dimensions for the image search area
	index_col:=num_col-1
	index_riga:=num_riga-1
	coord_hat_x1 := 556
	coord_hat_y1 := 286
	pix_hat_x := 6
	pix_hat_y := 6
	X1 := coord_hat_x1 + (65 * index_col) - 1
	Y1 := coord_hat_y1 + (65 * index_riga) - 1
	X2 := X1 + pix_hat_x + 1
	Y2 := Y1 + pix_hat_y + 1

	; Using Gdip_ImageSearch
	im_hat := Gdip_CreateBitmapFromFile("C:\Users\" . utente . "\Desktop\hat.png")
	pBitMap := GDIP_BitmapFromScreen()
	search_im_res := Gdip_ImageSearch(pBitMap, im_hat, OutputVar, X1, Y1, X2, Y2, 18, Trans:="", SearchDirection:=1, Instances:=1, LineDelim:="`n", CoordDelim:=",")

	if (search_im_res > 0) {
		; Do something when image is found
	}

	; Using ImageSearch
	ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, *18 C:\Users\%utente%\Desktop\hat.png
	if (ErrorLevel = 0) {
		; Do something when image is found
	}
	; ...
}
But again, due to the fact the two functions are using the same parameters, I'm confident to exclude a problem with the value of the variables.

Thanks for the reply.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: slowwd and 82 guests