How ImageSearch Works (Difference with PixelSearch?) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
dntkdlsqjawns
Posts: 7
Joined: 02 Aug 2017, 07:30

How ImageSearch Works (Difference with PixelSearch?)

07 Aug 2017, 21:31

Hi
I want to know how imagesearch of ahk works (algorithm) , but I can't find the source.... :cry:
Is there any place that described the way or actual codes of ahk commands?

I am just thinking that imagesearch would work in the way below:
1. get the pixelcolor of each pixels (both needle image and haystack)
2. compare the values of pixelcolor and find the image (maybe using algorithm like kmp...)

Am I right?
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: How ImageSearch Works (Difference with PixelSearch?)

07 Aug 2017, 22:36

How does the sun set? How is a rainbow made? How does the positrac rear-end on a Plymouth work?

It just does.
dntkdlsqjawns
Posts: 7
Joined: 02 Aug 2017, 07:30

Re: How ImageSearch Works (Difference with PixelSearch?)

07 Aug 2017, 23:37

neomulemi6 wrote:How does the sun set? How is a rainbow made? How does the positrac rear-end on a Plymouth work?

It just does.
The sun sets because the earch rotates. Rainbow is made because of light reflection.
Positrac rear-end of a Plymouth will work as the engineer designed.
Everything doesn't "just" happen.
All codes are made with original authors' efforts and all codes work in a way that author intended.

By saying what you just said, you're not only showing your ignorance, but also ignoring many people's efforts right now.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How ImageSearch Works (Difference with PixelSearch?)  Topic is solved

08 Aug 2017, 03:56

The source code is usually in cpp files e.g. script2.cpp contains ImageSearch:

Code: Select all

ResultType Line::ImageSearch(int aLeft, int aTop, int aRight, int aBottom, LPTSTR aImageFile)
// Author: ImageSearch was created by Aurelian Maga.
...
Gdip_ImageSearch is useful for searching an image stored in a variable, versus searching on the current screen.
AutoHotkey/Gdip_ImageSearch.ahk at master · MasterFocus/AutoHotkey · GitHub
https://github.com/MasterFocus/AutoHotkey/blob/master/Functions/Gdip_ImageSearch/Gdip_ImageSearch.ahk

I've added more links to the 'SOURCE CODE' section here:
best utilities + best AutoHotkey scripts (+ useful tips) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28149

See also:
AutoHotkey_L/script2.cpp at master · Lexikos/AutoHotkey_L · GitHub
https://github.com/Lexikos/AutoHotkey_L/blob/master/source/script2.cpp#L4722
How does AHK's ImageSearch work? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=26857
ImageSearch with transparency tolerance? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33208

Some other 'how does it work?' questions:
jeeswg's homepage - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=17&t=30931&p=144442#p144442
Last edited by jeeswg on 17 Mar 2019, 15:53, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: How ImageSearch Works (Difference with PixelSearch?)

08 Aug 2017, 15:19

dntkdlsqjawns wrote:
neomulemi6 wrote:How does the sun set? How is a rainbow made? How does the positrac rear-end on a Plymouth work?

It just does.
The sun sets because the earch rotates. Rainbow is made because of light reflection.
Positrac rear-end of a Plymouth will work as the engineer designed.
Everything doesn't "just" happen.
All codes are made with original authors' efforts and all codes work in a way that author intended.

By saying what you just said, you're not only showing your ignorance, but also ignoring many people's efforts right now.
Or I could have been quoting a very funny movie. Who knows.
User avatar
Cerberus
Posts: 169
Joined: 12 Jan 2016, 15:46

Re: How ImageSearch Works (Difference with PixelSearch?)

09 Aug 2017, 00:11

jeeswg wrote:The source code is usually in cpp files e.g. script2.cpp contains ImageSearch:

Code: Select all

ResultType Line::ImageSearch(int aLeft, int aTop, int aRight, int aBottom, LPTSTR aImageFile)
// Author: ImageSearch was created by Aurelian Maga.
...
Gdip_ImageSearch is useful for searching an image stored in a variable, versus searching on the current screen.
AutoHotkey/Gdip_ImageSearch.ahk at master · MasterFocus/AutoHotkey · GitHub
https://github.com/MasterFocus/AutoHotk ... Search.ahk

