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
| Detail | Value | Description |
|---|---|---|
| Class Name | RegionMaskService | Generates a binary image mask from JSON input. |
| Input | regionJson (String), h (Height), w (Width) | User-defined regions and the target image dimensions. |
| Output | Mat (OpenCV Binary Mask) | A black-and-white image where White (255) means COMPARE and Black (0) means IGNORE. |
| Dependencies | org.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
- Creation: An initial mask (
mask) is created usingMat.ones(h, w, CvType.CV_8U).CV_8Uindicates an 8-bit, single-channel (grayscale) image. - 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. - No JSON Check: If
regionJsonis 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:
- Ignore Mode (Default): If only
ignoreregions are present, the mask remains white (compare all), and theignoreareas are marked black. - Select Mode: If at least one
selectregion is found (hasSelectis true), the interpretation changes:- The entire mask is first reset to black (0).
- Only the explicit
selectareas 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 Type | Action | OpenCV Operation | Effect on Comparison |
|---|---|---|---|
select | Sets the region in the mask to White (255). | mask.submat(...) is set to Scalar(255). | Includes this area for difference checking. |
ignore | Sets 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}
]