Nodes/opencv-comfyui/OpenCV getRectSubPix_0
ComfyUI Node

OpenCV getRectSubPix_0

Crop With Sub-Pixel Precision, Not Pixel Luck

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getRectSubPix_0
  • image
  • patch
  • nparray
patchSize
center
patchType

OpenCV getRectSubPix_0 extracts a rectangular patch from an image centered on a point that can be between pixels, using bilinear interpolation. It wraps cv2.getRectSubPix(image, patchSize, center, patch, patchType). Where a normal crop has to snap your center to integer coordinates, this one keeps the fractional part and interpolates the pixels around it - so you get the same visual content regardless of where exactly you point, which matters a lot in tracking and alignment work.

The mechanism

You give it an image, a patch size, and a center. If the center lands at (100.3, 75.7) - an impossible pixel location - OpenCV weights the four surrounding pixels to synthesize the patch as if the camera had been aimed precisely there. That's the whole trick, and it's a big one for anything where sub-pixel accuracy matters: optical flow, template tracking, video stabilization, precise feature matching. If you've ever fought "why is my crop off by a pixel depending on rounding," this node is the answer.

Inputs and outputs

  • image (NPARRAY) - the source, an OpenCV numpy array (grayscale or color).
  • patchSize (STRING) - patch dimensions as a literal, [64, 64]. Brackets required - this is one of the composite-type strings the pack parses with ast.literal_eval.
  • center (STRING) - the center as a literal too, [100.3, 75.7]. Float values are fine and are the point of the node.
  • patchType (INT) - output dtype. -1 matches the source type; 5 is CV_32F; 6 is CV_64F. Use -1 unless you need float output downstream.
  • patch (NPARRAY, optional) - OpenCV's out-parameter for the result. Ignore it; the node returns the patch as its output anyway.
  • Output nparray - the extracted patch.

It's a crop, so the output is genuinely an image - you can pipe it back through Nparrays2Image and preview it, which puts this node in the small club of this pack's outputs that behave like what a beginner expects. One subtlety: the patch is centered on the point, so with a [64, 64] patch the effective bounding box is center ± 32, and near image edges OpenCV clamps to available pixels rather than erroring.

When you'd reach for it

Any time "where is this feature, to the fraction of a pixel" is the question. Template-tracking loops, dense optical-flow preprocessing, re-centering crops in a stabilization graph, or extracting a consistent patch from a moving subject so downstream nodes see identical framing. It's the deterministic, interpolating alternative to a naive integer crop - the kind of precision you don't get for free from a simple slice.

Install

ComfyUI Manager, search opencv-comfyui (display "OpenCV"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart, no models. Pack rules: source image comes in as NPARRAY via Image2Nparray with batch size 1, and both composite literals need brackets - [100.3, 75.7], not 100.3, 75.7, or you'll get invalid syntax (<unknown>, line 0). The _1 variant is an identical duplicate overload; pick either.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
imageNPARRAY
patchSizeSTRING
centerSTRING
patchTypeINT
patchoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY