ComfyUI Node

Focus Point From Mask

SAM-to-bokeh in two wires

By EricRollei·Created 8 months ago·Updated 4 months ago· 19
Focus Point From Mask
  • image
  • mask
  • focus_point
  • preview
  • x
  • y
fallback_x50.0
fallback_y50.0

FocusPointFromMask is the interactive heart of the refocus workflow. It takes an image and a mask, computes the mask's weighted centroid, and turns it into a FOCUS_POINT - the exact coordinate where your refocus should stay sharp. The workflow it enables is the one the README leads with: connect an Impact Pack SAM Detector (or MaskPainter) to your image, click on the thing you want in focus, and the node extracts the point automatically. Click a face, get a portrait-DOF refocus. Click a background car, and the subject goes artfully soft. Two clicks, two different photographs.

What it gives you

  • focus_point (FOCUS_POINT) - the centroid as an (x, y) + image dimensions struct, wired into Compute Defocus Map.
  • preview (IMAGE) - your image with a semi-transparent green mask overlay and a red crosshair at the focus point, so you can verify the extraction in one glance.
  • x / y (INT) - the raw pixel coordinates, for debugging or for feeding other math.

Three outputs is generous for a node this small, and the x/y pair is the one people forget: it's handy if you want to see the number that went in, or reuse the coordinate elsewhere in the graph.

Inputs

  • image (required)
  • mask (required) - from SAM, MaskPainter, or any mask source. The centroid is weighted by mask intensity, so soft masks still work sensibly.
  • fallback_x / fallback_y (50 each) - percentage coordinates used if the mask is empty. The node prints a warning to the console when it falls back, so keep an eye on the log if your focus point keeps landing at center.

The classic setup

Load Image ──▶ SAM Detector ──▶ FocusPointFromMask ──▶ Compute Defocus Map
              (Impact Pack)         ▲                        │
              click your target ────┘                        ▼
                                                  Genfocus Bokeh (Native)

SAM Detector needs to be present as an image input for its bounding box - the README's diagram passes the loaded image to both the SAM detector and FocusPointFromMask. Impact Pack is the standard provider here (it ships the SAM detector nodes this is designed to pair with), but any mask source works - the node doesn't care where the mask came from.

Gotchas

  • An empty/black mask triggers the fallback silently from the node's perspective - the only sign is the console warning. If your refocus keeps coming out centered, your mask isn't producing what you think. Preview the mask before this node.
  • The centroid is a single point, and blur is computed by depth distance from that point's plane - so a large mask on a varied-depth subject produces a focus point in the middle of it, not a "keep this whole region sharp" promise. For an object with real depth (a person's whole body), you may want to mask just the face for the plane you actually care about.
  • The point is clamped to image bounds, so edge cases won't crash.

Install

Part of the Refocus pack - ComfyUI Manager ("Refocus - Generative Refocusing") or git clone https://github.com/EricRollei/comfyui-refocus into custom_nodes/, restart. No models, no pip extras; pure numpy centroid math. The only "install" beyond that is Impact Pack itself if you want the SAM Detector source the workflow expects.

CategoryRefocus/Defocus

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
maskMASKMask from SAM, MaskPainter, or any mask source
fallback_xoptFLOAT50.00–100Fallback X% if mask is empty
fallback_yoptFLOAT50.00–100Fallback Y% if mask is empty

Outputs (4)

NameTypeDescription
focus_pointFOCUS_POINT
previewIMAGE
xINT
yINT