cv2.reduceArgMax
Which column is the brightest one?
- src
- nparray
An undocumented function, wrapped
Here's the thing about cv2.reduceArgMax: OpenCV exposes it, uses it, and barely documents it. There's no page for it in the OpenCV docs the way there is for cv2.reduce - it behaves as the argmax counterpart of reduce, returning indices instead of values along one axis, and the pack's widget tooltips for it are empty, which is the pack telling you it has nothing to add either.
So: it collapses a 2-D matrix along one axis and tells you which element was the maximum in each resulting position. reduce answers "what's the biggest value in this column"; reduceArgMax answers "which row was it in".
That's a genuinely useful question - which row has the most white pixels, which column of a flow field moved the most, which scanline the object is on - and it saves you from np.argmax plus reshape gymnastics in the middle of an image graph.
Inputs and output
- src - 2-D array. IMAGE / MASK / NPARRAY all resolve; the node is on the pack's per-frame safe list, so an IMAGE batch is processed frame by frame. No LATENT socket.
- axis - required, defaults to 0, which collapses to a single row (one index per column) by the same convention
cv2.reduce'sdimuses. Set it to 1 for one index per row. - lastIndex (optional, default False) - picks which occurrence wins when the maximum appears more than once in a line: the first or the last. Because upstream documents this parameter poorly, the practical move is to run it both ways on a test array and watch the index shift; then you know your build's behaviour instead of trusting a guess.
Output: a single nparray of indices - not a picture, not a scalar.
Where it earns its place
Two honest uses. First, as a fast locator on a 1-D-ish problem: reduce a mask to per-row counts with cv2.reduce, then take an argmax over that to find the row, then use the number to place a crop or draw a line. Second, in signal-ish pipelines where you've already reduced an image to a curve and want the peak position, not the peak height - the honest 1-D "where was the peak" answer that a plotting node would render as a curve.
What it is not is a general "find the brightest pixel". For that you want cv2.minMaxLoc, which gives you both value and location of the global extremum of a single channel in one call. reduceArgMax gives you a whole row of answers along one axis, which is a different (and sometimes better) thing: it degrades gracefully across a shape that varies in brightness.
Also note the axis/dim arithmetic: with a MASK (H, W) and axis=0, you get one index per column, each in 0..H-1. That's "for this x, which y". If you wanted "for this y, which x", that's axis=1. Getting that backwards is the most common way to spend ten minutes staring at a line drawn 90° off.
Availability, which is its own gotcha
The pack's registry is generated in two steps: from the type stubs, then probed against your installed OpenCV. Functions this build doesn't implement are skipped rather than installed-and-broken. reduceArgMax is present in normal opencv-contrib-python-headless~=5.0.0.93 builds (the version this pack is curated against), but if you're on an unusual build - a distro package, a hand-rolled OpenCV, an ancient wheel - the node may simply not appear in the menu, and no amount of restarting will conjure it. Confirm what your install actually has:
python -c "import cv2; print(cv2.reduceArgMax)"
Install
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Manager route: search "ComfyUI CV" by bmad4ever. Python ≥ 3.12 and a ComfyUI recent enough to use the V3 node API. Restart ComfyUI, reload the browser page.
Traps
Multi-channel surprise. src is documented as a 2-D matrix; an IMAGE arrives 3-channel. Either extract a channel first or accept that you're indexing a channel-flattened result - and check the output shape with CV Array Shape before you wire an index into a crop.
Reading the output as an image. It's an index array. Wire it into array consumers (CV CastArray, arithmetic, a plot) - not a preview expecting 0–255 pixels.
Assuming ties are handled the way you'd like. lastIndex decides, and the default (False) means first occurrence. If a line is flat, "first" is arbitrary but deterministic; if that matters, prove it on a test array.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY,IMAGE,MASK | - - - Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. | |
| axis | INT | 0-2147483648–2147483647 | - - - |
| lastIndexopt | BOOLEAN | false | - - - Preset to the OpenCV default (False). |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |