Nodes/opencv-comfyui/OpenCV reduceArgMax_0
ComfyUI Node

OpenCV reduceArgMax_0

Find the brightest row or column — reduceArgMax_0

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV reduceArgMax_0
  • src
  • dst
  • nparray
axis
lastIndex

reduceArgMax_0 answers a surprisingly common question: where is the maximum? Not what it's worth - where it lives. Given an image (or any matrix), it returns the index of the maximum value along one axis, as a vector of integer positions.

That's the "argmax" operation from numpy, exposed through OpenCV by opencv-comfyui (geroldmeisinger/opencv-comfyui). The real-world use in ComfyUI is peak location. Run your image through a channel-mean or threshold, then argmax a row to find which column holds the bright spot - a laser dot, a highlight, the leftmost pixel of a detected object. It's the plumbing for "find the thing without a detector."

The inputs that matter

  • src - an NPARRAY, straight from Image2Nparray. Grayscale is the sane choice; a raw BGR frame will argmax across the whole thing and mostly report channel noise.
  • axis - the direction to scan. 0 finds, for each column, the row index of its max - output is a row of length = image width. 1 finds, for each row, the column index of its max - output is a column of length = image height. (Yes, it's the same convention as reduce_0's dim, and yes, everyone reverses it at least once.)
  • lastIndex - a boolean. False returns the first occurrence of the max; True returns the last. Matters when a value ties - say, an all-black row where the max is zero everywhere.
  • dst - optional out-parameter from OpenCV's call-by-reference style; leave it unconnected.

Output is an nparray of integer indices. Like its sibling reduce_0, it is not an image - don't feed it to Nparrays2Image. Wire the index into a math or routing node instead.

Installing it

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

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

restart, and confirm OpenCV is there:

pip install opencv-contrib-python

The gotchas

First, ties. If a row is uniformly dark, argmax returns the first pixel with that value - which is arbitrary but deterministic, and lastIndex flips it. Second, remember the axis direction: axis=0 gives you row indices, axis=1 gives column indices. Third, and this is the pack-wide rule: everything runs on NPARRAY, BGR, 0–255 numpy arrays, so the Image2Nparray in and Nparrays2Image out bridge is your friend - just don't expect the argmax output itself to be a viewable image.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
srcNPARRAY
axisINT
lastIndexBOOLEAN
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY