Skip to main content

RegionMaskService

RegionMaskService.java​

The RegionMaskService is responsible for generating a binary mask based on user-defined JSON coordinates. This mask allows the comparison logic (in CompareService and QuadTreeService) to either selectively compare certain regions or ignore specific dynamic areas.


1. Overview and Purpose​

DetailValueDescription
Class NameRegionMaskServiceGenerates a binary image mask from JSON input.
InputregionJson (String), h (Height), w (Width)User-defined regions and the target image dimensions.
OutputMat (OpenCV Binary Mask)A black-and-white image where White (255) means COMPARE and Black (0) means IGNORE.
Dependenciesorg.json package for JSON parsing.

Export to Sheets


2. The apply Method Workflow​

The method constructs a mask (Mat) that matches the dimensions of the images being compared, and then overlays the user-defined regions.

2.1 Initialization and Default Mask​

  1. Creation: An initial mask (mask) is created using Mat.ones(h, w, CvType.CV_8U). CV_8U indicates an 8-bit, single-channel (grayscale) image.
  2. Default White: The mask is initialized completely white (255) using mask.setTo(new org.opencv.core.Scalar(255)). This represents the default state: compare the entire image.
  3. No JSON Check: If regionJson is null or empty, the full white mask is immediately returned, meaning no filtering will occur.

2.2 Handling 'Select' Regions (Mode Switching)​

The service supports two complementary modes:

  1. Ignore Mode (Default): If only ignore regions are present, the mask remains white (compare all), and the ignore areas are marked black.
  2. Select Mode: If at least one select region is found (hasSelect is true), the interpretation changes:
    • The entire mask is first reset to black (0).
    • Only the explicit select areas will be marked white. Everything else is ignored.
java
if (hasSelect) {
// If 'select' regions exist, we start with a black mask
mask.setTo(new org.opencv.core.Scalar(0));
}

2.3 Processing Region List​

The method iterates through the JSONArray parsed from regionJson, processing each rectangle defined by its coordinates (x,y,width,height) and its type.

Region TypeActionOpenCV OperationEffect on Comparison
selectSets the region in the mask to White (255).mask.submat(...) is set to Scalar(255).Includes this area for difference checking.
ignoreSets the region in the mask to Black (0).mask.submat(...) is set to Scalar(0).Excludes this area from difference checking.

Export to Sheets

The submat method is key here, as it creates a view of a specific rectangular area of the mask, allowing the setTo operation to efficiently apply the scalar value (0 or 255) only to that rectangle.

Example JSON Structure:

JSON
[
{"type": "select", "x": 100, "y": 100, "width": 50, "height": 50},
{"type": "ignore", "x": 500, "y": 50, "width": 80, "height": 20}
]