cv2.reduceArgMin
The same trick, for the darkest line
- src
- nparray
Argmin, same shape as argmax
cv2.reduceArgMin is the mirror of cv2.reduceArgMax: collapse a 2-D matrix along one axis and return the index of the smallest value at each position, rather than the value itself. The pack wraps both identically, and both come with empty tooltips because upstream OpenCV barely documents either - they exist in the Python bindings, they're used internally, and the docs don't have a page for them in the way cv2.reduce does.
When argmin is the question you're asking: the topmost row with any mask, the darkest column of a scan, the index of the nearest object in a distance map, the position of the minimum of a profile you built with cv2.reduce.
Inputs
- src - 2-D array. IMAGE / MASK / NPARRAY all resolve to an array; the node is on the per-frame safe list, so an IMAGE batch is handled frame by frame and re-stacked. No LATENT socket (the pack only extends image sockets to latent for channel-agnostic arithmetic and geometry).
- axis - required, default 0, which collapses to a single row: one index per column, i.e. "for this x, which y". Set it to 1 for "for this y, which x".
- lastIndex (optional, default False) - which occurrence of the minimum wins when there are ties. Since upstream's description of this flag is thin, test both settings on an array you control if the answer matters; don't take a guess from a forum post.
Output: nparray of indices.
The honest use cases
Distance-transform workflows. Take a binary mask, run cv2.distanceTransform (distance to the nearest zero pixel, i.e. the mask edge), and now a minimum is meaningful: it's the boundary. Collapse that with reduceArgMin and you get, for each row or column, the location of the nearest edge - a cheap way to find a bounding line, a surface, or the first non-background scanline without writing a loop.
The second family is reporting: reduce an array with REDUCE_MIN to get values, reduceArgMin to get where they were. Both at once is a nice "the darkest column is #412, its value is 3" summary you can print with a text node, and far cheaper than eyeballing a preview.
The candour part: this is a scratch node. No shipped example workflow in the pack uses reduceArgMin or reduceArgMax - they exist because the registry is auto-generated from the type stubs and these functions are in the stubs. That's not a knock on it (one of the more useful things about a pack that wraps all of cv2 is that the odd corners are reachable), but it does mean there's no screenshot tutorial to follow. Read it as: an ordinary cv2 call with the pack's socket plumbing on top, and no hidden behaviour.
Worth comparing with the neighbours before you commit:
- cv2.minMaxLoc for the global extremum of a single channel, value and location in one go (and a curated wrapper in this pack) - the right tool when "one answer for the whole image" is what you want.
- cv2.reduce when you want the values, not the positions (
REDUCE_MIN). - cv2.findNonZero when your question is really "where is anything at all", which is usually the faster question to ask of a binary mask.
Availability
The pack builds its registry from the installed build and skips anything your cv2 doesn't expose, so if this node is absent from the menu, your OpenCV doesn't have it - restarting won't help. The quick check:
python -c "import cv2; print(cv2.reduceArgMin)"
Install
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Or via ComfyUI Manager: search "ComfyUI CV" (author bmad4ever). The pack wants Python ≥ 3.12 and a ComfyUI built on the V3 node API; on older ComfyUI versions nothing from this pack registers. Restart afterwards, then reload the page.
Traps
Channel confusion. An IMAGE is 3-channel; if you pipe one straight in, the index you get is over a flattened/flattened-by-channel reduction rather than over pixels. Extract a channel first, or use a MASK, where single-channel is guaranteed.
Axis flipped. "For this x, which y" is axis=0. If your result draws a line at 90° to what you expected, that's the whole bug.
Indexes treated as pixels. The output is an array of positions. It drives crop boxes, draw calls and arithmetic - not an image preview. Confirm the shape with CV Array Shape once and stop second-guessing it.
Overflow on the way in. If you built the profile with cv2.reduce using SUM on a uint8 input, fix that node's dtype to CV_32F first - an argmin over wrapped values is confidently wrong.
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 | — |