Nodes/opencv-comfyui/OpenCV meanShift_1
ComfyUI Node

OpenCV meanShift_1

The same object tracker, other overload

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV meanShift_1
  • probImage
  • int
  • literal
window
criteria

You know the drill by now: meanShift_1 and meanShift_0 are the same node, generated once per cv2 overload. Same inputs, same outputs, same behavior - the only meaningful difference is which overload the pack routed you to, and since there's no Image2UMat node, even that is academic. If you're here you want the mean-shift tracking algorithm, and this page is the one that tells you how to make it behave.

Mean shift is one of the oldest tricks in the OpenCV playbook: take a probability image (where bright = likely target) and a starting rectangle, then repeatedly nudge the rectangle toward the centroid of the bright region inside it until it stops moving or you hit a iteration/epsilon limit. It's the pre-deep-learning way to keep a window glued to an object across video frames - and in a ComfyUI context, the thing it's actually good for is per-frame video processing, like stabilizing a region you're feeding to an image model or keeping a mask aligned to a moving subject.

What you must get right

The three inputs are where all the fun lives:

  • probImage - one NPARRAY, and it should be single-channel (a probability or backprojection map). A plain BGR photo in here triggers the pack's most common assertion: error: (-215:Assertion failed) img.type() == CV_8UC1. If you only have a color image, convert to grayscale with cvtColor (code=6 for BGR2GRAY) before it hits this node.
  • window - STRING literal, Python syntax: (x, y, width, height), e.g. (100, 50, 60, 60).
  • criteria - STRING literal: (type, max_iter, epsilon), e.g. (3, 10, 1.0). 3 means "stop on iteration count OR epsilon, whichever first." This is a TermCriteria, the same structure CamShift and other iterative OpenCV functions use.

Outputs: int (how many iterations ran) and literal (the updated window, as a string - ((x, y, w, h))). The literal is text, so read it from the queue or feed it to a string node; don't wire it into anything image-shaped.

Setup and expectations

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

or ComfyUI Manager → "opencv-comfyui". Dependencies: opencv-contrib-python, numpy, torch. The literal-parsing trap is the same across the pack: forget the parentheses and you get invalid syntax (<unknown>, line 0). Use real tuple syntax.

And a reality check: mean shift is a means to a narrow end. It's not for stills, it's not a general detector - it's a "keep following the bright blob" loop for sequences of frames. If that's your workflow, this is a free, dependency-light way to do it. If you grabbed it thinking it'd find objects in a single image, that's not this node's job.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
probImageNPARRAY
windowSTRING
criteriaSTRING

Outputs (2)

NameTypeDescription
intINT
literalSTRING