Forum Discussion

Hazey's avatar
Hazey
Occasional Contributor
9 years ago

Find image in image fails

I'm trying to use the Find Image within Image feature of TestComplete but it always seems to fail. I have tried this with some very basic images but still can't get it to work, and I'm wondering if I am doing something wrong. 

 

I have the following basic Python code for this:

def FindImage():
  PaintImage = Aliases.Sys.mspaint.wndMSPaintApp.Picture()
  Log.Picture(PaintImage)
  
  CompImage = 'C:\\tmp\\tmp.jpg'
  
  rect = Utils.Rect
  
  rect = Regions.Find(PaintImage,CompImage)
  if rect is None:
    Log.Message("Not Found")

I originally tried to get this to work on a web application I am testing, but tried to revert to something very simple when that kept failing. I tried to just create a basic image in MS Paint and then took part of the image and tried to search for it. You can see the images here: http://imgur.com/a/uuz6F

 

I have tried debugging it to make sure that both images are actually being loaded successfully, and I can see each of the objects have the correct values in the Locals Panel. However the rect object is always empty.

 

Is there any reason that this should fail?

3 Replies

  • jpr347's avatar
    jpr347
    Occasional Contributor

    For desktop apps I've been using the standard "Stores\Regions" repository, although for web testing there is the "images" repository which appears to be more functional.

     

    The way I did it was create a bunch of regions using the manual scan option.

     

    Once in the repositry crop it by editing with paint.net or suchlike.

     

    In script:

     

    • Create  a picture of the region to find:

            Dim mIconRegionPicture: Set mIconRegionPicture = Regions.GetPicture(pIconRegionName)

     

    • Capture a Picture of the window of interest and crop if necessary to reduce the search area:

        Dim FloorPlanWindow: Set FloorPlanWindow = FloorPlan.Picture(0, 0, -1, -1,False)

     

    • Use the Picture.Find method using the region picture as a paramerter

              Dim Found:  Set found = FloorPlanWindow.Find(mIconRegionPicture, mLeft, mTop, mUseTransparency, mPixelTolerance, False, mColourTolerance, mIconRegionMaskPicture)

     

     

    I noticed you've used the Regions.Find method which is essentially the same, I'm saving regions in PNG but that should not make any difference.

     

    Can you attach a copy of the original file you have posted in imgur? 

     

  • jpr347's avatar
    jpr347
    Occasional Contributor

    Hello Hazy, 


    Is "rect" exactly the same as the image in PaintImage?  Might sound like a daft question without the pixel and colour tolerances it must be exactly pixel-for-pixel identical, as do the colour values right down to the 1-significant bit.

    Even with files upon saving re-sampling can take place which can make them different.

     

    Differnt graphics cards and resolution can rended images differently as well.

     

    Try adding some tollerances.

     

    I had the same problem and a pixel tolerance of 0.75% (of the total pixels in the image being searched), and 42% colour tolerance (In absolute terms = 255*42/100) seemed to solve the problem.  These were found by trial and error by the way but worth it.

     

    The colour depth eg 24 or 32-bit should ideally be identical, but it's not so critical with the correct tolerances.


    (I found working with percentages easier to manage as the same parameters can be used for all my images irrespective of size.)

     

    Another factor that improved recognition was adding larger transparency margins around the images although that may be something you do't have any control over.

    • Hazey's avatar
      Hazey
      Occasional Contributor

      Thanks for the reply.

       

      I have tried changing tolerances but still haven't had any success. I have even tried increasing the tolerances to high numbers but it still won't find a match. I also tried creating some new images and saving them as bmp files as I believe that is what TestComplete use by default. I have tried to just use two saves images, rather than getting a picture image during the script execution and then using a saved image to search for. Even using two images and searching for one in the other I still can't get it to find the image.

       

      Does anyone know of any images I could use to try this with, that people have used successfully.