Image Search Prio

Ask gaming related questions (AHK v1.1 and older)
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Image Search Prio

21 Apr 2022, 02:11

Code: Select all

ImageSearch, X, Y, 746, 148, 1802, 1205, *20 Healthbar.bmp
if !ErrorLevel {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
}
i got this little code fired i need a possibility to always prio whats closer to the middle of the screen, also is there a possibility to search an circular area for images from middle of the screen. THY
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

21 Apr 2022, 06:06

i mean if i have 5 images found it shoud always prio whats closer to the middle of the screen
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

21 Apr 2022, 06:37

ImageSearch uses a rectangular area to find a single image, but you can specify the coordinates according to any formula that you may want. If you are searching multiple times, you could use a loop where the coordinates are based on some computation of your choosing. If you search five times in different areas and then find five images, you can create your own formula to specify which image should be used, as you will have the coordinates of each match. In essence, those are arbitrary decisions that you make yourself, especially since the returned coordinates are those of the top left corner of the match. boiler has posted a script that can find all matching images, so you may be able to adapt that to your liking. If you decide to create your own approach, then I would start with two image searches, get those two matching results, and then write a formula to decide which match is better. After you have a working approach with two matches, scaling it up to any number of matches would likely be straightforward. To find multiple matches, the general approach is to find one match, and then "advance" the target (search) coordinates so that you can search again in a different area.

viewtopic.php?style=2&t=99250&p=440789#p440801
viewtopic.php?p=391569#p391569
viewtopic.php?f=76&t=83792
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

21 Apr 2022, 07:26

any chance you can make an example whit 2 images on my variant?:D
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

21 Apr 2022, 08:12

This will find all of the matches.

Code: Select all

image = %A_ScriptDir%\Healthbar.bmp
Global found := []
If !FileExist(image) {
 MsgBox, 48, Error, File not found. Aborting.`n`n%image%
 Return
} Else CoordMode, Pixel
CoordMode, Mouse
For each, place in imgFindAll(image, 0, 0, A_ScreenWidth, A_ScreenHeight, imgSize(image)) {
 MouseMove, place.x, place.y
 SoundBeep, 1500, 20
 Sleep, 400
}
ExitApp

imgFindAll(image, x1, y1, x2, y2, size) {
 ImageSearch, x, y, x1, y1, x2, y2, %image%
 If ErrorLevel
  Return
 found.Push({x: x, y: y})
 imgFindAll(image, x + size.w, y         , x2, y + size.h, size)
 imgFindAll(image, 0         , y + size.h, x2, y2        , size)
 Return found
}

imgSize(img) { ; Returns an array indicating the image's width (w) and height (h),
               ; obtained from the file's properties
               ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
 SplitPath, img, fn, dir
 objShell := ComObjCreate("Shell.Application")
 objFolder := objShell.NameSpace(dir)
 objFolderItem := objFolder.ParseName(fn)
 scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
 Return {w: scale.1, h: scale.2}
}
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

22 Apr 2022, 00:58

Honestly i tryied to bind it in my script bud i have no idea how it shoud work. It looks like if im running it wrong also the exit app in the code always forces to close the app:D. Also you shoud know im not a programmer at all so im still learning and i may dont see something obvisious stuff. How woud look like an example when i press space the app searches all images compares the middle of the screen via pythagors whit img position and targets the lowest value. Cheers
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

22 Apr 2022, 06:25

Before you modify the script, try the script by itself, with no other code. That will tell you whether you have a working starting point.
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

22 Apr 2022, 06:54

When i open your code it makes a peep and close itself;D

Code: Select all

~Space::
While, GetKeyState("Space","P") {
ImageSearch, X, Y, 1050, 325, 1525, 645, *20 Healthbar.bmp
If (ErrorLevel == 0) {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
	sleep, 20
	send, w
	sleep, 20
	send, e
	sleep, 20
	send, q
}
else if (ErrorLevel ==1) {
ImageSearch, X, Y, 895, 180, 1630, 765, *20 Healthbar.bmp
If (ErrorLevel == 0) {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
	sleep, 20
	send, w
	sleep, 20
	send, e
	sleep, 20
	send, q
}
else if (ErrorLevel ==1) {
ImageSearch, X, Y, 550, 40, 1980, 1145, *20 Healthbar.bmp
If (ErrorLevel == 0) {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
	sleep,, 20
	send, w
	sleep, 20
	send, e
	sleep, 20
	send, q
}
}
}
}
Thats for now a solution try not particulary happy whit it :facepalm:
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

22 Apr 2022, 06:59

If it beeped, then I guess it worked, right? You can display the search's ErrorLevel if needed, to confirm.

The script is a demonstration. It finds the images, and then moves the mouse to each of them. You can adjust according to what you need.
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

22 Apr 2022, 07:14

Well it works whit the Image in the Folder bud not whit ingame
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

22 Apr 2022, 07:31

I'm not sure what you mean by that, but if the image in your folder is found only outside the game, then perhaps your game is showing a different image.
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

22 Apr 2022, 08:01

i need *20 to add this variable at the image search to have abit of a varianz while search
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Image Search Prio

22 Apr 2022, 08:01

dunno where to add that:D
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Image Search Prio

22 Apr 2022, 09:43

Just like in your original script.

Code: Select all

ImageSearch, x, y, x1, y1, x2, y2, *20 %image%

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 105 guests