ImagePos, Loops, WaitScreenImage and WhileDo?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
marvinzzz
Newbie
Posts: 4
Joined: Sun Aug 18, 2013 11:08 pm

ImagePos, Loops, WaitScreenImage and WhileDo?

Post by marvinzzz » Sun Aug 18, 2013 11:42 pm

I have the following script which is working as intended - it writes the y co-ordinates of an image's position to a text file when it detects it:

Code: Select all

Wait>3
FindImagePos>c:\test\win.bmp,SCREEN,20,1,Xpos,YPos,NumFound
If>NumFound>0
  WriteLn>c:\test\results.txt,%YPos_0%,%XPos_0%
Else
  MessageModal>No image found
Endif
In my case, the image "win.bmp" will display at different times and at different y positions on the screen. For example, the image will show / hide overtime like: 0:30-0:35, 1:10-1:13, 1:20-1:25, etc. The idea is that the complete history of the y coordinates are written to file.

How can I expand the script above to continually check until a different image is displayed to indicate that the script should exit?

I think I need to create a DoWhile statement with the WaitScreenImage (for the exit image) which encapsulates the entire script above however I'm not sure how to put everything together.

Any help if possible would be appreciated.

marvinzzz
Newbie
Posts: 4
Joined: Sun Aug 18, 2013 11:08 pm

Post by marvinzzz » Mon Aug 19, 2013 4:43 am

I've iterated the script to include labels so I can loop through until a certain image is found:
Wait>3
Label>Start
Let>NumFound1=0
FindImagePos>c:\test\end2.bmp,SCREEN,0,1,Xpos,YPos,NumFound1
If>NumFound1>0
Goto>Exit
Else
Goto>Checker_1
Endif

Label>Checker_1
FindImagePos>c:\test\win.bmp,SCREEN,0,1,Xpos,YPos,NumFound2
If>NumFound2>0
WriteLn>c:\test\results.txt,%YPos_0%,%XPos_0%
Wait>0.25
Else
Goto>Checker_2
Endif

Label>Checker_2
FindImagePos>c:\test\bmp\Jim.bmp,SCREEN,0,1,Xpos,YPos,NumFound3
If>NumFound3>0
WriteLn>c:\test\results.txt, %NumFound3%, P1 is Jim
Wait>0.25
Goto>Checker_3
Else
Goto>Checker_3
Endif


Label>Checker_3
FindImagePos>c:\test\bmp\Joe.bmp,SCREEN,0,1,Xpos,YPos,NumFound4
If>NumFound4>0
WriteLn>c:\test\results.txt, %NumFound4%, P2 is Joe
Wait>0.25
Else
Goto>Start
Endif

Label>Exit
WriteLn>c:\test\results.txt, %NumFound5%, File ended
Also the scope has been extended to attempt to handle multiple images that are "seen" however it's quite unstable like this.

The code is running sequentially and there's event some overlap in terms of when images appear on screen. That is to say that there may be cases where I miss logging an event because I use wait periods in order not to write many duplicate lines in the text file.

Is it possible to have any number amount of loops fire and run independently of each other in their own "threads" or suchlike so that their wait periods, etc are not affected by each other?

Also, is there a better / more elegant way to check for multiple images than my structure above? I have in fact around 40 images that I need to check for and I think it'll become problematic if I can't handle the comparison part efficiently. I think I need something like an OR operator with FindImagePos like fine img1.bmp or img2.bmp or img3.bmp, etc. Is something like that possible?

Many thanks.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1417
Joined: Sun Nov 03, 2002 3:19 am

Post by Dorian (MJT support) » Tue Aug 20, 2013 12:08 am

I wonder whether a custom trigger would serve you better? So instead of having one macro which is constantly running, you'd have a number of different macros which would fire up when certain images appeared.

I haven't experimented with 40 triggers of this type at once though. I have a feeling that might cause a similar drain on your resources.

Take a look at this thread :
http://www.mjtnet.com/forum/custom-even ... ght=custom trigger

marvinzzz
Newbie
Posts: 4
Joined: Sun Aug 18, 2013 11:08 pm

Post by marvinzzz » Tue Aug 20, 2013 5:08 pm

Many thanks for your response. The triggers look interesting though I wonder about the added time that it'll take for each macro to load and initialize. I will experiment with them if the script I'm working on proves too slow.

In that vain, is it possible to only "look" in certain fixed dimensions in some way for each of my images? Their positions are always within a fixed area of the screen so I'm guessing I can save a ton of cpu / gain some speed (should that be a factor) by only looking in a smaller area.

I'm guessing that something like this should do the trick? (taken from the help section):
GetScreenRes>X,Y
ScreenCapture>10,10,200,200,d:\mypic.bmp
FindImagePos>d:\today_button.bmp,d:\screen.bmp,0,1,X,Y,NumFound
If>NumFound>0
// do something..
Endif

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Wed Aug 21, 2013 2:41 am

Change the FindImagePos to a fixed region as well if you know its only going to appear in the one area.

return_offset is used to determine which coordinates should be returned and can be one of the following:

0
Top left position of located image within bitmap_to_scan. In other words X, Y is the top left corner of needle_bitmap within haystack

1
Center: X,Y will be set to the center of the located image within the main haystack image.

2
Top right

3
Bottom left

4
Bottom right

5
Middle top

6
Middle bottom

7
Middle left

8
Middle right


X_Array is the name of an array to store the X coordinates of each match. If using CCOEFF matching (the default) only the most likely result is returned and so there will be only one result in X_Array_0 if a match is found. If using EXACT matching multiple results may be returned. The first match is stored in X_Array_0, the second in X_Array_1, etc.

Y_Array is the name of an array to store the Y coordinates of each match. If using CCOEFF matching (the default) only the most likely result is returned and so there will be only one result in X_Array_0 if a match is found. If using EXACT matching multiple results may be returned. The first match is stored in Y_Array_0, the second in Y_Array_1, etc.

NumFound returns the number of matches found. If an error is encountered NumFound will be set to -1. E.g. if the needle image is larger than the haystack image, NumFound will be set to -1.

Note that if haystack is SCREEN or a WINDOW the x and y positions returned are absolute screen positions and can be used directly with MouseMove to move the mouse to the found position.
FIREFIGHTER

marvinzzz
Newbie
Posts: 4
Joined: Sun Aug 18, 2013 11:08 pm

Post by marvinzzz » Sun Aug 25, 2013 12:38 pm

Thanks for your help CyberCitizen.

In the end I used a trigger as suggested that waits for a certain image to appear that fires a series of subroutines to searches 300 possible images within fixed areas of the screen.

The script is around 4000 lines long and the longest possible search only takes around 1.3 seconds on my machine which is within the requirement.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts