Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Datajuicer Data juicer ImageSubplotFilter

From Leeroopedia
Revision as of 12:21, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Datajuicer_Data_juicer_ImageSubplotFilter.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Image Filtering, Computer Vision, Hough Transform
Last Updated 2026-02-14 16:00 GMT

Overview

Detects and filters out image samples that contain subplot/grid layouts by analyzing their internal line structure using Hough Line Transform and edge detection.

Description

This filter is valuable for cleaning image datasets of composite/grid images (e.g., comparison charts, multi-panel figures) that may confuse vision models expecting single coherent images.

Algorithm: 1. Convert images to grayscale and apply Gaussian blur for noise reduction 2. Apply Canny edge detection with configurable thresholds 3. Use Hough Line Transform (probabilistic variant) to detect straight lines 4. Classify detected lines as horizontal or vertical based on angle tolerance 5. Calculate a confidence score from multiple weighted components:

    • Line count score (20% each for H/V) -- Ratio of detected lines to minimum thresholds
    • Regularity score (15% each for H/V) -- Line spacing consistency via coefficient of variation
    • Grid structure score (20%) -- Intersection density analysis
    • Length consistency score (5% each for H/V) -- Line length uniformity

Class: ImageSubplotFilter Extends the Filter base class with `_batched_op = True` for efficient batch processing. Key methods:

  • compute_stats_single() -- Detects subplots in each image and stores confidence scores, horizontal/vertical line counts, and a boolean subplot_detected flag in the sample stats.
  • process_single() -- Applies threshold checks combining confidence score, minimum horizontal lines, and minimum vertical lines. Supports "any" (filter if any image has subplots) and "all" (filter only if all images have subplots) strategies.

Usage

Configure in YAML to filter out images containing grid-like subplot layouts. Tune Canny and Hough parameters for different image types.

Code Reference

Source Location

Signature

@OPERATORS.register_module("image_subplot_filter")
@LOADED_IMAGES.register_module("image_subplot_filter")
class ImageSubplotFilter(Filter):
    _batched_op = True

    def __init__(
        self, min_horizontal_lines: int = 3, min_vertical_lines: int = 3,
        min_confidence: float = 0.5, any_or_all: str = "any",
        canny_threshold1: int = 70, canny_threshold2: int = 190,
        hough_threshold: int = 110, min_line_length: int = 110,
        max_line_gap: int = 18, angle_tolerance: float = 4.0,
        *args, **kwargs,
    ): ...
    def compute_stats_single(self, sample, context=False): ...
    def process_single(self, sample) -> bool: ...

Import

from data_juicer.ops.filter.image_subplot_filter import ImageSubplotFilter

I/O Contract

Inputs

Name Type Required Description
min_horizontal_lines int No Minimum horizontal lines for subplot detection (default: 3)
min_vertical_lines int No Minimum vertical lines for subplot detection (default: 3)
min_confidence float No Minimum confidence score threshold (default: 0.5)
any_or_all str No Strategy for multi-image samples: "any" or "all" (default: "any")
canny_threshold1 int No First Canny edge detection threshold (default: 70)
canny_threshold2 int No Second Canny edge detection threshold (default: 190)
hough_threshold int No Hough Transform accumulator threshold (default: 110)
min_line_length int No Minimum detectable line length in pixels (default: 110)
max_line_gap int No Maximum gap between line segments (default: 18)
angle_tolerance float No Tolerance in degrees for H/V classification (default: 4.0)

Outputs

Name Type Description
keep bool True to retain the sample, False to filter it out
stats.image_subplot_confidence List[float] Confidence scores per image
stats.horizontal_peak_count List[int] Horizontal line counts per image
stats.vertical_peak_count List[int] Vertical line counts per image
stats.subplot_detected bool Whether any image contains detected subplots

Usage Examples

# In YAML config:
# process:
#   - image_subplot_filter:
#       min_horizontal_lines: 3
#       min_vertical_lines: 3
#       min_confidence: 0.5
#       any_or_all: 'any'
#       canny_threshold1: 70
#       canny_threshold2: 190

# Programmatic usage:
from data_juicer.ops.filter.image_subplot_filter import ImageSubplotFilter

filter_op = ImageSubplotFilter(
    min_horizontal_lines=3,
    min_vertical_lines=3,
    min_confidence=0.6,
    any_or_all="any",
)

# Compute stats and filter
sample = filter_op.compute_stats_single(sample, context=True)
keep = filter_op.process_single(sample)
print(f"Keep sample: {keep}")
print(f"Confidence: {sample['stats']['image_subplot_confidence']}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment