AutoHotkey Community

It is currently May 26th, 2012, 9:31 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: September 19th, 2008, 1:36 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
I am wondering why I have to specify such a high variation level, so I wrote a test case


Code:
; example of using imagesearch


; bring up calculator
run, calc

WinWaitActive, Calculator, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Calculator to start.
    return
}

; move the calculator to a known location
winmove, 200, 200

; copy the calculator image to the clipboard
send, {alt down}{printscreen}{alt up}

; save the calculator image to a file
run, mspaint

winwaitactive, untitled - Paint, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Paint to start.
    return
}

send, {ctrl down}v{ctrl up}

send, {ctrl down}s{ctrl up}

winwaitactive, Save As, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Save As to start.
    return
}

filedelete, %a_ScriptDir%\calc.png
send, %a_ScriptDir%\calc.png

send, {enter}

send, {alt down}
send, f
send, x
send, {alt up}

; ensure that the calculator image file exists
ifnotexist, %a_ScriptDir%\calc.png
{
  msgbox, That is strange, I could not fine %a_ScriptDir%\calc.png
  return
}

; make sure that the calculator is the top most window
winactivate, Calculator


CoordMode, pixel, screen


loop, 255
{
  ; find the newly created image on screen
  variation = %a_index%
  imagesearch, outx, outy, 190, 190, 600, 600, *%a_index% %a_ScriptDir%\calc.png
  ifequal, errorlevel, 0
    break
}
if ErrorLevel = 2
    MsgBox Could not conduct the search.
else if ErrorLevel = 1
    MsgBox Icon could not be found on the screen.
else
    MsgBox The icon was found at %outX%x%outY% variation=%variation%.

   
   
; close the calculator window
send, {alt down}
send, {f4}
send, {alt up}
   
exitapp


The image is eventually found at 200x200 and the variation=236 (using BMP, JPG or PNG formats).

How can I use imagesearch without having to specify such a high
variation?

I forgot to mention, I am running AHK 1.0.47.06, Windows XP SP3, and my screen is at 1400x1050 and 32-bit color.

EDIT: an interesting finding: I decided to try finding just the icon of calculator, using the icon in the calc.exe


Code:
  imagesearch, outx, outy, 190, 190, 600, 600, *%a_index% *icon1 C:\WINDOWS\system32\calc.exe



and the result was 427x326 variation=176 (at least the variation was less, but why so high still). The window is places at 200x200 before starting the imagesearch, so the true location should be less that 210x210, or so.

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2008, 5:10 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
I am left wondering


have a offended the entire subscriber base of this community?

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 24th, 2008, 4:04 am 
Offline

Joined: December 20th, 2005, 4:15 am
Posts: 165
Location: Malaysia
Not sure what you're getting at, or what problem you're trying to solve. Perhaps you could mention the actual app you're working with? Also, being sarky doesn't help.

ImageSearch has worked very well for me in many different apps. As an example, I have a script for WoW, and my ImageSearch variations are only at 3 or 4.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2008, 2:28 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
My working with imagesearch began when I was coding a way to automate web testing for at work. The code would wait for certain things to appear in the window before continuing and looked for buttons by picture in order to click on them, etc.

I had my proof of concept code working pretty good but I had to use a variation of 200 and it seemed that sometimes it wouldn't match at all, or sometimes match everything.

So, I whipped up the code at the top of this thread to make a stand-alone test case that I could experiment with. Once I determined that nothing I could think of made a difference I posted it hoping that someone could point out what I did wrong, or what I left out that would make it work better.

My thanks to any and all who look at this and comment.

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2008, 9:24 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
I finally got this working as I wanted it to, and the variation is only a 1.

My problem was two fold:

the first problem is that I used alt-prtscrn to capture just the active window, this caused the calculator menu shortcuts to be hilighted - F with an underscore, etc - so now I search after sending {alt down}),

the second problem was during testing as I eliminated parts of the code I hand made the bmp of the calculator from a fullscreen shot and that produced a slightly different picture file.

So now this works as desired


Code:
; example of using imagesearch


; bring up calculator
run, calc

WinWaitActive, Calculator, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Calculator to start.
    return
}



; move the calculator to a known location
winmove, 200, 200



