Nodes/ComfyUI-Conditioning-Rebalance/Border Mask & Crop Detector
ComfyUI Node

Border Mask & Crop Detector

Letterbox bars are eating your training data — this node finds them

By nova452·Created 3 months ago·Updated 28 days ago· 509
Border Mask & Crop Detector
  • image
  • mask
  • cropped_image
detect_toptrue
detect_bottomtrue
detect_lefttrue
detect_righttrue
threshold0.80
color_tolerance0.020
max_scan_depth0.35

Black bars. White bars. Random matte color bars. However you get them, a letterboxed image will quietly sabotage anything you feed it into - a reference encoder reads the bars as content, a training dataset learns to reproduce them, and an inpaint mask that "should" cover the subject doesn't. BorderMaskDetector from nova452's Rebalance Pack exists to find those bars and give you two things at once: a mask that marks exactly where they are, and a cropped image with them gone.

It's part of the pack that shipped the Krea 2 conditioning-rebalance node on release day, and like the rest of the utility side it exists to keep image-edit pipelines honest about what's actually in the frame. If you're building a Krea 2 or Ideogram 4 reference/edit workflow, or cleaning a dataset for LoRA training, this is the node that makes your source images behave.

How it works

The detection is simple but robust. For each edge you enable, the node walks inward row by row (or column by column), computes the median color of that line, then counts how many pixels are within color_tolerance of the median. If at least threshold fraction of the line matches, that line is border and it keeps going. The first line that doesn't match stops the scan. Uniform bars score 100% matches and stop being a thing the moment you hit real image content.

Two details keep it honest. max_scan_depth caps how far in it will search (as a fraction of the width/height, default 0.35), so a large flat-color region in the middle of a photo doesn't get eaten. And the cropped output is computed from the union of all four detected borders across the whole batch - useful when every frame in a video or folder has the same bars, and the reason mixed-size images in one batch crop to the "safest" shared box rather than each to its own.

The inputs that matter

  • image - the tensor with the bars.
  • detect_top / bottom / left / right - the four BOOLEAN toggles, all on by default. Turn off a side if it legitimately has a uniform edge you want to keep (a sky gradient on the top edge is a classic).
  • threshold (0.8) - what fraction of a row/column must match the median to count as border. Raise it toward 1.0 if it's eating subtle content, lower it if bars are coming back undetected.
  • color_tolerance (0.02) - how close a pixel must be to the median to match. Raise it for gradient bars, lower it for clean ones.
  • max_scan_depth (0.35, max 0.5) - how far in to search.

It returns mask (a MASK with 1.0 on the bars) and cropped_image (an IMAGE with the bars trimmed). The mask feeds a SetLatentNoiseMask or an inpaint composite; the cropped image feeds your reference encoder or goes straight to a loader.

Install

It ships in the Rebalance Pack, so it installs with the whole thing. In ComfyUI Manager, search "ComfyUI-Conditioning-Rebalance" (or "Rebalance Pack") and hit install. Manual way:

cd ComfyUI/custom_nodes
git clone https://github.com/nova452/ComfyUI-ConditioningKrea2Rebalance

Restart ComfyUI afterwards. No model files, no extra pip packages - this one is pure torch.

Troubleshooting

Where people actually get burned: a batch mixing images with and without bars, because the crop box is shared across the batch (the node takes the max of the top borders, min of the bottom ones, etc.). If your folder is mixed, process it in two passes - or just accept the slightly less aggressive crop. Second classic: a white border on a white background with a low threshold gets over-eager, so keep an eye on it when the image has a flat background that matches the border color. And remember it only detects - if you want the mask softened or feathered before compositing, that's a job for a mask node downstream.

CategoryRebalance-Pack/image

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
detect_topBOOLEANtrue
detect_bottomBOOLEANtrue
detect_leftBOOLEANtrue
detect_rightBOOLEANtrue
thresholdFLOAT0.800.01–1
color_toleranceFLOAT0.0200–1
max_scan_depthFLOAT0.350.01–0.5

Outputs (2)

NameTypeDescription
maskMASK
cropped_imageIMAGE