Finding an Image Within Another Image

Applies to TestComplete 15.47, last modified on January 20, 2023

Your test scripts may need to find one image within another. To perform this task, use the Find or FindRegion method of the Regions object or the Find method of the Picture object. The difference between these methods is that the methods of the Regions object can operate with Picture, window and onscreen objects as well as images stored in the Regions collection of the Stores project item, while the method of the Picture object search for an image within the image to which this object provides scripting access to (that is, this image may be or may not be stored in the Regions collection).

Below, are several samples that demonstrate how to search for one image within another.

Searching for a stored image within a window image

The Regions.Find and Regions.FindRegion methods can be used to search for one image within another. These methods are very similar and differ only in its parameter's order. Both the sought-for and search-in image can be specified as a Picture, window or onscreen object or, if the image is stored to the Regions collection, you can specify it using the name, which the image has in this collection.

The following code searches for an image within another image that is stored in the Regions collection.

JavaScript

function Test()
{
let w;
  w = Sys.Desktop.ActiveWindow().Picture(20, 20, 50, 50);
  
  // Region1 is part of the Regions collection
  if (strictEqual(Regions.FindRegion(w, "Region1"), null))
    Log.Warning("Not found");
}

JScript

function Test()
{
var w;
  w = Sys.Desktop.ActiveWindow().Picture(20, 20, 50, 50);
  
  // Region1 is part of the Regions collection
  if (Regions.FindRegion(w, "Region1") == null)
    Log.Warning("Not found");
}

Python

def Test():
  w = Sys.Desktop.ActiveWindow().Picture(20, 20, 50, 50)
  
  # Region1 is part of the Regions collection
  if Regions.FindRegion(w, "Region1") == None:
    Log.Warning("Not found")

VBScript

Sub Test
  Set w = Sys.Desktop.ActiveWindow.Picture(20, 20, 50, 50)
  
  ' Region1 is part of the Regions collection
  If Regions.FindRegion(w, "Region1") Is Nothing Then
    Log.Warning "Not found"
  End If
End Sub

DelphiScript

procedure Test();
var
  w : OleVariant;
begin
  w := Sys.Desktop.ActiveWindow.Picture(20, 20, 50, 50);
  
  // Region1 is part of the Regions collection
  if Regions.FindRegion(w, 'Region1') = nil then
    Log.Warning('Not found');
end;

C++Script, C#Script

function Test()
{
  var w;
  w = Sys["Desktop"]["ActiveWindow"]()["Picture"](20, 20, 50, 50);
  // Region1 is part of the Regions collection  
  if (Regions["FindRegion"](w, "Region1") == null)
    Log["Warning"]("Not found");
}

Searching with Picture.Find - Example 1

This example demonstrates how to search within an image using the Find method of the Picture object. The following code searches for the image under the cursor within a rectangle of your application’s window.

JavaScript

function Test()
{
  var w, Pict1, Pict2;
  
  // Obtains one of the images
  Pict1 = Sys.Desktop.PictureUnderMouse(20, 20, false);
  
  // Obtains another image
  w = Sys.Process("MyApp", 1).Window("MyWndClass", "MyWndCaption", 1);
  Pict2 = w.Picture(10, 10, 200, 150);
  
  if (strictEqual(Pict2.Find(Pict1), null))
    Log.Warning("Not found");
}

JScript

function Test()
{
  var w, Pict1, Pict2;
  
  // Obtains one of the images
  Pict1 = Sys.Desktop.PictureUnderMouse(20, 20, false);
  
  // Obtains another image
  w = Sys.Process("MyApp", 1).Window("MyWndClass", "MyWndCaption", 1);
  Pict2 = w.Picture(10, 10, 200, 150);
  
  if (Pict2.Find(Pict1) == null)
    Log.Warning("Not found");
}

Python

def Test():
  
  # Obtains one of the images
  Pict1 = Sys.Desktop.PictureUnderMouse[20, 20, False]
  
  # Obtains another image
  w = Sys.Process("MyApp", 1).Window("MyWndClass", "MyWndCaption", 1)
  Pict2 = w.Picture(10, 10, 200, 150)
  
  if Pict2.Find(Pict1) == None:
    Log.Warning("Not found")

