find image or color and count the number of times it finds it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

find image or color and count the number of times it finds it

Post by maxkill » 27 Jan 2023, 18:28

viewtopic.php?f=6&t=100838&p=504303#p504303

Was instructed to ask here so how do I actually use this? I have a shape that I would like to count but it's sometimes a bit obstructed from it's pure form, however the color is always the same and the color can be set not to be found anywhere else on screen except in the object/image that needs to be searched (maybe colorsearch could be used aswell or instead as a clump of certain colored pixels in any spot on screen).

User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: find image or color and count the number of times it finds it

Post by Chunjee » 27 Jan 2023, 19:01

I think ImageSearchAll() is awesome. Since it returns an array; just call the .count() of the array to see how many times it found the image.

for example:

Code: Select all

arr := ImageSearchAll("C:/testFolder/testImage.png")
msgbox, % arr.count()

https://www.autohotkey.com/docs/v1/lib/Object.htm#Count

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: find image or color and count the number of times it finds it

Post by maxkill » 27 Jan 2023, 19:39

Chunjee wrote:
27 Jan 2023, 19:01
I think ImageSearchAll() is awesome. Since it returns an array; just call the .count() of the array to see how many times it found the image.

for example:

Code: Select all

arr := ImageSearchAll("C:/testFolder/testImage.png")
msgbox, % arr.count()

https://www.autohotkey.com/docs/v1/lib/Object.htm#Count
thnx, how do I put it in the code?

Code: Select all

ImageSearchAll(imageFile, x1:=0, y1:=0, x2:="Screen", y2:="Screen", var:=0) {
	x2 := x2 = "Screen" ? A_ScreenWidth : x2
	y2 := y2 = "Screen" ? A_ScreenHeight : y2
	found := []
	y := y1
	loop {
		x := x1
	    lastFoundY := 0
		loop {
			ImageSearch, foundX, foundY, x, y, x2, y2, % "*" var " " needle1.png
			if (ErrorLevel = 2)
				return -1
			if !ErrorLevel {
				if (lastFoundY = 0 || lastFoundY = foundY) {
					found.Push({x: foundX, y: foundY})
					x := foundX + 1
					lastFoundY := foundY
				} else
					break
			}
		} until ErrorLevel
		y := lastFoundY + 1
	} until (x = x1) && ErrorLevel
	return found
}

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: find image or color and count the number of times it finds it

Post by boiler » 27 Jan 2023, 20:09

Those who are looking to help should note that the version of the function that OP posted above is NOT the actual function and cannot work because he edited it in his attempt to use it for his purpose. Go to the link he posted in the OP and get the actual function.

Basically, this thread is about walking OP through the basics of how to use functions, as I have already pointed to the documentation regarding functions in the other thread (that you don't edit the function itself and it doesn't do anything unless you call it), and he said that didn't help. I also pointed to an example of using the function in the other thread, which seems to be exactly what is being asked here, so it should be noted that showing a working script that calls the function (which I'll post again below for reference) isn't helpful to OP either. Perhaps someone else can take it from here.

Code: Select all

#Include ImageSearchAll.ahk
CoordMode, Pixel, Screen
Locations := ImageSearchAll("MyImage.png", 100, 150, 500, 300)
for Index, Location in Locations
	MsgBox, % "Location " Index " of " Locations.Count() ": (" Location.x "," Location.y ")"

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: find image or color and count the number of times it finds it

Post by maxkill » 28 Jan 2023, 12:35

boiler wrote:
27 Jan 2023, 20:09
Those who are looking to help should note that the version of the function that OP posted above is NOT the actual function and cannot work because he edited it in his attempt to use it for his purpose. Go to the link he posted in the OP and get the actual function.

Basically, this thread is about walking OP through the basics of how to use functions, as I have already pointed to the documentation regarding functions in the other thread (that you don't edit the function itself and it doesn't do anything unless you call it), and he said that didn't help. I also pointed to an example of using the function in the other thread, which seems to be exactly what is being asked here, so it should be noted that showing a working script that calls the function (which I'll post again below for reference) isn't helpful to OP either. Perhaps someone else can take it from here.

Code: Select all

#Include ImageSearchAll.ahk
CoordMode, Pixel, Screen
Locations := ImageSearchAll("MyImage.png", 100, 150, 500, 300)
for Index, Location in Locations
	MsgBox, % "Location " Index " of " Locations.Count() ": (" Location.x "," Location.y ")"
Any chance of helping me put the script as an example so I can try it? This code you use is beyond my knowledge in coding, I know only lower level coding in AHK.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: find image or color and count the number of times it finds it

Post by boiler » 28 Jan 2023, 12:42

That script is an example, and it’s only a few lines long, so I’m not sure what else to do to make a simpler script. Are you having issues knowing where to put your file name (even after I told you that you don’t change it in the function itself and you put it in the function call like the example script)? Are you having trouble defining you search rectangle? Have you saved the function itself (and only the function) to a separate script named ImageSearchAll.ahk so it can be included?

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: find image or color and count the number of times it finds it

