Forum Discussion

Teemu's avatar
Teemu
New Contributor
9 years ago

Image comparison and image find problems

I'm testing an application, which has a map with several icons. I couldn't find the icons in the Object Browser. My goal is to find a camera icon on the map and click the icon.  

 

I have found the original icon as a png file in the program files and try to locate that in the map, but it's not finding the icon. I've read the documentation about the Picture.Find method and tried to use the color and pixel tolerance values. Below is the relevant code of my application in Python. 

 

#Starting the application
TestedApps.Livi_TLoik_Ui_Kaynnistin.Run()
livi_TLoik_Ui_Karttatyokalu = Aliases.Livi_TLoik_Ui_Karttatyokalu
hwndSource = livi_TLoik_Ui_Karttatyokalu.HwndSource_Shell
grid = hwndSource.Shell.Grid
###
#Unrelevant code emitted#
###
screenPicture = Sys.Desktop.ActiveWindow().Picture()
cameraPicture = Utils.Picture
cameraPicture.LoadFromFile("C:\\Program Files\\T-LOIK\\Kartta\\kuvat\\symbolit\\kamera\\s_map_camera_outdated.png")

#With a small pixel tolerance value it doesn't find any match, this value finds a wrong place. 
location = screenPicture.Find(cameraPicture, 0, 0, False, 500,True, 50)
Log.Message(location.Bottom)
Log.Message(location.Top)
Log.Message(location.Right)
x = (location.Left + location.Right)/2
y = (location.Top + location.Bottom)/2
grid.Click(x, y)

 

 

I also tried the compare function with a screenshot of the icon on the map and the same icon, but that didn't work either.

 

I have attached the camera icon and a screenshot of the map. There are several of the camera icons on the map and it doesn't really matter which one I would click. 

 

How could I make TestComplete find the image on the map? How could I have debugged this myself? 

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Are you getting any error messages? Or is it simply clicking in the wrong place on screen?

     

    One observation... you're x and y value calculations at the end, you're dividing by 2... so, that means the potential is there to be a "half" a pixel.  There's no such thing.  So, perhaps, you need to round x and y to the nearest integer value. I don't know if that's the problem itself but it is something I would do to have more "correct" code.

    • Teemu's avatar
      Teemu
      New Contributor

      I have gotten two types of error messages. 

       

      Case 1. If I have high pixel tolerance value
      It finds a wrong place and clicks it, this causes an error because after that it tries to look for a pop-up window that should open after clicking the camera. But of course the pop up doesn't exist, because the test has not actually clicked on the camera. 

       

      Case 2. I have low pixel tolerance value

      No match is found and I got an error saying that None object does not have attribute Left. This is also quite obvious since location is then "None" and it does not have the attribute. 

      In regards, to your integer rounding suggestion, that's a good point. I hadn't thought about that. However, at least on my own Python version, integer division is rounded to the lower number automatically. For example 7 / 2 results in 3. 

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Just to help with debugging...  have you tried doing a simple recording of clicking on a camera image to see what co-ordinates are used there? 

        Also, in your first case, what co-ordinates are used for x and y? How far off is the click versus any of the camera icons?

        Where my brain is going with this (and this is just me thinking out loud, really), is that the co-ordinates returned by the Find Picture call may be correct with regards to the captured picture... but the grid component itself may have a different resolution or configuration. So, while the pixel location on the image may find a camera correctly at pixel (25,100) (as an example), the grid resolution may have (25,100) at a completely different location. Does that make sense?