VBScript

Sub Test
  ' Obtains one of the images
  Set Pict1 = Sys.Desktop.PictureUnderMouse(20, 20, False)
  
  ' Obtains another image
  Set w = Sys.Process("MyApp", 1).Window("MyWndClass", "MyWndCaption", 1)
  Set Pict2 = w.Picture(10, 10, 200, 150)
  
  If Pict2.Find(Pict1) Is Nothing Then
    Log.Warning "Not found"
  End If
End Sub

DelphiScript

procedure Test;
var
  w, Pict1, Pict2 : OleVariant;
begin
  // Obtains one of the images
  Pict1 := Sys.Desktop.PictureUnderMouse(20, 20, False);
  
  // Obtains another image
  w := Sys.Process('MyApp', 1).Window('MyWndClass', 'MyWndCaption', 1);
  Pict2 := w.Picture(10, 10, 200, 150);
  
  if ( Pict2.Find(Pict1) = nil ) then
    Log.Warning('Not found');
end;

C++Script, C#Script

function Test()
{
  var w, Pict1, Pict2;
  
  // Obtains one of the images
  Pict1 = Sys["Desktop"]["PictureUnderMouse"](20, 20, false);
  
  // Obtains another image
  w = Sys["Process"]("MyApp", 1)["Window"]("MyWndClass", "MyWndCaption", 1);
  Pict2 = w["Picture"](10, 10, 200, 150);
  
  if (Pict2["Find"](Pict1) == null)
    Log["Warning"]("Not found");
}

Searching with Picture.Find - Example 2

The previous example demonstrates how you can search for an image that is represented by the Picture object. If you need to search for an image that is stored in the Regions collection, you should obtain the Picture object that corresponds to this image and then use the Picture.Find method. The following code demonstrates this:

JavaScript

function Test()
{
  var Pict, StoredPict;
  
  // Obtains one of the images
  Pict = Sys.Desktop.PictureUnderMouse(20, 20, false);
  
  // Obtains another image
  StoredPict = Regions.GetPicture("StoredImageName");
  
  if (strictEqual(Pict.Find(StoredPict), null))
    Log.Warning("Not found");
}

JScript

function Test()
{
  var Pict, StoredPict;
  
  // Obtains one of the images
  Pict = Sys.Desktop.PictureUnderMouse(20, 20, false);
  
  // Obtains another image
  StoredPict = Regions.GetPicture("StoredImageName");
  
  if (Pict.Find(StoredPict) == null)
    Log.Warning("Not found");
}

Python

def Test():
  # Obtains one of the images
  Pict = Sys.Desktop.PictureUnderMouse[20, 20, False]
  
  # Obtains another image
  StoredPict = Regions.GetPicture("StoredImageName")
  
  if Pict.Find(StoredPict) == None:
    Log.Warning("Not found")

VBScript

Sub Test
  ' Obtains one of the images
  Set Pict = Sys.Desktop.PictureUnderMouse(20, 20, False)
  
  ' Obtains the stored image
  Set StoredPict = Regions.GetPicture("StoredImageName")
  
  If Pict.Find(StoredPict) Is Nothing Then
    Log.Warning "Not found"
  End If
End Sub

DelphiScript

procedure Test;
var
  Pict, StoredPict : OleVariant;
begin
  // Obtains one of the images
  Pict := Sys.Desktop.PictureUnderMouse(20, 20, False);
  
  // Obtains stored image
  StoredPict := Regions.GetPicture('StoredImageName');
  
  if ( Pict.Find(StoredPict) = nil ) then
    Log.Warning('Not found');
end;

C++Script, C#Script

function Test()
{
  var Pict, StoredPict;
  
  // Obtains one of the images
  Pict = Sys["Desktop"]["PictureUnderMouse"](20, 20, false);
  
  // Obtains stored image
  StoredPict = Regions["GetPicture"]("StoredImageName");
  
  if (Pict["Find"](StoredPict) == null)
    Log["Warning"]("Not found");
}

See Also

Region Checkpoints
About Region Checkpoints
How Image Comparison Works
Comparing and Finding Images - Specific Tasks

Highlight search results