Search area determined by previous image location

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
zorg
Newbie
Posts: 2
Joined: Wed Jul 24, 2019 6:35 pm

Search area determined by previous image location

Post by zorg » Wed Jul 24, 2019 10:01 pm

I've looked but didn't find an answer so if i've missed something point me in the right direction pls. And yes im new to this so all help is appreciated, and if there some wizard/preset for a variable position like this pls advise.
but here is the situation:
i have to find an image and click it. easy. coded and working.
however i have situations where i have 2 of the same image in the search area. and as they change positions its not possible to lets say find both and click the first one.
however there is an image next to each of them that can be used to determine which one to click.
Yes i know there is an option where you can click right/left/above/below and image with a preset offset. but the distance between image 1 and image 2 is not always exactly the same. and except in one scenario its not in a straight line more like down and to the right (which i guess you can solve by 2 move mouse commands one for each direction). but the variable distance is the problem factor.

so tried to do that by defining the search area based on the location of that unique image.

so basically search for Image 1
find its location
use that location to determine the search area of the Image 2 (that one we need to click)

Code: Select all

ScreenCapture>6,160,647,703,%TEMP_DIR%\screenrect.bmp
//Find and Do Nothing Top Left of 
FindImagePos>%BMP_DIR%\image_1.bmp,%TEMP_DIR%\screenrect.bmp,0.7,0,XArr,YArr,NumFound,CCOEFF

**BREAKPOINT**
ScreenCapture>{%XArr_0%+6},{%YArr_0%+160},{%XArr_0%+435},{%YArr_0%+298},%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_2.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+159},{%YArr_0%+576}
  LClick
Endif
so this was my attemtp to make that work:

Code: Select all

ScreenCapture>{%XArr_0%+6},{%YArr_0%+160},{%XArr_0%+435},{%YArr_0%+298},%TEMP_DIR%\screenrect.bmp
the stab in the dark actually miraculously worked, confirmed by checking the created screenrect.bmp in the temp directory :)

but here is where i run out of luck. and can't figure out how to solve

Code: Select all

If>NumFound>0
  MouseMove>{%XArr_0%+159},{%YArr_0%+576}
since 159 & 576 are original X,Y starting coordinates for the ScreenCapture/search area i determined in the Image recognition Wizard before i adjusted it to the variable one.
but because the X,Y are not static and thus incorrect the mouse obviously moves to the wrong place.
so it should read something to the effect of:

Code: Select all

If>NumFound>0
  MouseMove>{%XArr_0%+(old%XArr_0%+6) },{%YArr_0%+(old%XArr_0%+160)}
except i have no clue how to tell it to use the original XArr_0, from image 1 at this point.

thanks for the help

User avatar
PepsiHog
Automation Wizard
Posts: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Search area determined by previous image location

Post by PepsiHog » Fri Jul 26, 2019 1:33 pm

@zorg,
You can search for both images. The first line findimage finds the main image. The next line findimage finds the second image. Use the if statement after the two findimage commands. Use different vars for each findimage, so you can refer to and use the data for both images. Findimage can give the x,y of the image center, so just use that in your mousemove. if each var in each findimage has a different name, you have no issues with locating and clicking any image you want. Remember, make all vars some type of unique name. A var name that helps you know what image it refers to. Also, findimage results are arrays. so if it finds the same image twice, the result for that image will be the next array. Such as x_1,y_1 would be the first image found and x_2,y_2 will be the second image it found.

To be clear, in findimage you have XArr,YArr,NumFound. Change their names to indicate what they are. For the main image you could use MainX,MainY,MainFound. And maybe SecondX,SecondY,SecondFound for the other image. Also, I said about how it will find both images if there, that continues out for any number of the same image. MainX_3, MainY_3........

It is also useful, if the image contains some background (that may change) to capture a small portion of the image that has no background. The center part of the image. And just search for the smaller portion of the image.

Hope this helps,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

zorg
Newbie
Posts: 2
Joined: Wed Jul 24, 2019 6:35 pm

Re: Search area determined by previous image location

Post by zorg » Tue Jul 30, 2019 9:02 pm

Thanks for the reply.
i don't know if I'm not understanding something in the way you are describing the search of 2 images at once but im not seeing a relationship between them. If the distance between the two searched images was always the same i see how i can in that case use the coordinates of found image 1 to determine the mousemove to the one i need. but the distance it not always the same

the situation is that i have 2 identical images (buttons to click) on the screen. but i only want to click a specific one. I cant determine which one by the order they appear in as that changes (the two boxes containing the image and the button randomly switch places) . so the only way to tell them apart is by their proximity to a different image.
________________________
| image 1
| Button to click
|______________________|
________________________
| image 2
| Button to click
|______________________|
because the distance of image 1 or 2 to the button i need to click is not always the same finding image 1 and specifying the distance to move mouse doesn't work.

so the only way i can figure it to work was as i posted above by finding image 1 and limiting the search area (screenrect.bmp) based on that.
so search the whole area for image 1 then once its located limit the search area only on the box size around image 1. this way the 2nd identical button is not in the search area and cant be selected by mistake.
Limiting the search area and finding the right button, worked in the above code. the problem i ran into at that point was calculating the coordinated to move the mouse to. I needed to add the original values of XArr and Y from image 1 to the new XArr and Y values created by finding the button image. except the original X/Y Arr was overwrite and not available any more

but changing the naming for each X/YArr to be different as suggested would then (i think - have't tested) keep each in memory separately until overwritten. so then the mouse move can be determined by adding the 2: MainX + SecondX.

i had done some additional searching and came up with this in the mean time:

Code: Select all

ScreenCapture>6,160,647,703,%TEMP_DIR%\screenrect.bmp
    //Find and Do Nothing Top Left of
    FindImagePos>%BMP_DIR%\empty.bmp,%TEMP_DIR%\screenrect.bmp,0.95,0,XArr,YArr,NumFound,CCOEFF
    If>NumFound>0
      Let>TopLeftX=Xarr_0+6
      Let>TopLeftY=YArr_0+160

      ScreenCapture>{%XArr_0%+6},{%YArr_0%+160},{%XArr_0%+350},{%YArr_0%+298},%TEMP_DIR%\screenrect.bmp
      //Find and Left Click Center of
      FindImagePos>%BMP_DIR%\abort.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
      If>NumFound>0
        MouseMove>{%XArr_0%+%TopLeftX%},{%YArr_0%+%TopLeftY%}
        LClick
      Endif
      Endif
It saves the XArr and YArr of image 1 and makes it available to add in for the mouse move, after the second image is found.
Seems to work, but if someone has a better/more stable or more elegant solution I'm all ears.

User avatar
PepsiHog
Automation Wizard
Posts: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Search area determined by previous image location

Post by PepsiHog » Mon Aug 05, 2019 8:29 pm

@zorg
Ok. Let's recap. You said there are two images on the screen that are identical. You said that there is no way for you to know which of these images to click on except that there is an image next to the image that will tell you which to click on. Right?

Thus, and therefore, the only image that is really useful here, is the image that tells which image to click on. Right? Let's refer to this image as the second image. So doesn't that mean that what you need to know is where the "second image" is on the screen?

Macro Scheduler has a help file. It describes everything about the command FindImage. I am not going over it again.

You do know how to do a screen capture. I am curious why you are leaving this to the imagination of those that are only trying to help you. This forum allows you to post screen captures.

I have been programming in Macro Scheduler since 2009. I can help you do this, if you really want the help.

PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

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