Imagesearching multiple targets (please help!)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zKade
Posts: 8
Joined: 01 Nov 2020, 11:46

Imagesearching multiple targets (please help!)

28 Nov 2020, 22:12

I am trying to use imagesearch to find the same image multiple times quickly.

For example I have a box that could have the same image appear 30 times in different positions I want to index the positions of every single instance

My initial thought is to do some math when the imagesearch returns the first image to run 4 more searches around the area the first image was found effectively excluding the successful scan(s) from the next scan(s) but when I'm working with so many images I feel like this would be cumbersome

This scenario has been the bane of my existence and has been on my mind for over a year now and no matter how much I learn I can't think of (or Google) an efficient way to go about this.
:headwall:
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Imagesearching multiple targets (please help!)

28 Nov 2020, 22:27

ImageSearch starts at the top row of the designated region, and moves downward. If there is more than one match, the one closest to the top will be found. Therefore, start searching at the top left corner of the screen; whenever an image is found, there are two more regions to search: to the right of the found image (x+width, y, A_ScreenWidth, y+height), and below the found image (0, y+height, A_ScreenWidth, A_ScreenHeight). Those coordinates are known, because you know the image's dimensions (width x height). You keep doing that-- search to the right, and search below-- until you reach the bottom right corner.

This method will find every instance of the image.

The script is already written and posted somewhere; search the forum if you like, or it might even be faster just to write it again. Below is one example of what I described.

Code: Select all

image = e:\data\temp2\t2.png
Global places := []
CoordMode, Pixel
CoordMode, Mouse
imgFindAll(image, 0, 0, A_ScreenWidth, A_ScreenHeight)
If places.Count() {
 size := imgSize(image)
 Loop, % places.Count() {
  SoundBeep, 1500, 20
  MouseMove, xpos(places[A_Index]) + size.w / 2, ypos(places[A_Index]) + size.h / 2
  Sleep, 400
 }
}
ExitApp

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}
}

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

xpos(pos) { ; This function provides the x-value of the combined position value; it is a remainder
 Return Mod(pos, 65536)
}

ypos(pos) { ; This function provides the y-value of the combined position value
 Return Floor(pos/65536)
}

pos(x,y) { ; This routine provides a single number that is a function of x and y coordinates
 Return 65536*y+x
}
nacken012
Posts: 92
Joined: 22 Jul 2016, 14:39

Re: Imagesearching multiple targets (please help!)

29 Nov 2020, 02:45

Ich mache das mit gdip

Code: Select all

#SingleInstance, Ignore
#NoEnv
SetBatchLines, -1
ListLines Off
SendMode Input
#Persistent
#Include, D:\Tools\AutoHotkey 1.1\Gdip_All.ahk
#Include D:\Tools\AutoHotkey 1.1\Gdip_ImageSearch.ahk

pToken := Gdip_Startup()
winName := "ahk_exe chrome.exe"
clientW := 1920 ; set your client area width
clientH := 1080 ; set your client area height
WinGetPos, x, y, w, h, %winName%
winBorder := (w-clientW)/2
x := x+winBorder
y := y+(h-clientH-winBorder)
snap := Gdip_BitmapFromScreen(x "|" y "|" clientW "|" clientH)
Gdip_SaveBitmapToFile(snap, "Bilder/NeueInsel/OrangenShot.bmp")
Sleep, 250
Gdip_DisposeImage(snap)
bmpHaystack := Gdip_CreateBitmapFromFile("Bilder/NeueInsel/OrangenShot.bmp")
Sleep, 250
bmpNeedle := Gdip_CreateBitmapFromFile("Bilder/NeueInsel/Orangen.bmp")
Sleep, 200

RET := Gdip_ImageSearch(bmpHaystack,bmpNeedle,OutputList,0,0,0,0,40,"",1,0)
Sleep, 100
Sleep, 100
Gdip_DisposeImage(bmpHaystack)
Gdip_DisposeImage(bmpNeedle)
Gdip_Shutdown(gdipToken)


FileDelete, c:\temp\Orangenbaum.txt
Sleep, 100
fileappend,%OutputList%, c:\temp\Orangenbaum.txt

Sleep, 300
MouseMove, 1900, 10, 0
Sleep, 300

textdatei = c:\temp\Orangenbaum.txt
Loop, read, %textdatei%
    {
    ZeilenZahl:=A_Index
    }
	
Loop, %ZeilenZahl%
{

FileReadLine, AusgabeVar, c:\temp\Orangenbaum.txt, A_Index
sleep, 20

meineKoordinaten := StrSplit(AusgabeVar, ",")
MausX := meineKoordinaten[1]
MausY := meineKoordinaten[2]
Sleep, 20

;------------------ Orangenbaum anklicken ---------------------------
MouseMove, %MausX%, %MausY%, 0
Sleep, 35

SendEvent, {Click, Rel 5, 10, 0}
Click, Left, 1
Sleep, 25
}
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Imagesearching multiple targets (please help!)

29 Nov 2020, 09:04

Very nice! Danke schön, @nacken012!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Gorgrak, Leonardo_Portela, Sean24 and 122 guests