Nodes/ComfyUI-RefineNode/RefineNode Restore Mask To Original
ComfyUI Node

RefineNode Restore Mask To Original

Find a mask on the crop, then put it back on the original image

By 1Kynx·Created 4 months ago·Updated 25 days ago· 30
RefineNode Restore Mask To Original
  • mask
  • info
  • mask

Here's a workflow you'll actually hit: RefineNode Preprocess Mask crops your product and focuses on a region, then you run a second mask detector (SAM, a segmentation model, whatever) on that cropped or model-space image to find, say, the exact outline of the text you're fixing. Great. Except that detected mask lives in cropped coordinates, and the moment you try to use it on the original full-resolution image, it's in the wrong place and the wrong size. RefineNode Restore Mask To Original exists purely to fix that coordinate mismatch.

It takes a mask that was detected after the crop-and-resize pipeline and maps it back to the original image's coordinate space, using the info metadata that recorded where everything came from.

How it works

Two inputs: mask (the mask detected on the processed image) and info (the REFINENODE_INFO from Preprocess Mask, or the updated info from Reference Image Process). One output: mask, restored to the original image's size and position.

The mechanism is a straight reverse-transform. If your image only went through Preprocess Mask, the node reverses the focus_crop by mapping the detected mask back through the original crop_box. If it also passed through Reference Image Process, it first reverses that node's resize/crop/fill transform, then applies the crop_box reversal on top. Read that as: this node knows the entire geometry of the pipeline up to that point, as long as you keep feeding it the info.

It handles batches, too. Multiple masks in (say, the output of RefineNode Slice And Match Masks) get every mask restored. If there's only one info item for several masks, that same coordinate transform is reused across all of them.

When you'd actually use it

The README's example is the honest one: crop a product with Preprocess Mask, run a mask detector on the cropped/model image to get a precise region, then restore that region to original coordinates. Two scenarios make this worth having:

  • You want to refine only the detected sub-region (the text, not the box around it) - detect it on the crop where detection is easy, restore it, and feed the result back as a new mask.
  • You're validating or comparing masks against the original image and need everything in one coordinate system.

It pairs naturally with RefineNodeReferenceImageProcess: that node records its transform into the info, and this node is the counterpart that unwinds it. Same bookkeeping, opposite direction.

Install

ComfyUI Manager, search "ComfyUI-RefineNode", or:

cd ComfyUI/custom_nodes
git clone https://github.com/1Kynx/ComfyUI-RefineNode.git

Restart ComfyUI. No model files, no extra dependencies - it's pure coordinate math on masks.

Gotchas

  • "Missing RefineNode info for mask restore" - the info slot is empty or wasn't produced by Preprocess Mask/Reference Image Process. There's no other source for the transform; re-wire it.
  • The restored mask can look slightly soft at the edges because it's been resized in both directions. That's expected; if you're going to reuse it as a refinement mask, a small mask_grow on the compositing side usually absorbs it.
  • This node reverses the transforms in order - if you add another resize step between Reference Image Process and this node, the geometry no longer matches. Keep the pipeline linear.
CategoryRefineNode/Mask

Inputs (2)

NameTypeDefaultDescription
maskMASK
infoREFINENODE_INFO

Outputs (1)

NameTypeDescription
maskMASK