Nodes/ComfyDL/Box Corner→Center
ComfyUI Node

Box Corner→Center

Bounding boxes in (x1, y1, x2, y2)? Convert them to center-width-height

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Box Corner→Center
  • boxes
  • boxes_ccwh

CdlBoxCornerToCenter converts bounding boxes from corner format - top-left and bottom-right (x1, y1, x2, y2) - into center format - center-x, center-y, width, height (cx, cy, w, h). It's the mirror image of CdlBoxCenterToCorner, and together they cover the two box encodings you'll meet in every object-detection tutorial. The corner form is what IoU, NMS and drawing routines want; the center form is how a lot of detectors predict boxes (regressing an offset from an anchor center is more natural than predicting corners directly).

How it works

Trivial arithmetic per box, exactly the inverse of the other converter:

cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
w  = x2 - x1
h  = y2 - y1

The center is just the midpoint of the two corners; the width and height are the spans. The node runs that over every row of your boxes tensor (it normalizes to 2-D first, so a single box or a whole batch both work) and stacks the results back into an [N, 4] tensor.

Inputs and output

  • boxes - a cdlTensor of [N, 4] boxes in (x1, y1, x2, y2) corner format.

The output is boxes_ccwh, a cdlTensor of [N, 4] boxes in (cx, cy, w, h) format. Pure transform, no learned state, deterministic.

Where it fits

ComfyDL's detection nodes standardize on corner format for their cdlBbox tensors, but you'll hit center format in the places you'd expect - anchor priors, and any model output that regresses center offsets. This node is what you use when a box arrives in corners but the next step expects center form. The two converters round-trip: center → corner → center returns your original boxes, which makes a nice sanity check if you ever suspect a formatting bug rather than a math one. In the README's detection workflow, keep this one near CdlMultiboxPrior when priors are expressed in center terms.

Installing it

It ships with ComfyDL, one install for the whole pack:

cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt

Restart ComfyUI, or search "ComfyDL" in ComfyUI Manager. No dependency beyond what ComfyUI already provides.

Gotchas

  • This node doesn't check box validity. If x2 < x1 (a flipped box from upstream), you'll get a negative width and a center that's somewhere wrong - the math still runs, which is exactly why it's easy to miss. Validate upstream.
  • It's easy to grab the wrong converter when you're tired: corner→center is CdlBoxCornerToCenter, center→corner is CdlBoxCenterToCorner. The names say what they take in. If your downstream IoU looks insane, check you picked the one that produces the format the next node expects.
Categoryd2l/ObjectDetection

Inputs (1)

NameTypeDefaultDescription
boxesTENSOR

Outputs (1)

NameTypeDescription
boxes_ccwhTENSOR