MultipleImageSearch

Post your working scripts, libraries and tools
Taktyk_zza_plotu
Posts: 1
Joined: 24 Dec 2018, 08:45

MultipleImageSearch

24 Dec 2018, 09:12

Small function to search a region of the screen for an image and all it duplications.

Code: Select all

F10::
{
	A:=MultipleImageSearch(13,151,712,850,27,"*Trans0xFFFFFF","C:\ahk\img.bmp")
	;A = 2D array [[x],[y]] with coordinates of find images
}
return

MultipleImageSearch(X1,Y1,X2,Y2,img_lenght,options,dir)
{
	xy:=[[],[]]
	xs:=[]
	loop
	{
		i:=A_Index-1
		if (i=0)
		{
			Xn:=X1
			Yn:=Y1
			Xp:=X2
		}
		else if (i > xy[1].MaxIndex())
		{
			Break
		}
		else
		{
			Xn:=xs[i]
			Yn:=xy[2,i]+1
			Xp:=xy[1,i]+img_lenght-1
		}
		loop
		{
			ImageSearch, x, y, Xn, Yn, Xp, Y2,%options% %dir%
			if (ErrorLevel = 0)
			{
				xy[1].Push(x)
				xy[2].Push(y)
				xs.Push(Xn)
				Xn:=x+1
				Yn:=y
			}
			else
			{
				Break
			}
		}
	}
	return xy
}
godofOOF
Posts: 24
Joined: 22 Dec 2018, 06:03

Re: MultipleImageSearch

28 Dec 2018, 20:00

how does this work?
carno
Posts: 204
Joined: 20 Jun 2014, 16:48

Re: MultipleImageSearch

30 Dec 2018, 15:40

Please offer an example. ;)
swagfag
Posts: 2905
Joined: 11 Jan 2017, 17:59

Re: MultipleImageSearch

30 Dec 2018, 21:55

run this(put imgs in script folder):

Code: Select all

#NoEnv
#SingleInstance Force
SetBatchLines -1
SetTitleMatchMode 2
SetWorkingDir %A_ScriptDir%
CoordMode Pixel

Gui, New, +AlwaysOnTop
Gui, Add, Picture, , sample.png
Gui, Add, Text, +hwndstatusText, now searching:
Gui, Add, Text, x+m yp w200 +hwndsearchText, nothing yet
Gui, Add, Button, xm gSearch +hwndstartBtn, start searching
Gui, Add, Button, x+m yp gContinueSearch Disabled +hwndcontinueBtn, continue searching
Gui, Show

Return

Search:
	GuiControl, Disable, %startBtn%
	Res := MultipleImageSearch(0, 0, A_ScreenWidth, A_ScreenHeight, 100, "", "needle.png")
	GuiControl, Enable, %startBtn%
	GuiControl, Disable, %continueBtn%
	GuiControl, Text, %searchText%, finished
Return

ContinueSearch:
	Pause Off
	GuiControl, Disable, %continueBtn%
Return

GuiClose:
	ExitApp

showHideRect(args*) {
	static HWND := ""

	if HWND
		if (args.Count() == 0)
		{
			Gui %HWND%: Destroy
			HWND := ""
			return
		}
		else
			Gui %HWND%: Destroy

	x1 := args[1] ? args[1] : 0
	y1 := args[2] ? args[2] : 0
	x2 := args[3] ? args[3] : A_ScreenWidth
	y2 := args[4] ? args[4] : A_ScreenHeight
	thickness := args[5] ? args[5] : 1
	alpha := args[6] ? args[6] : 254
	color := args[7] ? args[7] : "red"

	w := x2 - x1
	h := y2 - y1

	if color is number
		color := Format("0x{:X}", color)


	Gui New, +E0x20 +AlwaysOnTop +ToolWindow -Caption +HwndHWND
	Gui %HWND%:Color, % color
	Gui %HWND%:Show, % Format("x{} y{} w{} h{} NoActivate", x1, y1, w, h)
	WinSet, Region, % Format("0-0 {1:}-0 {1:}-{2:} 0-{2:} 0-0   {3:}-{3:} {4:}-{3:} {4:}-{5:} {3:}-{5:} {3:}-{3:}", w, h, thickness, w - thickness, h - thickness), ahk_id %HWND%
	WinSet, Trans, % alpha, ahk_id %HWND%

	return HWND
}

MultipleImageSearch(X1,Y1,X2,Y2,img_len,options,dir) {
	global continueBtn, searchText, statusText

	xy := [[]  ; 1
		 , []] ; 2

	xs := []

	loop
	{
		i := A_Index-1 ; zero based index

		if (i=0) ; first iteration
		{
			x_new := X1
			y_new := Y1
			Xp := X2
		}
		else if (i > xy[1].MaxIndex()) ; i bigger than xy.Count()
			Break
		else ; not 1st iteration && i not bigger than .Count()
		{
			x_new := xs[i] ;
			y_new := xy[2, i] + 1 ;go next pixel line down
			Xp := xy[1, i] + img_len - 1 ;
		}

		loop
		{

			showHideRect(x_new, y_new, Xp, Y2, 2)
			GuiControl, Text, %statusText%, now searching
			GuiControl, Text, %searchText%, % Format("({}, {}, {}, {})", x_new, y_new, Xp, Y2)
			GuiControl, Enable, %continueBtn%
			Pause
			showHideRect()
			Sleep 100

			ImageSearch, x, y, x_new, y_new, Xp, Y2, %options%%dir%
			if (ErrorLevel = 0)
			{
				showHideRect(x, y, x + 100, y + 100, 2, "", "lime")
				GuiControl, Text, %statusText%, found needle!
				GuiControl, Text, %searchText%, % Format("({}, {}, {}, {})", x_new, y_new, Xp, Y2)
				GuiControl, Enable, %continueBtn%
				Pause
				showHideRect()
				Sleep 100

				xy[1].Push(x)
				xy[2].Push(y)

				xs.Push(x_new)

				x_new := x+1
				y_new := y
			}
			else
				Break
		}
	}
	return xy
}
sample.png
sample.png (7.24 KiB) Viewed 580 times
needle.png
needle.png (420 Bytes) Viewed 580 times

Return to “Scripts and Functions”

Who is online

Users browsing this forum: enrica and 54 guests