Skip to main content

QuadTreeService

QuadTreeService.java​

The QuadTreeService implements the recursive QuadTree algorithm for efficiently detecting and isolating pixel-level differences between two images. This method significantly speeds up comparison by avoiding pixel-by-pixel checks in large, uniform areas.


1. Overview and Constants​

DetailValueDescription
Class NameQuadTreeServiceImplements the quadtree comparison logic.
MIN_SIZE16The minimum block size (16×16) where recursion stops, forcing a full pixel difference check.
SENSITIVITY5The threshold used in the diffGray method. Pixel differences below this intensity value are ignored.
Key Functiondiff(Mat img1, Mat img2)The public entry point for the comparison.

Export to Sheets


2. Difference Calculation: diffGray​

This private utility function calculates the raw pixel difference between two image blocks and applies a sensitivity threshold.

StepOpenCV FunctionPurpose
1. Absolute DifferenceCore.absdiff(a, b, diff)Calculates the absolute difference between every pixel in a and b (preserving color channels).
2. Grayscale ConversionImgproc.cvtColor(diff, gray, ...)Converts the color difference image to a single-channel grayscale image.
3. ThresholdingImgproc.threshold(gray, thresh, SENSITIVITY, ...)Creates a binary mask (thresh). Any pixel whose difference intensity is greater than SENSITIVITY (5) is set to white (255); all others are set to black (0). This filters out minor, imperceptible differences (noise).

Export to Sheets

The resulting thresh is a difference mask: a black and white image where only the significant differences are visible as white pixels.


3. Recursive QuadTree Algorithm: quadtreeDiff​

This is the core recursive function that divides the image block into four quadrants.

3.1 Base Cases (Stopping Recursion)​

The recursion stops under two main conditions, based on the current block being processed:

  1. No Difference Found:
    • Checks if the current block pair (block1, block2) has any difference pixels using Core.countNonZero(diffGray(block1, block2)).
    • If the count is 0, the block is identical. The function immediately returns a black Mat (zero difference), avoiding further recursion.
  2. Minimum Size Reached:
    • Checks if the block width or height (w or h) is less than or equal to MIN_SIZE (16).
    • If the minimum size is hit, the block is too small to split further. The function returns the full difference mask for this block via diffGray(block1, block2).

3.2 Recursive Step​

If neither base case is met, the algorithm proceeds to split the block:

  1. Split: The block is split into four Rect objects (quads), representing the top-left, top-right, bottom-left, and bottom-right quadrants.
  2. Recurse: The quadtreeDiff function is called recursively for each of the four quadrants. This is the mechanism for the tree-like traversal.
  3. Combine Results: The difference masks (sub Mat) returned from the recursive calls are copied back into the appropriate section of the main mask Mat for the current level.
  4. Return: The combined mask for the current block is returned up the recursion chain.

This approach ensures that only blocks containing significant differences (i.e., blocks that cannot pass the "No Difference Found" check) are split and processed down to the minimum size, optimizing performance over areas of the image that are static.