SubtitleEdit/subtitleedit · error · ArgumentException

Bounding box array [centerPerX, centerPerY, perWidth, perHei

Error message

Bounding box array [centerPerX, centerPerY, perWidth, perHeight] not set or invalid

What it means

Thrown by the BoundingBox constructor when the box argument is null or does not have exactly 4 elements. The array is expected to hold [centerPerX, centerPerY, perWidth, perHeight] as normalized (0-1) coordinates parsed from Google Lens output; a wrong shape makes the downstream ToPixelCoords math meaningless, so construction fails fast.

Source

Thrown at src/ui/Logic/Ocr/GoogleLens/BoundingBox.cs:19

using System;

namespace Nikse.SubtitleEdit.Logic.Ocr.GoogleLens;

public class BoundingBox
{
    private readonly int[] _imageDimensions;

    public double CenterPerX { get; }
    public double CenterPerY { get; }
    public double PerWidth { get; }
    public double PerHeight { get; }
    public PixelCoordinates PixelCoords { get; }

    public BoundingBox(double[] box, int[] imageDimensions)
    {
        if (box == null || box.Length != 4)
        {
            throw new ArgumentException("Bounding box array [centerPerX, centerPerY, perWidth, perHeight] not set or invalid");
        }
        
        if (imageDimensions == null || imageDimensions.Length != 2)
        {
            throw new ArgumentException("Image dimensions [width, height] not set or invalid");
        }

        _imageDimensions = imageDimensions;
        CenterPerX = box[0];
        CenterPerY = box[1];
        PerWidth = box[2];
        PerHeight = box[3];
        PixelCoords = ToPixelCoords();
    }

    private PixelCoordinates ToPixelCoords()
    {
        var imgWidth = _imageDimensions[0];

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Validate the region array has Length == 4 before constructing BoundingBox.
  2. Skip/null-check Lens regions that lack bounding data rather than constructing a box.
  3. If ParseResult is the source, ensure the region Select only emits arrays of length 4.
  4. Guard the upstream double[] with a null check at the call site.

Example fix

// before
var box = new BoundingBox(region, dims);

// after
if (region == null || region.Length != 4) continue;
var box = new BoundingBox(region, dims);
Defensive patterns

Strategy: validation

Validate before calling

if (box == null || box.Length != 4)
    return; // or skip this region
var bb = new BoundingBox(box, imageDimensions);

Type guard

static bool IsValidBox(double[] box) => box != null && box.Length == 4;

Try / catch

try { var bb = new BoundingBox(region, dims); }
catch (ArgumentException ex) when (ex.Message.Contains("Bounding box array")) { continue; }

Prevention

When it happens

Trigger: Constructing new BoundingBox(box, imageDimensions) where box is null, empty, or has fewer/more than 4 entries. Commonly happens when Google Lens returned no region for a detected object, or when ParseResult's region extraction yielded a null/short array.

Common situations: A Lens response where a text region has no bounding data; an upstream parsing change that altered the array shape; defensive code passing a default null before checking Lens output.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/bbeb5a9f2bb5919e. Report an issue: GitHub.