Optimizing pattern matching performance using Sikuli

    Pattern matching is what Sikuli does most of the time. Usually, the matching process time is acceptable for most uses but in some cases we might need to improve it. For example:
  • Test scenarios are growing longer and it is becoming more important to minimize delays as much is possible. 
  • The visual content is changing quickly and we have to be able to capture it.
Here I will discuss few techniques that can be used in order to reduce the wait time by making the search more efficient and adopted to our needs.


Generally, this goal can be reached through the following approaches:
  1. Regions and Patterns manipulation 
  2. Function settings
  3. Configuration

Region and Pattern Manipulation

    In order to understand the rationale behind applying this approach, let's have a quick look at how pattern matching is implemented in Sikuli which is done by utilizing the OpenCV Template Matching functionality using the matchTemplate function:

 matchTemplate(Mat image, Mat templ, Mat result, int method)  

The function slides through image, compares the overlapped patches of size w x h. After the function finishes the comparison, the best matches can be found as global minimums (or maximums).

   So based on this knowledge, it is clear that the larger the Region, the longer is the search time within its bounds. Hence, defining a smaller regions can speed up the search time quite significantly.

    For example, searching the whole screen takes up to 1sec. Searching an image within a 100x100 pixels area, however, will only take 0.001sec

Function settings

 exists(image, waitTime)  

Setting this parameter to 0, will force the function to return after only one search attempt. It will bring down the waiting times to <1sec.

NOTE: Default wait time in Sikuli is 3sec. This setting can be changed by modifying the AutoWaitTimeout  setting.


Configuration

 Settings.WaitScanRate  

This parameter will set the number of search attempts that will be done in one second. So setting the parameter to a higher number and using it with  exists(image, 0) will speed up the process.


NOTE: Increasing the WaitScanRate should be done with caution as it might overload the CPU.

Comments