How can I use ImageSearch to find and click on all instances of an image?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gtabor
Posts: 3
Joined: 12 Jun 2021, 08:33

How can I use ImageSearch to find and click on all instances of an image?

12 Jun 2021, 08:48

Hi there,

I have a very boring task which I am hoping to automate with AHK, but I'm having a little trouble with the programming aspect. I have opened image file (full-screen) with a bunch of small circles on it, and I am trying to hover over the center of each circle and press Ctr + V (until every circle is done).

There is one aspect that is a little strange, though: because the image is in JPG format, each circle is aliased slightly differently. The difference is so slight, that you wouldn't know unless you zoomed in really far. Would this cause a problem for detection?

I think ImageSearch might work, but can it find every instance of the circle on the screen?

Thank you so much!
User avatar
mikeyww
Posts: 26884
Joined: 09 Sep 2014, 18:38

Re: How can I use ImageSearch to find and click on all instances of an image?

12 Jun 2021, 09:13

Yes, transparency issues can matter in searching. The key with the search is that your reference image specified in the command must exactly match a visible area (or use the hue variation option). Short of a variation, a "difference so slight" is a difference.

Here is a "search all" function from boiler.

https://www.autohotkey.com/boards/viewtopic.php?p=391569#p391569
gtabor
Posts: 3
Joined: 12 Jun 2021, 08:33

Re: How can I use ImageSearch to find and click on all instances of an image?

12 Jun 2021, 10:30

Hi mikeyww,

Thank you for your help! If I'm trying to search the whole screen would I call the function like the code below?

Code: Select all

4::
ImageSearchAll("C:\Users\Mi\Documents\example.jpg", , , , , 50)
Return
Also, how would I use its returned value to hover and click on each image found?

Thank you again!
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: How can I use ImageSearch to find and click on all instances of an image?

12 Jun 2021, 11:32

https://docs.opencv.org/3.4/d4/d70/tutorial_hough_circle.html
If it is difficult for You to translate it to ahk, then it will be easier to write code on python.
User avatar
mikeyww
Posts: 26884
Joined: 09 Sep 2014, 18:38

Re: How can I use ImageSearch to find and click on all instances of an image?

12 Jun 2021, 13:06

Here is one example.

Code: Select all

Global places := []
CoordMode, Mouse
CoordMode, Pixel
image = %A_ScriptDir%\test.png
SoundBeep, 1500
imgFindAll(image, 500, 500, 800, 800)
SoundBeep, 1000
If !places.Count() {
 MsgBox, 48, Nothing found, Nothing found.
 Return
}
For k, v in places {
 MouseClick,, v.1, v.2
 Sleep, 300
}

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

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, wpulford and 376 guests