; copy the calculator image to the clipboard
send, {alt down}{printscreen}{alt up}



; save the calculator image to a file
run, mspaint

winwaitactive, untitled - Paint, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Paint to start.
    return
}

; bring up the image attributes dialog
send, {ctrl down}e{ctrl up}

; set new image size to 1x1
send, 1{tab}1

; make sure Units=pixels and Colors=Colors
send, {alt down}
send, p
send, l
send, {alt up}
Send, {enter}

send, {ctrl down}v{ctrl up}

send, {ctrl down}s{ctrl up}

winwaitactive, Save As, , 2
if ErrorLevel
{
    MsgBox, Timed out waiting for Save As to start.
    return
}

; make sure that the file doesn't exist
filedelete, %a_ScriptDir%\calc.bmp
send, %a_ScriptDir%\calc.bmp

send, {enter}

; close paint
send, {alt down}
send, f
send, x
send, {alt up}



; ensure that the calculator image file exists
ifnotexist, %a_ScriptDir%\calc.bmp
{
  msgbox, That is strange, I could not fine %a_ScriptDir%\calc.bmp
  return
}



; make sure that the calculator is the top most window
winactivate, Calculator
winwaitactive, Calculator

CoordMode, pixel, screen

; must search with alt down, because doing the alt-prtscrn causes
; the calculator menu shortcuts to be hilighted
send, {alt down}

loop, 255
{
  ; find the newly created image on screen
  variation = %a_index%

  imagesearch, outx, outy, 190, 190, 500, 500, *%a_index% %a_ScriptDir%\calc.bmp

  ifequal, errorlevel, 0
    break
}
if ErrorLevel = 2
    MsgBox Could not conduct the search.
else if ErrorLevel = 1
    MsgBox Icon could not be found on the screen.
else
    MsgBox The icon was found at %outX%x%outY% variation=%variation%.

send, {alt up}
   
; close the calculator window
send, {alt down}
send, {f4}
send, {alt up}
   
exitapp


_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: ImageSearch tips
PostPosted: September 26th, 2008, 4:05 am 
Offline

Joined: December 20th, 2005, 4:15 am
Posts: 165
Location: Malaysia
A few tips for ImageSearch:

a) Limit the search area.
b) Use smaller images for the search
c) Implement backup search(es)
d) Try not to use ImageSearch

Point a) you probably know already. Point b) will increase the rate of success and (very) slightly boost performance. For web pages, my search images are about 50x50 pixels max. For games, they are no more than 25x25 pixels max.

Point c) is a bit kludgy, but Ahk is so efficient that you can easily slip in an extra 1 or 2 ImageSearches, even in a loop. Basically this involves screencapping another region of the object you're trying to click on -- something a bit different from your first screencap. If you're dealing with a game or Flash interface, you can recap the same region a few seconds later, or in a different session. The screencaps may look the same, but there are usually subtle differences due to variations in rendering.

Here's part of my code for a backup image search:
Code:
ImageSearch, GoldKey1_X, GoldKey1_Y, UpperLeftX,UpperLeftY
   , LowerRightX,LowerRightY , *3 goldkey1.png
If ErrorLevel = 0
   {
   Return
   }
; backup search
ImageSearch, GoldKey1_X, GoldKey1_Y, UpperLeftX,UpperLeftY
   , LowerRightX,LowerRightY , *4 goldkeybackup.png
If ErrorLevel = 0
   {
   Return
   }


As for point d), I'd have to note that ImageSearch is not a particularly elegant, reliable or efficient way of automating programs. You can try using an app's keyboard shortcuts instead. IE and FF have a good number of shortcuts, and you can accomplish quite a lot with just keyboard sequences (which also make for simpler Ahk code). A more reliable method would be to use an app's Controls. When it comes to manipulating browsers with Ahk, I have to recommend IE over FF simply because IE has more Controls, and they are more accessibile to Ahk.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2008, 1:58 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
Lemming,
d) isn't feasible for what I want to do, the app (Oracle Forms), runs inside an ActiveX region and unless I missed something Window Spy doesn't "see" anything there.

Thank you for the idea of backup searches, I hadn't considered that.

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: iBob35555VR, krajan, tomoe_uehara and 64 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group