I've added more links to the 'SOURCE CODE' section here:
best utilities + best AutoHotkey scripts (+ useful tips) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28149
...
Interesting! So do you think this Gdip Image Search could be faster than the regular ImageSearch? And can it output the coördinates of where it found the image? I didn't see that option at a glance.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How ImageSearch Works (Difference with PixelSearch?)

09 Aug 2017, 05:55

(Gdip_ImageSearch stores the results in a ByRef variable, 'OutputList'.)

From some tests it seems that ImageSearch is slightly faster, when the same thing is done as Gdip_ImageSearch, i.e. printscreen and perform one search (although maybe there is some code change that would result in Gdip_ImageSearch being faster).

However, Gdip_ImageSearch, has various options that can be set, allowing it to do things that ImageSearch can't, e.g. specify a search direction, and retrieve multiple results. Or to apply further image searches to an image already in memory, rather than performing a printscreen each time. Or to allow to you search an image that isn't the screen (e.g. from a file, from the clipboard, generated via AHK).

One thing I used Gdip_ImageSearch for was to OCR a timer, I would take multiple screenshots of a timer, until I had two images with exactly the same contents twice, i.e. to confirm that the snapshot was of an actual timestamp, and not a timestamp in transition, when the second was changing.

Code: Select all

q:: ;benchmark tests - ImageSearch v. Gdip_ImageSearch
pToken := Gdip_Startup()
vImgW := 10, vImgH := 10
pBitmapNeedle := Gdip_CreateBitmap(vImgW, vImgH)
G := Gdip_GraphicsFromImage(pBitmapNeedle)
pBrush := Gdip_BrushCreateSolid(0xffff0000)
Gdip_FillRectangle(G, pBrush, 0, 0, vImgW, vImgH)
hBitmapNeedle := Gdip_CreateHBITMAPFromBitmap(pBitmapNeedle)
vNum := 100

;SplashImage, % "HBITMAP:" hBitmapNeedle, B ;B: borderless
vOutput := ""

CoordMode, Pixel, Screen
vTickCount1 := A_TickCount
Loop, % vNum
	ImageSearch, vPosX, vPosY, 0, 0, % A_ScreenWidth, % A_ScreenHeight, % "HBITMAP:*" hBitmapNeedle
vTickCount2 := A_TickCount
vOutput .= (vTickCount2-vTickCount1) "`t" vPosX " " vPosY "`r`n"
CoordMode, Mouse, Screen
;MouseMove, % vPosX, % vPosY

vTickCount3 := A_TickCount
Loop, % vNum
{
	pBitmap := Gdip_BitmapFromScreen("0|0|" A_ScreenWidth "|" A_ScreenHeight)
	;note: coordinates are XYRB not XYWH
	Gdip_ImageSearch(pBitmap, pBitmapNeedle, vOutputList, 0, 0, A_ScreenWidth, A_ScreenHeight)
}
vTickCount4 := A_TickCount
oTemp := StrSplit(vOutputList, ",")
vPosX := oTemp.1, vPosY := oTemp.2, oTemp := ""
vOutput .= (vTickCount4-vTickCount3) "`t" vPosX " " vPosY "`r`n"
oTemp := ""
CoordMode, Mouse, Screen
;MouseMove, % vPosX, % vPosY

vTickCount5 := A_TickCount
pBitmap := Gdip_BitmapFromScreen("0|0|" A_ScreenWidth "|" A_ScreenHeight)
Loop, % vNum
{
	;note: coordinates are XYRB not XYWH
	Gdip_ImageSearch(pBitmap, pBitmapNeedle, vOutputList, 0, 0, A_ScreenWidth, A_ScreenHeight)
}
vTickCount6 := A_TickCount
oTemp := StrSplit(vOutputList, ",")
vPosX := oTemp.1, vPosY := oTemp.2, oTemp := ""
vOutput .= (vTickCount6-vTickCount5) "`t" vPosX " " vPosY "`r`n"
oTemp := ""
CoordMode, Mouse, Screen
;MouseMove, % vPosX, % vPosY

Gdip_DeleteBrush(pBrush)
Gdip_DeleteGraphics(G)
DeleteObject(hBitmapNeedle)
Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(pBitmapNeedle)
Gdip_Shutdown(pToken)

Clipboard := vOutput
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Cerberus
Posts: 169
Joined: 12 Jan 2016, 15:46

Re: How ImageSearch Works (Difference with PixelSearch?)

09 Aug 2017, 08:31

Ah, OK, good to know! That sounds like a good use.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], mikeyww, Redbird and 50 guests