Post by maxkill » 28 Jan 2023, 12:47

boiler wrote:
28 Jan 2023, 12:42
That script is an example, and it’s only a few lines long, so I’m not sure what else to do to make a simpler script. Are you having issues knowing where to put your file name (even after I told you that you don’t change it in the function itself and you put it in the function call like the example script)? Are you having trouble defining you search rectangle? Have you saved the function itself (and only the function) to a separate script named ImageSearchAll.ahk so it can be included?
Oh really, that's it? I thought it was some snippet of a code to be used in a full script. I will try it then. So all I need is this imagesearchall ahk downloaded in my scipts folder then take any? image of the image that I would like to find and it should work. What if there is some obstruction to the image and let's say it only finds a 80% match will it still work, or can we incoorporate some find color function if not. I will try it now and try to find the imagesearch apk file

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: find image or color and count the number of times it finds it

Post by maxkill » 28 Jan 2023, 12:53

boiler wrote:
28 Jan 2023, 12:42
text
I tried it now using your script

Code: Select all

#Include ImageSearchAll.ahk
CoordMode, Pixel, Screen
Locations := ImageSearchAll("needle1.png", 100, 150, 500, 300)
for Index, Location in Locations
	MsgBox, % "Location " Index " of " Locations.Count() ": (" Location.x "," Location.y ")"
and the imagesearch ahk i found is

Code: Select all

ImageSearchAll(x1, y1, x2, y2, filename, var := 0, area := "All")
{   ; Updated: 08/22/2021
    static path := A_ScriptDir . "\ImageSearch\", ext := ".png"
	; Found coordinates are returned as a simple array of coordinate pairs
	; each coordinate pair is an associative array with keys 'x' and 'y'
	found := [], imageFile := "*" . var . A_Space . path . filename . ext
	loop {
		x := x1, lastFoundY := 0
		loop {
			ImageSearch, foundX, foundY, x, y1, x2, y2, % imageFile
			if (ErrorLevel = 2)
				return -1
			if !ErrorLevel {
				if (lastFoundY = 0 || lastFoundY = foundY)
					(found.Push({x: foundX, y: foundY}), x := ++foundX, lastFoundY := foundY)
				else
                    break
			}
		} until (ErrorLevel || area = "Col")
		y1 := ++lastFoundY
	} until (((x = x1) && ErrorLevel) || area = "Row")
	return found
}
and I named it ImageSearchAll.ahk

it won't find a single instance of the image searched :?: :|

edit; maybe I need to change the coordinates to search the whole screen I what are the commands for full screen search again for this

I found this line
ImageSearch, FoundX, FoundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, ImageFile
but how do I put that into your script?

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: find image or color and count the number of times it finds it

Post by boiler » 28 Jan 2023, 13:28

maxkill wrote: Oh really, that's it? I thought it was some snippet of a code to be used in a full script. I will try it then. So all I need is this imagesearchall ahk downloaded in my scipts folder then take any? image of the image that I would like to find and it should work. What if there is some obstruction to the image and let's say it only finds a 80% match will it still work, or can we incoorporate some find color function if not. I will try it now and try to find the imagesearch apk file
It can't work if there is only 80% match. It's using ImageSearch, so it has the same constraints.

maxkill wrote: edit; maybe I need to change the coordinates to search the whole screen I what are the commands for full screen search again for this
I found this line
ImageSearch, FoundX, FoundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, ImageFile
but how do I put that into your script?
Yes, like I said, you have to put in your own coordinates. The example was searching a specific part of the screen. You put the coordinates of your search rectangle into your function call, like this:

Code: Select all

Locations := ImageSearchAll("needle1.png", 0, 0, A_ScreenWidth, A_ScreenHeight)

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: find image or color and count the number of times it finds it

Post by maxkill » 28 Jan 2023, 15:15

boiler wrote:
28 Jan 2023, 13:28
text
Thanks for the help! Unfortunately it still finds nothing even when image is 100% visible and unobstructed. Do you have some backup 'search for blobs of pixels for example 50 pixels together of a certain color and count it instead? It really doesn't need to find the exact image just count arrows of a certain color ..

To summarize what I did is save the imagesearch ahk file as stated then use this code (double clickin on this script to activate it)

Code: Select all

#Include ImageSearchAll.ahk
CoordMode, Pixel, Screen
Locations := ImageSearchAll("needle1.png", 0, 0, A_ScreenWidth, A_ScreenHeight)
for Index, Location in Locations
	MsgBox, % "Location " Index " of " Locations.Count() ": (" Location.x "," Location.y ")"
and have the image to search in same folder as both of image ahk and search ahk, all files in same folder

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: find image or color and count the number of times it finds it

Post by boiler » 28 Jan 2023, 23:40

Try it with a simple image that you know it has to be able to find, like a pure white block in a Notepad window. Sometimes when people are new to this and try to find more complicated images, they don't realize that the background around the image matters.

But even before that, just get a simple ImageSearch script working to make sure you have all the mechanics right.

Post Reply

Return to “Ask for Help (v1)”